-
Notifications
You must be signed in to change notification settings - Fork 25
/
types.go
70 lines (60 loc) · 1.91 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gqlserver
import (
"reflect"
go2gql_scalars "github.com/EGT-Ukraine/go2gql/api/scalars"
tools_scalars "github.com/bhoriuchi/graphql-go-tools/scalars"
"github.com/graphql-go/graphql"
)
// Scalar types.
var (
JSON = tools_scalars.ScalarJSON
Bytes = go2gql_scalars.GraphQLBytesScalar
Uint64 = go2gql_scalars.GraphQLUInt64Scalar
Int64 = go2gql_scalars.GraphQLInt64Scalar
Uint32 = go2gql_scalars.GraphQLUInt32Scalar
NonNullJSON = graphql.NewNonNull(JSON)
NonNullUint64 = graphql.NewNonNull(Uint64)
NonNullInt64 = graphql.NewNonNull(Int64)
NonNullUint32 = graphql.NewNonNull(Uint64)
NonNullID = graphql.NewNonNull(graphql.ID)
NonNullBoolean = graphql.NewNonNull(graphql.Boolean)
NonNullInt = graphql.NewNonNull(graphql.Int)
NonNullString = graphql.NewNonNull(graphql.String)
)
func toNonNull(ofType graphql.Type) graphql.Type {
if _, ok := ofType.(*graphql.NonNull); ok {
return ofType
}
return graphql.NewNonNull(ofType)
}
// NewListNonNullList constructs [T]! type.
func NewListNonNullList(ofType graphql.Type) graphql.Type {
return graphql.NewNonNull(graphql.NewList(ofType))
}
// NewListNonNullElem constructs [T!] type.
func NewListNonNullElem(ofType graphql.Type) graphql.Type {
return graphql.NewList(toNonNull(ofType))
}
// NewListNonNullBoth constructs [T!]! type.
func NewListNonNullBoth(ofType graphql.Type) graphql.Type {
return graphql.NewNonNull(graphql.NewList(toNonNull(ofType)))
}
// NewStringEnum constructs an enum type.
func NewStringEnum[T ~string](name, desc string, values ...T) *graphql.Enum {
vm := graphql.EnumValueConfigMap{}
for _, value := range values {
vm[string(value)] = &graphql.EnumValueConfig{Value: value}
}
return graphql.NewEnum(graphql.EnumConfig{
Name: name,
Description: desc,
Values: vm,
})
}
// Optional turns zero value to nil.
func Optional(value any) any {
if reflect.ValueOf(value).IsZero() {
return nil
}
return value
}