30 lines
546 B
Go
30 lines
546 B
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/99designs/gqlgen/graphql"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func MarshalObjectID(value primitive.ObjectID) graphql.Marshaler {
|
|
|
|
return graphql.WriterFunc(func(writer io.Writer) {
|
|
|
|
io.WriteString(writer, strconv.Quote(value.Hex()))
|
|
})
|
|
|
|
}
|
|
|
|
func UnmarshalObjectID(value interface{}) (primitive.ObjectID, error) {
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
return primitive.ObjectIDFromHex(str)
|
|
}
|
|
|
|
return primitive.NilObjectID, errors.New("invalid Object ID string")
|
|
}
|