Skip to content

Commit 496ac0d

Browse files
committed
Make 'buildASTSchema' assign 'astNode' to types
1 parent 9c87fba commit 496ac0d

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { expect } from 'chai';
1111
import { describe, it } from 'mocha';
12-
import { parse } from '../../language';
12+
import { parse, print } from '../../language';
1313
import { printSchema } from '../schemaPrinter';
1414
import { buildASTSchema, buildSchema } from '../buildASTSchema';
1515
import dedent from '../../jsutils/dedent';
@@ -560,6 +560,78 @@ describe('Schema Builder', () => {
560560
expect(rootFields.field2.isDeprecated).to.equal(true);
561561
expect(rootFields.field2.deprecationReason).to.equal('Because I said so');
562562
});
563+
564+
it('Correctly assign AST nodes', () => {
565+
const schema = buildSchema(`
566+
schema {
567+
query: Query
568+
}
569+
570+
type Query {
571+
testField(testArg: TestInput): TestUnion
572+
}
573+
574+
input TestInput {
575+
testInputField: TestEnum
576+
}
577+
578+
enum TestEnum {
579+
TEST_VALUE
580+
}
581+
582+
union TestUnion = TestType
583+
584+
interface TestInterface {
585+
interfaceField: String
586+
}
587+
588+
type TestType implements TestInterface {
589+
interfaceField: String
590+
}
591+
592+
directive @test(arg: Int) on FIELD
593+
`);
594+
const query = schema.getType('Query');
595+
const testInput = schema.getType('TestInput');
596+
const testEnum = schema.getType('TestEnum');
597+
const testUnion = schema.getType('TestUnion');
598+
const testInterface = schema.getType('TestInterface');
599+
const testType = schema.getType('TestType');
600+
const testDirective = schema.getDirective('test');
601+
602+
const restoredIDL = printSchema(buildSchema(
603+
print(schema.astNode) + '\n' +
604+
print(query.astNode) + '\n' +
605+
print(testInput.astNode) + '\n' +
606+
print(testEnum.astNode) + '\n' +
607+
print(testUnion.astNode) + '\n' +
608+
print(testInterface.astNode) + '\n' +
609+
print(testType.astNode) + '\n' +
610+
print(testDirective.astNode)
611+
));
612+
expect(restoredIDL).to.be.equal(printSchema(schema));
613+
614+
const testField = query.getFields().testField;
615+
expect(print(testField.astNode)).to.equal(
616+
'testField(testArg: TestInput): TestUnion'
617+
);
618+
expect(print(testField.args[0].astNode)).to.equal(
619+
'testArg: TestInput'
620+
);
621+
expect(print(testInput.getFields().testInputField.astNode)).to.equal(
622+
'testInputField: TestEnum'
623+
);
624+
expect(print(testEnum.getValue('TEST_VALUE').astNode)).to.equal(
625+
'TEST_VALUE'
626+
);
627+
expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
628+
'interfaceField: String'
629+
);
630+
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
631+
'interfaceField: String'
632+
);
633+
expect(print(testDirective.args[0].astNode)).to.equal('arg: Int');
634+
});
563635
});
564636

565637
describe('Failures', () => {

src/utilities/buildASTSchema.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
263263
null,
264264
types,
265265
directives,
266+
astNode: schemaDef,
266267
});
267268

268269
function getDirective(
@@ -275,6 +276,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
275276
node => ((node.value: any): DirectiveLocationEnum)
276277
),
277278
args: directiveNode.arguments && makeInputValues(directiveNode.arguments),
279+
astNode: directiveNode,
278280
});
279281
}
280282

@@ -359,6 +361,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
359361
description: getDescription(def),
360362
fields: () => makeFieldDefMap(def),
361363
interfaces: () => makeImplementedInterfaces(def),
364+
astNode: def,
362365
});
363366
}
364367

@@ -372,7 +375,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
372375
type: produceOutputType(field.type),
373376
description: getDescription(field),
374377
args: makeInputValues(field.arguments),
375-
deprecationReason: getDeprecationReason(field)
378+
deprecationReason: getDeprecationReason(field),
379+
astNode: field,
376380
})
377381
);
378382
}
@@ -391,7 +395,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
391395
return {
392396
type,
393397
description: getDescription(value),
394-
defaultValue: valueFromAST(value.defaultValue, type)
398+
defaultValue: valueFromAST(value.defaultValue, type),
399+
astNode: value,
395400
};
396401
}
397402
);
@@ -403,6 +408,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
403408
name: typeName,
404409
description: getDescription(def),
405410
fields: () => makeFieldDefMap(def),
411+
astNode: def,
406412
resolveType: cannotExecuteSchema,
407413
});
408414
}
@@ -416,9 +422,11 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
416422
enumValue => enumValue.name.value,
417423
enumValue => ({
418424
description: getDescription(enumValue),
419-
deprecationReason: getDeprecationReason(enumValue)
425+
deprecationReason: getDeprecationReason(enumValue),
426+
astNode: enumValue,
420427
})
421428
),
429+
astNode: def,
422430
});
423431

424432
return enumType;
@@ -430,13 +438,15 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
430438
description: getDescription(def),
431439
types: def.types.map(t => produceObjectType(t)),
432440
resolveType: cannotExecuteSchema,
441+
astNode: def,
433442
});
434443
}
435444

436445
function makeScalarDef(def: ScalarTypeDefinitionNode) {
437446
return new GraphQLScalarType({
438447
name: def.name.value,
439448
description: getDescription(def),
449+
astNode: def,
440450
serialize: () => null,
441451
// Note: validation calls the parse functions to determine if a
442452
// literal value is correct. Returning null would cause use of custom
@@ -452,6 +462,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
452462
name: def.name.value,
453463
description: getDescription(def),
454464
fields: () => makeInputValues(def.fields),
465+
astNode: def,
455466
});
456467
}
457468
}

0 commit comments

Comments
 (0)