-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtype_info.go
381 lines (347 loc) · 9.13 KB
/
type_info.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package introspection adds very basic remote GraphQL
// introspection funcionality
package introspection
import (
"bytes"
"fmt"
"net/http"
"github.com/graphql-go/graphql"
"github.com/slothking-online/gql/client"
)
// Arg is graphql field argument
type Arg struct {
// Name is a schema defined argument name
Name string `json:"name,omitempty"`
// Description is a schema defined argument description
Description string `json:"description,omitempty"`
// Type is a type or type reference defined by schema
Type Type `json:"type,omitempty"`
DefaultValue string `json:"defaultValue,omitempty"`
}
// GoString formats argument as it would apear in schema
func (a Arg) GoString() string {
return fmt.Sprintf("%s: %s", a.Name, a.Type.GoString())
}
// Field is graphql field
type Field struct {
// Args is a list of arguments for a field defined by schema
Args []Arg `json:"args,omitempty"`
// Name is a field name defined by schema
Name string `json:"name,omitempty"`
// Type is a type or type reference defined by schema
Type Type `json:"type,omitempty"`
// Description is a schema defined argument description
Description string `json:"description,omitempty"`
}
// ArgsString formats field arguments as they would apear in schema
func (f Field) ArgsString() string {
if len(f.Args) == 0 {
return ""
}
buf := &bytes.Buffer{}
sep := "("
for _, a := range f.Args {
fmt.Fprintf(buf, "%s%s", sep, a.GoString()) // nolint: errcheck
sep = ", "
}
fmt.Fprint(buf, ")") // nolint: errcheck
return buf.String()
}
// GoString formats field as it would apear in schema
func (f Field) GoString() string {
return fmt.Sprintf("%s%s: %s", f.Name, f.ArgsString(), f.Type.GoString())
}
// ArgNames is an array of arguments
// that this field accepts
func (f Field) ArgNames() []string {
if len(f.Args) == 0 {
return nil
}
args := make([]string, 0, len(f.Args))
for _, a := range f.Args {
args = append(args, a.Name)
}
return args
}
// Type is graphql type definition
type Type struct {
// Name is a type or type reference name
Name string `json:"name,omitempty"`
// Fields is a list of fields for this type, defined by schema
// Object and Interface only
Fields []Field `json:"fields,omitempty"`
// Kind is a type kind, refer to https://godoc.org/github.com/graphql-go/graphql#pkg-constants
// for valid list of type kinds
Kind string `json:"kind,omitempty"`
// Description is a schema defined argument description
Description string `json:"description,omitempty"`
// PossibleTypes is a list of possible types defined by schema
// only valid for Union and Interface kidns
PossibleTypes []Type `json:"possibleTypes,omitempty"`
// OfType is a type reference which this type wraps
// only valid for NonNull and List type kinds
OfType *Type `json:"ofType,omitempty"`
}
// Named returns true if type is named, as in, not NonNull or List
func (t Type) Named() bool {
return t.Valid() && (t.OfType == nil && !t.NonNull() && !t.List())
}
// Scalar returns true if type is of scalar type
func (t Type) Scalar() bool {
return t.Kind == graphql.TypeKindScalar
}
// Enum returns true if type is an Enum
func (t Type) Enum() bool {
return t.Kind == graphql.TypeKindEnum
}
// Input returns true if type is an Input
func (t Type) Input() bool {
return t.Kind == graphql.TypeKindInputObject
}
// Interface returns true if type is an Interface
func (t Type) Interface() bool {
return t.Kind == graphql.TypeKindInterface
}
// Object returns true if type is an Object
func (t Type) Object() bool {
return t.Kind == graphql.TypeKindObject
}
// Union returns true type is an Union
func (t Type) Union() bool {
return t.Kind == graphql.TypeKindUnion
}
// NonNull returns true if type is NonNull
func (t Type) NonNull() bool {
return t.Kind == graphql.TypeKindNonNull
}
// List returns true if type is a List
func (t Type) List() bool {
return t.Kind == graphql.TypeKindList
}
// Valid returns true if type is valid type
func (t Type) Valid() bool {
return t.Kind != ""
}
// GetOfTypeLeaf follows solves all NonNull/List types
// until it reaches named type
func (t Type) GetOfTypeLeaf() Type {
tt := t
for tt.OfType != nil {
tt = *tt.OfType
}
return tt
}
// TypeRef returns true if type is a type reference
func (t Type) TypeRef() bool {
// Inputs/Interfaces/Objects without fields, Unions without possible
// types and types without kind but with name are assumed to be a
// reference
return ((t.Input() || t.Interface() || t.Object()) && len(t.Fields) == 0) ||
(t.Union() && len(t.PossibleTypes) == 0) ||
(!t.Valid() && t.Name != "")
}
// Deref finds a type matching this type reference
// in a list of types
func (t Type) Deref(types []Type) Type {
for _, tt := range types {
if t.Name == tt.Name {
return tt
}
}
return Type{}
}
// TypeForPath seeks a type in schema matching resolve path.
// First element of path must match one of type fields
func (t Type) TypeForPath(schema Schema, path []string) (tt Type, ok bool) {
t = t.GetOfTypeLeaf()
if t.TypeRef() {
t = t.Deref(schema.Types)
}
// Exact match found
// return self
if len(path) == 0 {
tt = t
ok = true
return
}
for _, field := range t.Fields {
if field.Name == path[0] {
// One of the field matches requested path
// return it
ft, fok := field.Type.TypeForPath(schema, path[1:])
if fok {
tt = ft
ok = fok
return
}
}
}
// no match found
return
}
// GoString formats type to a string as it would appear
// in schema
func (t Type) GoString() string {
switch {
case t.NonNull():
return t.OfType.GoString() + "!"
case t.List():
return "[" + t.OfType.GoString() + "]"
default:
return t.Name
}
}
func ofTypeDepthFunc(depth int) string {
if depth == 0 {
return "ofType {kind name}"
}
depth--
return fmt.Sprintf("ofType {kind name %s }", ofTypeDepthFunc(depth))
}
var (
fragmentFullType = `fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}`
fragmentInputValue = `fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}`
fragmentTypeRef = `fragment TypeRef on __Type {
kind
name
` + ofTypeDepthFunc(7) + `
}`
introspectionQuery = `query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}`
typeInfoQuery = `
query op($typeName: String!){
__type(name: $typeName) { ...FullType }
}
` + fragmentFullType + "\n" + fragmentInputValue + "\n" + fragmentTypeRef
schemaInfoQuery = introspectionQuery + "\n" + fragmentFullType + "\n" + fragmentInputValue + "\n" + fragmentTypeRef
)
// GetTypeInfo fetches type information from remote endpoint
func GetTypeInfo(cli *client.Client, typeName string, header http.Header) (Type, error) {
r := client.Raw{
Query: typeInfoQuery,
Header: header,
Variables: map[string]interface{}{
"typeName": typeName,
},
}
out := struct {
Type Type `json:"__type,omitempty"`
}{}
if _, err := cli.Raw(r, &out); err != nil {
return Type{}, err
}
return out.Type, nil
}
// Schema is a representation of remote schema
// returned by schema introspection
type Schema struct {
// Types defined in schema
Types []Type `json:"types,omitempty"`
// QueryType is a reference to a type of query root operation
QueryType Type `json:"queryType"`
// MutationType is a reference to a type of mutation root operation
MutationType Type `json:"mutationType,omitempty"`
// SubscriptionType is a reference to a type of subscription root operation
SubscriptionType Type `json:"subscriptionType,omitempty"`
}
// TypeForPath finds a type to which path would resolve to
func (s Schema) TypeForPath(path []string) (t Type, ok bool) {
if len(path) == 0 {
return
}
switch path[0] {
case "query":
t = s.QueryType
case "mutation":
t = s.MutationType
case "subscription":
t = s.SubscriptionType
default:
return
}
return t.TypeForPath(s, path[1:])
}
// FieldForPath finds a field to which path would resolve to
func (s Schema) FieldForPath(path []string) (f Field, ok bool) {
if len(path) <= 1 {
return
}
t, ok := s.TypeForPath(path[:len(path)-1])
if !ok {
return
}
for _, field := range t.Fields {
if field.Name == path[len(path)-1] {
f = field
ok = true
break
}
}
return
}
// GetSchemaTypes runs introspection query on remote endpoint returning schema
func GetSchemaTypes(cli *client.Client, header http.Header) (Schema, error) {
r := client.Raw{
Query: schemaInfoQuery,
Header: header,
}
out := struct {
Schema Schema `json:"__schema"`
}{}
if _, err := cli.Raw(r, &out); err != nil {
return Schema{}, err
}
return out.Schema, nil
}