-
Notifications
You must be signed in to change notification settings - Fork 2k
/
introspection.js
478 lines (462 loc) · 15.1 KB
/
introspection.js
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import isInvalid from '../jsutils/isInvalid';
import objectValues from '../jsutils/objectValues';
import { astFromValue } from '../utilities/astFromValue';
import { print } from '../language/printer';
import {
GraphQLObjectType,
GraphQLEnumType,
isScalarType,
isObjectType,
isInterfaceType,
isUnionType,
isEnumType,
isInputObjectType,
isListType,
isNonNullType,
isAbstractType,
isNamedType,
} from './definition';
import { GraphQLList, GraphQLNonNull } from '../type/wrappers';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { DirectiveLocation } from '../language/directiveLocation';
import type { GraphQLField } from './definition';
export const __Schema = new GraphQLObjectType({
name: '__Schema',
isIntrospection: true,
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: () => ({
types: {
description: 'A list of all types supported by this server.',
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))),
resolve(schema) {
return objectValues(schema.getTypeMap());
},
},
queryType: {
description: 'The type that query operations will be rooted at.',
type: GraphQLNonNull(__Type),
resolve: schema => schema.getQueryType(),
},
mutationType: {
description:
'If this server supports mutation, the type that ' +
'mutation operations will be rooted at.',
type: __Type,
resolve: schema => schema.getMutationType(),
},
subscriptionType: {
description:
'If this server support subscription, the type that ' +
'subscription operations will be rooted at.',
type: __Type,
resolve: schema => schema.getSubscriptionType(),
},
directives: {
description: 'A list of all directives supported by this server.',
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))),
resolve: schema => schema.getDirectives(),
},
}),
});
export const __Directive = new GraphQLObjectType({
name: '__Directive',
isIntrospection: true,
description:
'A Directive provides a way to describe alternate runtime execution and ' +
'type validation behavior in a GraphQL document.' +
"\n\nIn 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: GraphQLNonNull(GraphQLString) },
description: { type: GraphQLString },
locations: {
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))),
},
args: {
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
resolve: directive => directive.args || [],
},
// NOTE: the following three fields are deprecated and are no longer part
// of the GraphQL specification.
onOperation: {
deprecationReason: 'Use `locations`.',
type: GraphQLNonNull(GraphQLBoolean),
resolve: d =>
d.locations.indexOf(DirectiveLocation.QUERY) !== -1 ||
d.locations.indexOf(DirectiveLocation.MUTATION) !== -1 ||
d.locations.indexOf(DirectiveLocation.SUBSCRIPTION) !== -1,
},
onFragment: {
deprecationReason: 'Use `locations`.',
type: GraphQLNonNull(GraphQLBoolean),
resolve: d =>
d.locations.indexOf(DirectiveLocation.FRAGMENT_SPREAD) !== -1 ||
d.locations.indexOf(DirectiveLocation.INLINE_FRAGMENT) !== -1 ||
d.locations.indexOf(DirectiveLocation.FRAGMENT_DEFINITION) !== -1,
},
onField: {
deprecationReason: 'Use `locations`.',
type: GraphQLNonNull(GraphQLBoolean),
resolve: d => d.locations.indexOf(DirectiveLocation.FIELD) !== -1,
},
}),
});
export const __DirectiveLocation = new GraphQLEnumType({
name: '__DirectiveLocation',
isIntrospection: true,
description:
'A Directive can be adjacent to many parts of the GraphQL language, a ' +
'__DirectiveLocation describes one such possible adjacencies.',
values: {
QUERY: {
value: DirectiveLocation.QUERY,
description: 'Location adjacent to a query operation.',
},
MUTATION: {
value: DirectiveLocation.MUTATION,
description: 'Location adjacent to a mutation operation.',
},
SUBSCRIPTION: {
value: DirectiveLocation.SUBSCRIPTION,
description: 'Location adjacent to a subscription operation.',
},
FIELD: {
value: DirectiveLocation.FIELD,
description: 'Location adjacent to a field.',
},
FRAGMENT_DEFINITION: {
value: DirectiveLocation.FRAGMENT_DEFINITION,
description: 'Location adjacent to a fragment definition.',
},
FRAGMENT_SPREAD: {
value: DirectiveLocation.FRAGMENT_SPREAD,
description: 'Location adjacent to a fragment spread.',
},
INLINE_FRAGMENT: {
value: DirectiveLocation.INLINE_FRAGMENT,
description: 'Location adjacent to an inline fragment.',
},
SCHEMA: {
value: DirectiveLocation.SCHEMA,
description: 'Location adjacent to a schema definition.',
},
SCALAR: {
value: DirectiveLocation.SCALAR,
description: 'Location adjacent to a scalar definition.',
},
OBJECT: {
value: DirectiveLocation.OBJECT,
description: 'Location adjacent to an object type definition.',
},
FIELD_DEFINITION: {
value: DirectiveLocation.FIELD_DEFINITION,
description: 'Location adjacent to a field definition.',
},
ARGUMENT_DEFINITION: {
value: DirectiveLocation.ARGUMENT_DEFINITION,
description: 'Location adjacent to an argument definition.',
},
INTERFACE: {
value: DirectiveLocation.INTERFACE,
description: 'Location adjacent to an interface definition.',
},
UNION: {
value: DirectiveLocation.UNION,
description: 'Location adjacent to a union definition.',
},
ENUM: {
value: DirectiveLocation.ENUM,
description: 'Location adjacent to an enum definition.',
},
ENUM_VALUE: {
value: DirectiveLocation.ENUM_VALUE,
description: 'Location adjacent to an enum value definition.',
},
INPUT_OBJECT: {
value: DirectiveLocation.INPUT_OBJECT,
description: 'Location adjacent to an input object type definition.',
},
INPUT_FIELD_DEFINITION: {
value: DirectiveLocation.INPUT_FIELD_DEFINITION,
description: 'Location adjacent to an input object field definition.',
},
},
});
export const __Type = new GraphQLObjectType({
name: '__Type',
isIntrospection: true,
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.' +
'\n\nDepending 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: () => ({
kind: {
type: GraphQLNonNull(__TypeKind),
resolve(type) {
if (isScalarType(type)) {
return TypeKind.SCALAR;
} else if (isObjectType(type)) {
return TypeKind.OBJECT;
} else if (isInterfaceType(type)) {
return TypeKind.INTERFACE;
} else if (isUnionType(type)) {
return TypeKind.UNION;
} else if (isEnumType(type)) {
return TypeKind.ENUM;
} else if (isInputObjectType(type)) {
return TypeKind.INPUT_OBJECT;
} else if (isListType(type)) {
return TypeKind.LIST;
} else if (isNonNullType(type)) {
return TypeKind.NON_NULL;
}
throw new Error('Unknown kind of type: ' + type);
},
},
name: { type: GraphQLString },
description: { type: GraphQLString },
fields: {
type: GraphQLList(GraphQLNonNull(__Field)),
args: {
includeDeprecated: { type: GraphQLBoolean, defaultValue: false },
},
resolve(type, { includeDeprecated }) {
if (isObjectType(type) || isInterfaceType(type)) {
let fields = objectValues(type.getFields());
if (!includeDeprecated) {
fields = fields.filter(field => !field.deprecationReason);
}
return fields;
}
return null;
},
},
interfaces: {
type: GraphQLList(GraphQLNonNull(__Type)),
resolve(type) {
if (isObjectType(type)) {
return type.getInterfaces();
}
},
},
possibleTypes: {
type: GraphQLList(GraphQLNonNull(__Type)),
resolve(type, args, context, { schema }) {
if (isAbstractType(type)) {
return schema.getPossibleTypes(type);
}
},
},
enumValues: {
type: GraphQLList(GraphQLNonNull(__EnumValue)),
args: {
includeDeprecated: { type: GraphQLBoolean, defaultValue: false },
},
resolve(type, { includeDeprecated }) {
if (isEnumType(type)) {
let values = type.getValues();
if (!includeDeprecated) {
values = values.filter(value => !value.deprecationReason);
}
return values;
}
},
},
inputFields: {
type: GraphQLList(GraphQLNonNull(__InputValue)),
resolve(type) {
if (isInputObjectType(type)) {
return objectValues(type.getFields());
}
},
},
ofType: { type: __Type },
}),
});
export const __Field = new GraphQLObjectType({
name: '__Field',
isIntrospection: true,
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: GraphQLNonNull(GraphQLString) },
description: { type: GraphQLString },
args: {
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
resolve: field => field.args || [],
},
type: { type: GraphQLNonNull(__Type) },
isDeprecated: { type: GraphQLNonNull(GraphQLBoolean) },
deprecationReason: {
type: GraphQLString,
},
}),
});
export const __InputValue = new GraphQLObjectType({
name: '__InputValue',
isIntrospection: true,
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: GraphQLNonNull(GraphQLString) },
description: { type: GraphQLString },
type: { type: GraphQLNonNull(__Type) },
defaultValue: {
type: GraphQLString,
description:
'A GraphQL-formatted string representing the default value for this ' +
'input value.',
resolve: inputVal =>
isInvalid(inputVal.defaultValue)
? null
: print(astFromValue(inputVal.defaultValue, inputVal.type)),
},
}),
});
export const __EnumValue = new GraphQLObjectType({
name: '__EnumValue',
isIntrospection: true,
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: GraphQLNonNull(GraphQLString) },
description: { type: GraphQLString },
isDeprecated: { type: GraphQLNonNull(GraphQLBoolean) },
deprecationReason: {
type: GraphQLString,
},
}),
});
export const TypeKind = {
SCALAR: 'SCALAR',
OBJECT: 'OBJECT',
INTERFACE: 'INTERFACE',
UNION: 'UNION',
ENUM: 'ENUM',
INPUT_OBJECT: 'INPUT_OBJECT',
LIST: 'LIST',
NON_NULL: 'NON_NULL',
};
export const __TypeKind = new GraphQLEnumType({
name: '__TypeKind',
isIntrospection: true,
description: 'An enum describing what kind of type a given `__Type` is.',
values: {
SCALAR: {
value: TypeKind.SCALAR,
description: 'Indicates this type is a scalar.',
},
OBJECT: {
value: TypeKind.OBJECT,
description:
'Indicates this type is an object. ' +
'`fields` and `interfaces` are valid fields.',
},
INTERFACE: {
value: TypeKind.INTERFACE,
description:
'Indicates this type is an interface. ' +
'`fields` and `possibleTypes` are valid fields.',
},
UNION: {
value: TypeKind.UNION,
description:
'Indicates this type is a union. ' +
'`possibleTypes` is a valid field.',
},
ENUM: {
value: TypeKind.ENUM,
description:
'Indicates this type is an enum. ' + '`enumValues` is a valid field.',
},
INPUT_OBJECT: {
value: TypeKind.INPUT_OBJECT,
description:
'Indicates this type is an input object. ' +
'`inputFields` is a valid field.',
},
LIST: {
value: TypeKind.LIST,
description:
'Indicates this type is a list. ' + '`ofType` is a valid field.',
},
NON_NULL: {
value: TypeKind.NON_NULL,
description:
'Indicates this type is a non-null. ' + '`ofType` is a valid field.',
},
},
});
/**
* Note that these are GraphQLField and not GraphQLFieldConfig,
* so the format for args is different.
*/
export const SchemaMetaFieldDef: GraphQLField<*, *> = {
name: '__schema',
type: GraphQLNonNull(__Schema),
description: 'Access the current type schema of this server.',
args: [],
resolve: (source, args, context, { schema }) => schema,
};
export const TypeMetaFieldDef: GraphQLField<*, *> = {
name: '__type',
type: __Type,
description: 'Request the type information of a single type.',
args: [{ name: 'name', type: GraphQLNonNull(GraphQLString) }],
resolve: (source, { name }, context, { schema }) => schema.getType(name),
};
export const TypeNameMetaFieldDef: GraphQLField<*, *> = {
name: '__typename',
type: GraphQLNonNull(GraphQLString),
description: 'The name of the current Object type at runtime.',
args: [],
resolve: (source, args, context, { parentType }) => parentType.name,
};
export const introspectionTypes: $ReadOnlyArray<*> = [
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,
];
export function isIntrospectionType(type: mixed): boolean %checks {
return (
isNamedType(type) &&
// Would prefer to use introspectionTypes.some(), however %checks needs
// a simple expression.
(type.name === __Schema.name ||
type.name === __Directive.name ||
type.name === __DirectiveLocation.name ||
type.name === __Type.name ||
type.name === __Field.name ||
type.name === __InputValue.name ||
type.name === __EnumValue.name ||
type.name === __TypeKind.name)
);
}