Skip to content

Commit a75020a

Browse files
committed
Make 'extendSchema' assign 'astNode' & 'extensionASTNodes'
1 parent 8665b98 commit a75020a

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

src/utilities/__tests__/extendSchema-test.js

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import { describe, it } from 'mocha';
1111
import { expect } from 'chai';
1212
import dedent from '../../jsutils/dedent';
13+
import { buildSchema } from '../buildASTSchema';
1314
import { extendSchema } from '../extendSchema';
1415
import { execute } from '../../execution';
15-
import { parse } from '../../language';
16+
import { parse, print } from '../../language';
1617
import { printSchema } from '../schemaPrinter';
1718
import {
1819
GraphQLSchema,
@@ -196,6 +197,96 @@ describe('extendSchema', () => {
196197
`);
197198
});
198199

200+
it('correctly assign AST nodes to new and extended types', () => {
201+
const schemaFromIDL = buildSchema(`
202+
type Query {
203+
dummyField: String
204+
}
205+
`);
206+
const extensionAst = parse(`
207+
extend type Query {
208+
newField(testArg: TestInput): TestEnum
209+
}
210+
211+
enum TestEnum {
212+
TEST_VALUE
213+
}
214+
215+
input TestInput {
216+
testInputField: TestEnum
217+
}
218+
`);
219+
const extendedSchema = extendSchema(schemaFromIDL, extensionAst);
220+
const secondExtensionAst = parse(`
221+
extend type Query {
222+
oneMoreNewField: TestUnion
223+
}
224+
225+
union TestUnion = TestType
226+
227+
interface TestInterface {
228+
interfaceField: String
229+
}
230+
231+
type TestType implements TestInterface {
232+
interfaceField: String
233+
}
234+
235+
directive @test(arg: Int) on FIELD
236+
`);
237+
const extendedTwiceSchema = extendSchema(extendedSchema,
238+
secondExtensionAst);
239+
240+
const query = extendedTwiceSchema.getType('Query');
241+
const testInput = extendedTwiceSchema.getType('TestInput');
242+
const testEnum = extendedTwiceSchema.getType('TestEnum');
243+
const testUnion = extendedTwiceSchema.getType('TestUnion');
244+
const testInterface = extendedTwiceSchema.getType('TestInterface');
245+
const testType = extendedTwiceSchema.getType('TestType');
246+
const testDirective = extendedTwiceSchema.getDirective('test');
247+
248+
expect(query.extensionASTNodes).to.have.lengthOf(2);
249+
expect(testType.extensionASTNodes).to.have.lengthOf(0);
250+
251+
const restoredExtensionAST = parse(
252+
print(query.extensionASTNodes[0]) + '\n' +
253+
print(query.extensionASTNodes[1]) + '\n' +
254+
print(testInput.astNode) + '\n' +
255+
print(testEnum.astNode) + '\n' +
256+
print(testUnion.astNode) + '\n' +
257+
print(testInterface.astNode) + '\n' +
258+
print(testType.astNode) + '\n' +
259+
print(testDirective.astNode)
260+
);
261+
expect(
262+
printSchema(extendSchema(schemaFromIDL, restoredExtensionAST))
263+
).to.be.equal(printSchema(extendedTwiceSchema));
264+
265+
const newField = query.getFields().newField;
266+
expect(print(newField.astNode)).to.equal(
267+
'newField(testArg: TestInput): TestEnum'
268+
);
269+
expect(print(newField.args[0].astNode)).to.equal(
270+
'testArg: TestInput'
271+
);
272+
expect(print(query.getFields().oneMoreNewField.astNode)).to.equal(
273+
'oneMoreNewField: TestUnion'
274+
);
275+
expect(print(testInput.getFields().testInputField.astNode)).to.equal(
276+
'testInputField: TestEnum'
277+
);
278+
expect(print(testEnum.getValue('TEST_VALUE').astNode)).to.equal(
279+
'TEST_VALUE'
280+
);
281+
expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
282+
'interfaceField: String'
283+
);
284+
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
285+
'interfaceField: String'
286+
);
287+
expect(print(testDirective.args[0].astNode)).to.equal('arg: Int');
288+
});
289+
199290
it('builds types with deprecated fields/values', () => {
200291
const ast = parse(`
201292
type TypeWithDeprecatedField {

src/utilities/extendSchema.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export function extendSchema(
240240
subscription: subscriptionType,
241241
types,
242242
directives: getMergedDirectives(),
243+
astNode: schema.astNode,
243244
});
244245

245246
// Below are functions used for producing this schema that have closed over
@@ -332,11 +333,19 @@ export function extendSchema(
332333
}
333334

334335
function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
336+
const name = type.name;
337+
let extensionASTNodes = type.extensionASTNodes;
338+
if (typeExtensionsMap[name]) {
339+
extensionASTNodes = extensionASTNodes.concat(typeExtensionsMap[name]);
340+
}
341+
335342
return new GraphQLObjectType({
336-
name: type.name,
343+
name,
337344
description: type.description,
338345
interfaces: () => extendImplementedInterfaces(type),
339346
fields: () => extendFieldMap(type),
347+
astNode: type.astNode,
348+
extensionASTNodes,
340349
isTypeOf: type.isTypeOf,
341350
});
342351
}
@@ -348,6 +357,7 @@ export function extendSchema(
348357
name: type.name,
349358
description: type.description,
350359
fields: () => extendFieldMap(type),
360+
astNode: type.astNode,
351361
resolveType: type.resolveType,
352362
});
353363
}
@@ -357,6 +367,7 @@ export function extendSchema(
357367
name: type.name,
358368
description: type.description,
359369
types: type.getTypes().map(getTypeFromDef),
370+
astNode: type.astNode,
360371
resolveType: type.resolveType,
361372
});
362373
}
@@ -397,6 +408,7 @@ export function extendSchema(
397408
deprecationReason: field.deprecationReason,
398409
type: extendFieldType(field.type),
399410
args: keyMap(field.args, arg => arg.name),
411+
astNode: field.astNode,
400412
resolve: field.resolve,
401413
};
402414
});
@@ -419,6 +431,7 @@ export function extendSchema(
419431
type: buildOutputFieldType(field.type),
420432
args: buildInputValues(field.arguments),
421433
deprecationReason: getDeprecationReason(field),
434+
astNode: field,
422435
};
423436
});
424437
});
@@ -456,6 +469,7 @@ export function extendSchema(
456469
description: getDescription(typeNode),
457470
interfaces: () => buildImplementedInterfaces(typeNode),
458471
fields: () => buildFieldMap(typeNode),
472+
astNode: typeNode,
459473
});
460474
}
461475

