-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.go
213 lines (175 loc) · 5.74 KB
/
graphql.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package kosmo
import (
"reflect"
"github.com/graphql-go/graphql"
)
type metaInformations struct {
name string
description string
}
type functionInformations struct {
metaInformations
args graphql.FieldConfigArgument
resolver func(graphql.ResolveParams) (interface{}, error)
}
type typeInformations struct {
metaInformations
typ graphql.Output
}
// INFORMATIONS
// Contains all information needed the build a graphql schema through reflection
// metaInformations name and description are not always both filled
func reflectFunctionInformations(function interface{}) functionInformations {
function, description := validateResolverArgument(function)
reflectedFunction := reflect.ValueOf(function)
name := runtimeFunctionName(reflectedFunction)
args := functionToConfigArguments(reflectedFunction)
resolver := functionToResolver(reflectedFunction)
return functionInformations{
metaInformations: metaInformations{
name: name,
description: description,
},
args: args,
resolver: resolver,
}
}
func reflectTypeInformations(value interface{}) typeInformations {
value, description := validateResolverArgument(value)
rinfos := typeInformations{
metaInformations: metaInformations{
description: description,
},
}
switch reflectTypeKind(value) {
case "struct":
rinfos.typ = structToGraph(value)
default:
rinfos.typ = sliceToGraph(value)
}
return rinfos
}
// RESOLVER
// uses reflection to extract the arguments of a given function and creates a resolver type based
// on that arguments. The functionToResolver func returns a resolver that can be used in graphql-go's
// graphql library. The resolver-function call is currently kind of expensive becaus it uses reflection
// to call the passed in function
func functionToConfigArguments(fn reflect.Value) graphql.FieldConfigArgument {
argumentConfig := graphql.FieldConfigArgument{}
arg, hasArgs := reflectArgumentFromResolverFunction(fn)
if !hasArgs {
return argumentConfig
}
for i := 0; i < arg.NumField(); i++ {
argField := arg.Field(i)
config := parseTagConfig(argField.Tag.Get("kosmo"))
if config.Ignore {
continue
}
argumentType := nativeTypeToGraphQL(argField.Type.Name())
if config.Require {
argumentType = graphql.NewNonNull(argumentType)
}
argumentConfig[argField.Name] = &graphql.ArgumentConfig{
Type: argumentType,
}
}
return argumentConfig
}
func functionToResolver(fn reflect.Value) func(graphql.ResolveParams) (interface{}, error) {
arg, _ := reflectArgumentFromResolverFunction(fn)
return func(p graphql.ResolveParams) (interface{}, error) {
functionArguments := []reflect.Value{}
if arg != nil {
functionArguments = []reflect.Value{createFunctionStructArgumentFromMap(arg, p.Args)}
}
results := fn.Call(functionArguments)
returnValue := results[0].Interface()
returnError := results[1].Interface()
if returnError != nil {
return returnValue, returnError.(error)
}
return returnValue, nil
}
}
// TYPES
// uses reflection to extract type informations of the given value.
// the created graphql.Objects are cached to prevent the "Multiple types with the same name" error
// that graphql-go's graphql library returns if multiple graphql.Objects with the same name are created
func structToGraph(genStruct interface{}) *graphql.Object {
conf := structToGraphConfig(genStruct)
obj := graphqlObjectCache.Read(conf.Name, gqlObjFallbackFactory(conf)).(*graphql.Object)
return obj
}
func sliceToGraph(genSlice interface{}) *graphql.List {
conf := sliceToGraphConfig(genSlice)
obj := graphqlObjectCache.Read(conf.Name, gqlObjFallbackFactory(conf)).(*graphql.Object)
return graphql.NewList(obj)
}
// CONFIGS
// the created graphql.ObjectConfigs are build from the field names of the underling struct type
// the created configs are used to create graphql.Objects
func structToGraphConfig(genStruct interface{}) graphql.ObjectConfig {
underlingType := reflect.TypeOf(genStruct)
return buildObjectConfigFromType(underlingType)
}
func sliceToGraphConfig(genSlice interface{}) graphql.ObjectConfig {
underlingType := reflect.TypeOf(genSlice).Elem()
return buildObjectConfigFromType(underlingType)
}
func buildObjectConfigFromType(reflectedType reflect.Type) graphql.ObjectConfig {
name, infos := reflectStructInformations(reflectedType)
fields := nativeFieldsToGraphQLFields(infos)
return graphql.ObjectConfig{
Name: name,
Fields: fields,
}
}
// FIELDS
// Uses reflection to map the fields in a struct-type to create graphql.Types
// based on the type of the field
func nativeFieldsToGraphQLFields(fields []reflect.StructField) graphql.Fields {
graphQLFields := graphql.Fields{}
for _, field := range fields {
config := parseTagConfig(field.Tag.Get("kosmo"))
if config.Ignore {
continue
}
typ := nativeFieldToGraphQL(field)
graphQLFields[field.Name] = &typ
}
return graphQLFields
}
func nativeFieldToGraphQL(field reflect.StructField) graphql.Field {
var nTyp graphql.Output
switch field.Type.Kind().String() {
case "struct":
conf := buildObjectConfigFromType(field.Type)
nTyp = graphqlObjectCache.Read(conf.Name, gqlObjFallbackFactory(conf)).(*graphql.Object)
case "slice":
conf := buildObjectConfigFromType(field.Type.Elem())
nTyp = graphql.NewList(graphqlObjectCache.Read(conf.Name, gqlObjFallbackFactory(conf)).(*graphql.Object))
default:
nTyp = nativeTypeToGraphQL(field.Type.Name())
}
return graphql.Field{
Type: nTyp,
Description: field.Tag.Get("description"),
}
}
func nativeTypeToGraphQL(typeName string) graphql.Type {
switch typeName {
case "int":
return graphql.Int
case "uint":
return graphql.Int
case "string":
return graphql.String
case "float32":
return graphql.Float
case "float64":
return graphql.Float
default:
panic(typeName + " is not implemented yet")
}
}