Skip to content

Commit 390ff6c

Browse files
authored
Merge pull request #3 from DrOctavius/main
added non nil uuid graphql scalar
2 parents 7785cbd + 4855405 commit 390ff6c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Diff for: core/listeners/graphql/scalar/scalars.graphqls

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ scalar TimeDefault
44
scalar TimeRFC3339
55
"""2006-01-02T15:04:05.999999999Z07:00"""
66
scalar TimeRFC3339Nano
7-
scalar UUID
7+
8+
9+
scalar UUID
10+
scalar NonNilUUID

Diff for: core/listeners/graphql/scalar/uuid_non_nil.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scalar
2+
3+
import (
4+
"fmt"
5+
"github.com/99designs/gqlgen/graphql"
6+
"github.com/google/uuid"
7+
"io"
8+
)
9+
10+
func MarshalNonNilUUID(u uuid.UUID) graphql.Marshaler {
11+
return graphql.WriterFunc(func(w io.Writer) {
12+
if u == uuid.Nil {
13+
// todo: what to do in this case?!
14+
}
15+
w.Write([]byte("\"" + u.String() + "\""))
16+
})
17+
}
18+
19+
func UnmarshalNonNilUUID(v interface{}) (uuid.UUID, error) {
20+
switch v := v.(type) {
21+
case string:
22+
_uuid, _err := uuid.Parse(v)
23+
if _err != nil {
24+
return uuid.Nil, _err
25+
}
26+
if _uuid == uuid.Nil {
27+
return uuid.Nil, fmt.Errorf("%T nil uuid is not permitted", v)
28+
}
29+
return _uuid, nil
30+
default:
31+
return uuid.Nil, fmt.Errorf("%T is not a string", v)
32+
}
33+
}

0 commit comments

Comments
 (0)