@@ -464,6 +478,7 @@ export function extendSchema(
464478
name: typeNode.name.value,
465479
description: getDescription(typeNode),
466480
fields: () => buildFieldMap(typeNode),
481+
astNode: typeNode,
467482
resolveType: cannotExecuteExtendedSchema,
468483
});
469484
}
@@ -473,6 +488,7 @@ export function extendSchema(
473488
name: typeNode.name.value,
474489
description: getDescription(typeNode),
475490
types: typeNode.types.map(getObjectTypeFromAST),
491+
astNode: typeNode,
476492
resolveType: cannotExecuteExtendedSchema,
477493
});
478494
}
@@ -481,6 +497,7 @@ export function extendSchema(
481497
return new GraphQLScalarType({
482498
name: typeNode.name.value,
483499
description: getDescription(typeNode),
500+
astNode: typeNode,
484501
serialize: id => id,
485502
// Note: validation calls the parse functions to determine if a
486503
// literal value is correct. Returning null would cause use of custom
@@ -501,8 +518,10 @@ export function extendSchema(
501518
enumValue => ({
502519
description: getDescription(enumValue),
503520
deprecationReason: getDeprecationReason(enumValue),
521+
astNode: enumValue,
504522
}),
505523
),
524+
astNode: typeNode,
506525
});
507526
}
508527

@@ -511,6 +530,7 @@ export function extendSchema(
511530
name: typeNode.name.value,
512531
description: getDescription(typeNode),
513532
fields: () => buildInputValues(typeNode.fields),
533+
astNode: typeNode,
514534
});
515535
}
516536

@@ -524,6 +544,7 @@ export function extendSchema(
524544
),
525545
args:
526546
directiveNode.arguments && buildInputValues(directiveNode.arguments),
547+
astNode: directiveNode,
527548
});
528549
}
529550

@@ -541,6 +562,7 @@ export function extendSchema(
541562
description: getDescription(field),
542563
args: buildInputValues(field.arguments),
543564
deprecationReason: getDeprecationReason(field),
565+
astNode: field,
544566
})
545567
);
546568
}
@@ -554,7 +576,8 @@ export function extendSchema(
554576
return {
555577
type,
556578
description: getDescription(value),
557-
defaultValue: valueFromAST(value.defaultValue, type)
579+
defaultValue: valueFromAST(value.defaultValue, type),
580+
astNode: value,
558581
};
559582
}
560583
);

0 commit comments

Comments
 (0)