-
Notifications
You must be signed in to change notification settings - Fork 47
/
introspection.ex
427 lines (409 loc) · 13.2 KB
/
introspection.ex
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
defmodule GraphQL.Type.Introspection do
alias GraphQL.Type.ObjectType
alias GraphQL.Type.List
alias GraphQL.Type.NonNull
alias GraphQL.Type.String
alias GraphQL.Type.Boolean
def schema do
%ObjectType{
name: "__Schema",
description:
"""
A GraphQL Schema defines the capabilities of a GraphQL server. It
exposes all available types and directives on the server, as well as
the entry points for query, mutation, and subscription operations.
""",
fields: quote do %{
types: %{
description: "A list of all types supported by this server.",
type: %NonNull{ofType: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.type}}},
resolve: fn(schema, _, _) ->
Map.values(GraphQL.Schema.reduce_types(schema))
end
},
queryType: %{
description: "The type that query operations will be rooted at.",
type: %NonNull{ofType: GraphQL.Type.Introspection.type},
resolve: fn(%{query: query}, _, _) -> query end
},
mutationType: %{
description: "If this server supports mutation, the type that mutation operations will be rooted at.",
type: GraphQL.Type.Introspection.type,
resolve: nil #fn(%{mutation: mutation}, _, _) -> mutation end
},
subscriptionType: %{
description: "If this server support subscription, the type that subscription operations will be rooted at.",
type: GraphQL.Type.Introspection.type,
resolve: nil #fn(%{subscription: subscription}, _, _) -> subscription end
},
directives: %{
description: "A list of all directives supported by this server.",
type: %NonNull{ofType: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.directive}}},
resolve: nil #schema => schema.getDirectives(),
}
} end
}
end
def directive do
%ObjectType{
name: "__Directive",
description:
"""
A Directive provides a way to describe alternate runtime execution and
type validation behavior in a GraphQL document.
In some cases, you need to provide options to alter GraphQL’s
execution behavior in ways field arguments will not suffice, such as
conditionally including or skipping a field. Directives provide this by
describing additional information to the executor
""",
fields: %{
name: %{type: %NonNull{ofType: %String{}}},
description: %{type: %String{}},
args: %{
type: %NonNull{ofType: %List{ofType: %NonNull{ofType: input_value}}},
resolve: nil #directive => directive.args || []
},
onOperation: %{type: %NonNull{ofType: %Boolean{}}},
onFragment: %{type: %NonNull{ofType: %Boolean{}}},
onField: %{type: %NonNull{ofType: %Boolean{}}},
}
}
end
def type do
%ObjectType{
name: "__Type",
description:
"""
The fundamental unit of any GraphQL Schema is the type. There are
many kinds of types in GraphQL as represented by the `__TypeKind` enum.
Depending on the kind of a type, certain fields describe
information about that type. Scalar types provide no information
beyond a name and description, while Enum types provide their values.
Object and Interface types provide the fields they describe. Abstract
types, Union and Interface, provide the Object types possible
at runtime. List and NonNull types compose other types.
""",
fields: quote do %{
kind: %{
type: %NonNull{ofType: GraphQL.Type.Introspection.typekind}, # type_kind
resolve: fn(schema, _, _) ->
case schema do
%GraphQL.Type.ScalarType{} -> "SCALAR"
%GraphQL.Type.ObjectType{} -> "OBJECT"
%GraphQL.Type.Interface{} -> "INTERFACE"
#%GraphQL.Type.Union{} -> "UNION"
%GraphQL.Type.Enum{} -> "ENUM"
#%GraphQL.Type.Input{} -> "INPUT_OBJECT"
%GraphQL.Type.List{} -> "LIST"
%GraphQL.Type.NonNull{} -> "NON_NULL"
# since we can't subclass, maybe we can just check
# if the thing is a map and assume it's a scalar by
# default. otherwise we need checks for int/float/boolean
# etc etc etc any any custom types. We also sort of need
# some sort of injection for custom types :-\
# maybe attaching it to the type's module?
_ -> "SCALAR"
end
end
},
name: %{type: %String{}},
description: %{type: %String{}},
fields: %{
type: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.field}},
args: %{includeDeprecated: %{type: %Boolean{}, defaultValue: false}},
resolve: fn
(%GraphQL.Type.ObjectType{}=schema, args, rest) ->
thunk_fields = GraphQL.Execution.Executor.maybe_unwrap(schema.fields)
Enum.map(thunk_fields, fn({n, v}) -> Map.put(v, :name, n) end)
(_,_,_) -> []
# |> filter_deprecated
end
# resolve(type, { includeDeprecated }) {
# if (type instanceof GraphQLObjectType ||
# type instanceof GraphQLInterfaceType) {
# var fieldMap = type.getFields();
# var fields =
# Object.keys(fieldMap).map(fieldName => fieldMap[fieldName]);
# if (!includeDeprecated) {
# fields = fields.filter(field => !field.deprecationReason);
# }
# return fields;
# }
# return null;
# }
},
interfaces: %{
type: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.type}},
resolve: fn
(%GraphQL.Type.ObjectType{}=schema, args, rest) ->
schema.interfaces
(_, _, _) -> nil
end
},
possibleTypes: %{
type: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.type}},
resolve: fn
(%GraphQL.Type.Interface{name: name}, args, info) ->
GraphQL.Schema.reduce_types(info.schema)
|> Enum.filter(fn({_,t}) ->
Map.get(t, :interfaces, [])
|> Enum.filter(&(&1.name === name))
!== []
end) |> Enum.map(fn({k,v}) -> v end)
(_, _, _) -> nil
end
# resolve(type) {
# if (type instanceof GraphQLInterfaceType ||
# type instanceof GraphQLUnionType) {
# return type.getPossibleTypes();
# }
# }
},
enumValues: %{
type: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.enum_value}},
args: %{includeDeprecated: %{type: %Boolean{}, defaultValue: false}},
resolve: fn
(%GraphQL.Type.Enum{}=schema, _, _) -> schema.values
(_,_,_) -> nil
end
# resolve(type, { includeDeprecated }) {
# if (type instanceof GraphQLEnumType) {
# var values = type.getValues();
# if (!includeDeprecated) {
# values = values.filter(value => !value.deprecationReason);
# }
# return values;
# }
# }
},
inputFields: %{
type: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.input_value}},
resolve: nil
# resolve(type) {
# if (type instanceof GraphQLInputObjectType) {
# var fieldMap = type.getFields();
# return Object.keys(fieldMap).map(fieldName => fieldMap[fieldName]);
# }
# }
},
ofType: %{type: GraphQL.Type.Introspection.type}
} end
}
end
def typekind do
%{
name: "__TypeKind",
description: "An enum describing what kind of type a given `__Type` is.",
values: %{
SCALAR: %{
value: "SCALAR",
description: "Indicates this type is a scalar."
},
OBJECT: %{
value: "OBJECT"
},
INTERFACE: %{
value: "INTERFACE"
},
UNION: %{
value: "UNION"
},
ENUM: %{
value: "ENUM"
},
INPUT_OBJECT: %{
value: "INPUT_OBJECT"
},
LIST: %{
value: "LIST"
},
NON_NULL: %{
value: "NON_NULL"
},
NOT_FOUND: %{
value: "NOT_FOUND"
}
}
} |> GraphQL.Type.Enum.new
end
def field do
%ObjectType{
name: "__Field",
description:
"""
Object and Interface types are described by a list of Fields, each of
which has a name, potentially a list of arguments, and a return type.
""",
fields: %{
name: %{type: %NonNull{ofType: %String{}}},
description: %{type: %String{}},
args: %{
type: %NonNull{ofType: %List{ofType: %NonNull{ofType: GraphQL.Type.Introspection.input_value}}},
resolve: fn
(%{args: _args} = schema, _, _) -> Enum.map(schema.args, fn({name,v}) -> Map.put(v, :name, name) end)
(_, _, _) -> []
end
},
type: %{type: %NonNull{ofType: GraphQL.Type.Introspection.type}},
isDeprecated: %{
type: %NonNull{ofType: %Boolean{}}
# resolve: field => !isNullish(field.deprecationReason),
},
deprecationReason: %{type: %String{}}
}
}
end
def input_value do
%ObjectType{
name: "__InputValue",
description:
"""
Arguments provided to Fields or Directives and the input fields of an
InputObject are represented as Input Values which describe their type
and optionally a default value.
""",
fields: %{
name: %{type: %NonNull{ofType: %String{}}},
description: %{type: %String{}},
type: %{type: %NonNull{ofType: GraphQL.Type.Introspection.type}},
defaultValue: %{
type: %String{},
description: "A GraphQL-formatted string representing the default value for this input value."
# resolve: inputVal => isNullish(inputVal.defaultValue) ?
# null :
# print(astFromValue(inputVal.defaultValue, inputVal))
}
}
}
end
def enum_value do
%ObjectType{
name: "__EnumValue",
description:
"""
One possible value for a given Enum. Enum values are unique values, not
a placeholder for a string or numeric value. However an Enum value is
returned in a JSON response as a string.
""",
fields: %{
name: %{type: %NonNull{ofType: %String{}}},
description: %{type: %String{}},
isDeprecated: %{
type: %NonNull{ofType: %Boolean{}}
# resolve: enumValue => !isNullish(enumValue.deprecationReason),
},
deprecationReason: %{type: %String{}}
}
}
end
def query do
"""
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
args {
...InputValue
}
onOperation
onFragment
onField
}
}
}
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
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
"""
end
def meta("type") do
%{
name: "__type",
type: GraphQL.Type.Introspection.type,
description: "Request the type information of a single type.",
args:
%{
name: %{type: %NonNull{ofType: %String{}}}
},
resolve: fn(_, %{name: name}, %{schema: schema}) ->
GraphQL.Schema.reduce_types(schema)[name]
end
}
end
def meta("typename") do
%{
name: "__typename",
type: %NonNull{ofType: %String{}},
description: "The name of the current Object type at runtime.",
args: [],
resolve: fn(_, _, %{parent_type: %{name: name}}) -> name end
}
end
def meta("schema") do
%{
name: "__schema",
type: %NonNull{ofType: GraphQL.Type.Introspection.schema},
description: "Access the current type schema of this server.",
args: [],
resolve: fn(_, _, args) -> args.schema end
}
end
end