taskcafe/internal/graph/scalars.go

27 lines
564 B
Go
Raw Permalink Normal View History

2020-04-10 04:40:22 +02:00
package graph
import (
"io"
"strconv"
2020-04-10 04:40:22 +02:00
"github.com/99designs/gqlgen/graphql"
"github.com/google/uuid"
"github.com/pkg/errors"
)
// MarshalUUID converts a UUID to JSON string
2020-04-10 04:40:22 +02:00
func MarshalUUID(uuid uuid.UUID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
w.Write([]byte(strconv.Quote(uuid.String())))
})
}
// UnmarshalUUID converts a String to a UUID
2020-04-10 04:40:22 +02:00
func UnmarshalUUID(v interface{}) (uuid.UUID, error) {
if uuidRaw, ok := v.(string); ok {
return uuid.Parse(uuidRaw)
}
return uuid.UUID{}, errors.New("uuid must be a string")
}