Skip to content

Commit 45edef0

Browse files
committed
Add 'astNode' & 'extensionASTNodes' fields to type definitions
1 parent 0f1ba1e commit 45edef0

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

src/type/__tests__/definition-test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ describe('Type System: Example', () => {
180180
description: undefined,
181181
isDeprecated: true,
182182
deprecationReason: 'Just because',
183-
value: 'foo'
183+
value: 'foo',
184+
astNode: null,
184185
});
185186
});
186187

@@ -200,13 +201,15 @@ describe('Type System: Example', () => {
200201
isDeprecated: false,
201202
deprecationReason: undefined,
202203
value: null,
204+
astNode: null,
203205
},
204206
{
205207
name: 'UNDEFINED',
206208
description: undefined,
207209
isDeprecated: false,
208210
deprecationReason: undefined,
209211
value: undefined,
212+
astNode: null,
210213
},
211214
]);
212215
});

src/type/definition.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import isNullish from '../jsutils/isNullish';
1313
import { ENUM } from '../language/kinds';
1414
import { assertValidName } from '../utilities/assertValidName';
1515
import type {
16+
ScalarTypeDefinitionNode,
17+
ObjectTypeDefinitionNode,
18+
FieldDefinitionNode,
19+
InputValueDefinitionNode,
20+
InterfaceTypeDefinitionNode,
21+
UnionTypeDefinitionNode,
22+
EnumTypeDefinitionNode,
23+
EnumValueDefinitionNode,
24+
InputObjectTypeDefinitionNode,
25+
TypeExtensionDefinitionNode,
1626
OperationDefinitionNode,
1727
FieldNode,
1828
FragmentDefinitionNode,
@@ -294,13 +304,15 @@ function resolveThunk<T>(thunk: Thunk<T>): T {
294304
export class GraphQLScalarType {
295305
name: string;
296306
description: ?string;
307+
astNode: ?ScalarTypeDefinitionNode;
297308

298309
_scalarConfig: GraphQLScalarTypeConfig<*, *>;
299310

300311
constructor(config: GraphQLScalarTypeConfig<*, *>): void {
301312
assertValidName(config.name);
302313
this.name = config.name;
303314
this.description = config.description;
315+
this.astNode = config.astNode || null;
304316
invariant(
305317
typeof config.serialize === 'function',
306318
`${this.name} must provide "serialize" function. If this custom Scalar ` +
@@ -364,6 +376,7 @@ GraphQLScalarType.prototype.toJSON =
364376
export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
365377
name: string;
366378
description?: ?string;
379+
astNode?: ?ScalarTypeDefinitionNode;
367380
serialize: (value: mixed) => ?TExternal;
368381
parseValue?: (value: mixed) => ?TInternal;
369382
parseLiteral?: (valueNode: ValueNode) => ?TInternal;
@@ -411,6 +424,8 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
411424
export class GraphQLObjectType {
412425
name: string;
413426
description: ?string;
427+
astNode: ?ObjectTypeDefinitionNode;
428+
extensionASTNodes: Array<TypeExtensionDefinitionNode>;
414429
isTypeOf: ?GraphQLIsTypeOfFn<*, *>;
415430

416431
_typeConfig: GraphQLObjectTypeConfig<*, *>;
@@ -421,6 +436,8 @@ export class GraphQLObjectType {
421436
assertValidName(config.name, config.isIntrospection);
422437
this.name = config.name;
423438
this.description = config.description;
439+
this.astNode = config.astNode || null;
440+
this.extensionASTNodes = config.extensionASTNodes || [];
424441
if (config.isTypeOf) {
425442
invariant(
426443
typeof config.isTypeOf === 'function',
@@ -562,7 +579,8 @@ function defineFieldMap<TSource, TContext>(
562579
name: argName,
563580
description: arg.description === undefined ? null : arg.description,
564581
type: arg.type,
565-
defaultValue: arg.defaultValue
582+
defaultValue: arg.defaultValue,
583+
astNode: arg.astNode,
566584
};
567585
});
568586
}
@@ -587,6 +605,8 @@ export type GraphQLObjectTypeConfig<TSource, TContext> = {
587605
isTypeOf?: ?GraphQLIsTypeOfFn<TSource, TContext>;
588606
description?: ?string;
589607
isIntrospection?: boolean;
608+
astNode?: ?ObjectTypeDefinitionNode;
609+
extensionASTNodes?: ?Array<TypeExtensionDefinitionNode>;
590610
};
591611

592612
export type GraphQLTypeResolver<TSource, TContext> = (
@@ -630,6 +650,7 @@ export type GraphQLFieldConfig<TSource, TContext> = {
630650
subscribe?: GraphQLFieldResolver<TSource, TContext>;
631651
deprecationReason?: ?string;
632652
description?: ?string;
653+
astNode?: ?FieldDefinitionNode;
633654
};
634655

635656
export type GraphQLFieldConfigArgumentMap = {
@@ -640,6 +661,7 @@ export type GraphQLArgumentConfig = {
640661
type: GraphQLInputType;
641662
defaultValue?: mixed;
642663
description?: ?string;
664+
astNode?: ?InputValueDefinitionNode;
643665
};
644666

645667
export type GraphQLFieldConfigMap<TSource, TContext> = {
@@ -655,13 +677,15 @@ export type GraphQLField<TSource, TContext> = {
655677
subscribe?: GraphQLFieldResolver<TSource, TContext>;
656678
isDeprecated?: boolean;
657679
deprecationReason?: ?string;
680+
astNode?: ?FieldDefinitionNode;
658681
};
659682

660683
export type GraphQLArgument = {
661684
name: string;
662685
type: GraphQLInputType;
663686
defaultValue?: mixed;
664687
description?: ?string;
688+
astNode?: ?InputValueDefinitionNode;
665689
};
666690

667691
export type GraphQLFieldMap<TSource, TContext> = {
@@ -691,6 +715,7 @@ export type GraphQLFieldMap<TSource, TContext> = {
691715
export class GraphQLInterfaceType {
692716
name: string;
693717
description: ?string;
718+
astNode: ?InterfaceTypeDefinitionNode;
694719
resolveType: ?GraphQLTypeResolver<*, *>;
695720

696721
_typeConfig: GraphQLInterfaceTypeConfig<*, *>;
@@ -700,6 +725,7 @@ export class GraphQLInterfaceType {
700725
assertValidName(config.name);
701726
this.name = config.name;
702727
this.description = config.description;
728+
this.astNode = config.astNode || null;
703729
if (config.resolveType) {
704730
invariant(
705731
typeof config.resolveType === 'function',
@@ -737,7 +763,8 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
737763
* Object type.
738764
*/
739765
resolveType?: ?GraphQLTypeResolver<TSource, TContext>,
740-
description?: ?string
766+
description?: ?string,
767+
astNode?: ?InterfaceTypeDefinitionNode,
741768
};
742769

743770

@@ -768,6 +795,7 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
768795
export class GraphQLUnionType {
769796
name: string;
770797
description: ?string;
798+
astNode: ?UnionTypeDefinitionNode;
771799
resolveType: ?GraphQLTypeResolver<*, *>;
772800

773801
_typeConfig: GraphQLUnionTypeConfig<*, *>;
@@ -778,6 +806,7 @@ export class GraphQLUnionType {
778806
assertValidName(config.name);
779807
this.name = config.name;
780808
this.description = config.description;
809+
this.astNode = config.astNode || null;
781810
if (config.resolveType) {
782811
invariant(
783812
typeof config.resolveType === 'function',
@@ -854,6 +883,7 @@ export type GraphQLUnionTypeConfig<TSource, TContext> = {
854883
*/
855884
resolveType?: ?GraphQLTypeResolver<TSource, TContext>;
856885
description?: ?string;
886+
astNode?: ?UnionTypeDefinitionNode;
857887
};
858888

859889

@@ -882,6 +912,7 @@ export type GraphQLUnionTypeConfig<TSource, TContext> = {
882912
export class GraphQLEnumType/* <T> */ {
883913
name: string;
884914
description: ?string;
915+
astNode: ?EnumTypeDefinitionNode;
885916

886917
_enumConfig: GraphQLEnumTypeConfig/* <T> */;
887918
_values: Array<GraphQLEnumValue/* <T> */>;
@@ -892,6 +923,7 @@ export class GraphQLEnumType/* <T> */ {
892923
this.name = config.name;
893924
assertValidName(config.name, config.isIntrospection);
894925
this.description = config.description;
926+
this.astNode = config.astNode || null;
895927
this._values = defineEnumValues(this, config.values);
896928
this._enumConfig = config;
897929
}
@@ -1008,6 +1040,7 @@ function defineEnumValues(
10081040
description: value.description,
10091041
isDeprecated: Boolean(value.deprecationReason),
10101042
deprecationReason: value.deprecationReason,
1043+
astNode: value.astNode || null,
10111044
value: value.hasOwnProperty('value') ? value.value : valueName,
10121045
};
10131046
});
@@ -1017,6 +1050,7 @@ export type GraphQLEnumTypeConfig/* <T> */ = {
10171050
name: string;
10181051
values: GraphQLEnumValueConfigMap/* <T> */;
10191052
description?: ?string;
1053+
astNode?: ?EnumTypeDefinitionNode;
10201054
isIntrospection?: boolean;
10211055
};
10221056

@@ -1028,13 +1062,15 @@ export type GraphQLEnumValueConfig/* <T> */ = {
10281062
value?: any/* T */;
10291063
deprecationReason?: ?string;
10301064
description?: ?string;
1065+
astNode?: ?EnumValueDefinitionNode;
10311066
};
10321067

10331068
export type GraphQLEnumValue/* <T> */ = {
10341069
name: string;
10351070
description: ?string;
10361071
isDeprecated?: boolean;
10371072
deprecationReason: ?string;
1073+
astNode?: ?EnumValueDefinitionNode;
10381074
value: any/* T */;
10391075
};
10401076

@@ -1063,6 +1099,7 @@ export type GraphQLEnumValue/* <T> */ = {
10631099
export class GraphQLInputObjectType {
10641100
name: string;
10651101
description: ?string;
1102+
astNode: ?InputObjectTypeDefinitionNode;
10661103

10671104
_typeConfig: GraphQLInputObjectTypeConfig;
10681105
_fields: GraphQLInputFieldMap;
@@ -1071,6 +1108,7 @@ export class GraphQLInputObjectType {
10711108
assertValidName(config.name);
10721109
this.name = config.name;
10731110
this.description = config.description;
1111+
this.astNode = config.astNode || null;
10741112
this._typeConfig = config;
10751113
}
10761114

@@ -1130,12 +1168,14 @@ export type GraphQLInputObjectTypeConfig = {
11301168
name: string;
11311169
fields: Thunk<GraphQLInputFieldConfigMap>;
11321170
description?: ?string;
1171+
astNode?: ?InputObjectTypeDefinitionNode;
11331172
};
11341173

11351174
export type GraphQLInputFieldConfig = {
11361175
type: GraphQLInputType;
11371176
defaultValue?: mixed;
11381177
description?: ?string;
1178+
astNode?: ?InputValueDefinitionNode;
11391179
};
11401180

11411181
export type GraphQLInputFieldConfigMap = {
@@ -1147,6 +1187,7 @@ export type GraphQLInputField = {
11471187
type: GraphQLInputType;
11481188
defaultValue?: mixed;
11491189
description?: ?string;
1190+
astNode?: ?InputValueDefinitionNode;
11501191
};
11511192

11521193
export type GraphQLInputFieldMap = {

src/type/directives.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
import { GraphQLString, GraphQLBoolean } from './scalars';
1717
import invariant from '../jsutils/invariant';
1818
import { assertValidName } from '../utilities/assertValidName';
19+
import type { DirectiveDefinitionNode } from '../language/ast';
1920

2021

2122
export const DirectiveLocation = {
@@ -52,6 +53,7 @@ export class GraphQLDirective {
5253
description: ?string;
5354
locations: Array<DirectiveLocationEnum>;
5455
args: Array<GraphQLArgument>;
56+
astNode: ?DirectiveDefinitionNode;
5557

5658
constructor(config: GraphQLDirectiveConfig): void {
5759
invariant(config.name, 'Directive must be named.');
@@ -84,10 +86,13 @@ export class GraphQLDirective {
8486
name: argName,
8587
description: arg.description === undefined ? null : arg.description,
8688
type: arg.type,
87-
defaultValue: arg.defaultValue
89+
defaultValue: arg.defaultValue,
90+
astNode: arg.astNode || null,
8891
};
8992
});
9093
}
94+
95+
this.astNode = config.astNode || null;
9196
}
9297
}
9398

@@ -96,6 +101,7 @@ type GraphQLDirectiveConfig = {
96101
description?: ?string;
97102
locations: Array<DirectiveLocationEnum>;
98103
args?: ?GraphQLFieldConfigArgumentMap;
104+
astNode?: ?DirectiveDefinitionNode;
99105
};
100106

101107
/**

src/type/schema.js

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
GraphQLNamedType,
2222
GraphQLAbstractType
2323
} from './definition';
24+
import type { SchemaDefinitionNode } from '../language/ast';
2425
import { GraphQLDirective, specifiedDirectives } from './directives';
2526
import { __Schema } from './introspection';
2627
import find from '../jsutils/find';
@@ -55,6 +56,7 @@ import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';
5556
*
5657
*/
5758
export class GraphQLSchema {
59+
astNode: ?SchemaDefinitionNode;
5860
_queryType: GraphQLObjectType;
5961
_mutationType: ?GraphQLObjectType;
6062
_subscriptionType: ?GraphQLObjectType;
@@ -107,6 +109,7 @@ export class GraphQLSchema {
107109
);
108110
// Provide specified directives (e.g. @include and @skip) by default.
109111
this._directives = config.directives || specifiedDirectives;
112+
this.astNode = config.astNode || null;
110113

111114
// Build type map now to detect any errors within this schema.
112115
let initialTypes: Array<?GraphQLNamedType> = [
@@ -227,6 +230,7 @@ type GraphQLSchemaConfig = {
227230
subscription?: ?GraphQLObjectType;
228231
types?: ?Array<GraphQLNamedType>;
229232
directives?: ?Array<GraphQLDirective>;
233+
astNode?: ?SchemaDefinitionNode;
230234
};
231235

232236
function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {

src/utilities/__tests__/buildClientSchema-test.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -404,27 +404,32 @@ describe('Type System: build schema from introspection', () => {
404404
value: 'VEGETABLES',
405405
description: 'Foods that are vegetables.',
406406
isDeprecated: false,
407-
deprecationReason: null, },
407+
deprecationReason: null,
408+
astNode: null, },
408409
{ name: 'FRUITS',
409410
value: 'FRUITS',
410411
description: 'Foods that are fruits.',
411412
isDeprecated: false,
412-
deprecationReason: null, },
413+
deprecationReason: null,
414+
astNode: null, },
413415
{ name: 'OILS',
414416
value: 'OILS',
415417
description: 'Foods that are oils.',
416418
isDeprecated: false,
417-
deprecationReason: null, },
419+
deprecationReason: null,
420+
astNode: null, },
418421
{ name: 'DAIRY',
419422
value: 'DAIRY',
420423
description: 'Foods that are dairy.',
421424
isDeprecated: false,
422-
deprecationReason: null, },
425+
deprecationReason: null,
426+
astNode: null, },
423427
{ name: 'MEAT',
424428
value: 'MEAT',
425429
description: 'Foods that are meat.',
426430
isDeprecated: false,
427-
deprecationReason: null, },
431+
deprecationReason: null,
432+
astNode: null, },
428433
]);
429434
});
430435

0 commit comments

Comments
 (0)