This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.go
237 lines (216 loc) · 6.34 KB
/
resolvers.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package graphql
import (
"reflect"
"strings"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
//
// ScalarResolver represents a collection of methods whose products represent
// the input and response values of a scalar type.
//
// == Example input SDL
//
// """
// Timestamps are great.
// """
// scalar Timestamp
//
// == Example implementation
//
// // MyTimestampResolver implements ScalarResolver interface
// type MyTimestampResolver struct {
// defaultTZ *time.Location
// logger logrus.LogEntry
// }
//
// // Serialize serializes given date into RFC 943 compatible string.
// func (r *MyTimestampResolver) Serialize(val interface{}) interface{} {
// // ... implementation details ...
// }
//
// // ParseValue takes given value and coerces it into an instance of Time.
// func (r *MyTimestampResolver) ParseValue(val interface{}) interface{} {
// // ... implementation details ...
// // eg. if val is an int use time.At(), if string time.Parse(), etc.
// }
//
// // ParseValue takes given value and coerces it into an instance of Time.
// func (r *MyTimestampResolver) ParseValue(val ast.Value) interface{} {
// // ... implementation details ...
// //
// // eg.
// //
// // if string value return value,
// // if IntValue Atoi and return value,
// // etc.
// }`
//
type ScalarResolver interface {
// Serialize an internal value to include in a response.
Serialize(interface{}) interface{}
// ParseValue parses an externally provided value to use as an input.
ParseValue(interface{}) interface{}
// ParseLiteral parses an externally provided literal value to use as an input.
ParseLiteral(ast.Value) interface{}
}
//
// InterfaceTypeResolver represents a collection of methods whose products
// represent the input and response values of a interface type.
//
// == Example input SDL
//
// "Pets are the bestest family members"
// interface Pet {
// "name of this fine beast."
// name: String!
// }
//
// == Example implementation
//
// // PetResolver implements InterfaceTypeResolver
// type PetResolver struct {
// logger logrus.LogEntry
// }
//
// // ResolveType should return type reference
// func (r *PetResolver) ResolveType(val interface {}, _ graphql.ResolveTypeParams) graphql.Type {
// // ... implementation details ...
// switch pet := val.(type) {
// when *Dog:
// return schema.DogType // Handled by type identified by 'Dog'
// when *Cat:
// return schema.CatType // Handled by type identified by 'Cat'
// }
// panic("Unimplemented")
// }`,
//
type InterfaceTypeResolver interface {
ResolveType(interface{}, ResolveTypeParams) *Type
}
//
// UnionTypeResolver represents a collection of methods whose products
// represent the input and response values of a union type.
//
// == Example input SDL
//
// """
// Feed includes all stuff and things.
// """
// union Feed = Story | Article | Advert
//
// == Example implementation
//
// // FeedResolver implements UnionTypeResolver
// type FeedResolver struct {
// logger logrus.LogEntry
// }
//
// // ResolveType should return type reference
// func (r *FeedResolver) ResolveType(val interface {}, _ graphql.ResolveTypeParams) graphql.Type {
// // ... implementation details ...
// switch entity := val.(type) {
// when *Article:
// return schema.ArticleType
// when *Story:
// return schema.StoreType
// when *Advert:
// return schema.AdvertType
// }
// panic("Unimplemented")
// }
//
type UnionTypeResolver interface {
ResolveType(interface{}, ResolveTypeParams) *Type
}
// DefaultResolver uses reflection to attempt to resolve the result of a given
// field.
//
// Heavily borrows from: https://github.com/graphql-go/graphql/blob/9b68c99d07d901738c15564ec1a0f57d07d884a7/executor.go#L823-L881
func DefaultResolver(source interface{}, fieldName string) (interface{}, error) {
sourceVal := reflect.ValueOf(source)
if sourceVal.IsValid() && sourceVal.Type().Kind() == reflect.Ptr {
sourceVal = sourceVal.Elem()
}
if !sourceVal.IsValid() {
return nil, nil
}
// Struct
if sourceVal.Type().Kind() == reflect.Struct {
return resolveStructValue(sourceVal, fieldName)
}
// map[string]interface
if sourceMap, ok := source.(map[string]interface{}); ok {
property := sourceMap[fieldName]
val := reflect.ValueOf(property)
if val.IsValid() && val.Type().Kind() == reflect.Func {
// try type casting the func to the most basic func signature
// for more complex signatures, user have to define ResolveFn
if propertyFn, ok := property.(func() interface{}); ok {
return propertyFn(), nil
}
}
return property, nil
}
// last resort, return nil
return nil, nil
}
func resolveStructValue(source reflect.Value, fieldName string) (interface{}, error) {
targetFieldName := strings.Title(fieldName)
for i := 0; i < source.NumField(); i++ {
valueField := source.Field(i)
typeField := source.Type().Field(i)
if typeField.Name == targetFieldName {
// If ptr and value is nil return nil
if valueField.Type().Kind() == reflect.Ptr && valueField.IsNil() {
return nil, nil
}
return valueField.Interface(), nil
}
checkTag := createCheckTag(typeField.Tag, fieldName)
if checkTag("json") || checkTag("graphql") {
return valueField.Interface(), nil
}
continue
}
return nil, nil
}
func createCheckTag(t reflect.StructTag, name string) func(string) bool {
return func(tagType string) bool {
tag := t.Get(tagType)
tOptions := strings.Split(tag, ",")
if len(tOptions) == 0 {
return false
}
if tOptions[0] != name {
return false
}
return true
}
}
type typeResolver interface {
ResolveType(interface{}, ResolveTypeParams) *Type
}
func newResolveTypeFn(typeMap graphql.TypeMap, impl interface{}) graphql.ResolveTypeFn {
resolver := impl.(typeResolver)
return func(p graphql.ResolveTypeParams) *graphql.Object {
typeRef := resolver.ResolveType(p.Value, p)
if typeRef == nil {
return nil
}
objType, ok := typeMap[typeRef.Name()]
if !ok {
return nil
}
return objType.(*graphql.Object)
}
}
type isTypeOfResolver interface {
IsTypeOf(interface{}, IsTypeOfParams) bool
}
func newIsTypeOfFn(impl interface{}) graphql.IsTypeOfFn {
resolver := impl.(isTypeOfResolver)
return func(p graphql.IsTypeOfParams) bool {
return resolver.IsTypeOf(p.Value, p)
}
}