Skip to content

Commit 3e34b17

Browse files
committed
Make 'buildASTSchema' assign 'astNode' to types
1 parent 45edef0 commit 3e34b17

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/utilities/__tests__/buildASTSchema-test.js

+73-1
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 {
@@ -558,6 +558,78 @@ type Query {
558558
expect(rootFields.field2.isDeprecated).to.equal(true);
559559
expect(rootFields.field2.deprecationReason).to.equal('Because I said so');
560560
});
561+
562+
it('Correctly assign AST nodes', () => {
563+
const schema = buildSchema(`
564+
schema {
565+
query: Query
566+
}
567+
568+
type Query {
569+
testField(testArg: TestInput): TestUnion
570+
}
571+
572+
input TestInput {
573+
testInputField: TestEnum
574+
}
575+
576+
enum TestEnum {
577+
TEST_VALUE
578+
}
579+
580+
union TestUnion = TestType
581+
582+
interface TestInterface {
583+
interfaceField: String
584+
}
585+
586+
type TestType implements TestInterface {
587+
interfaceField: String
588+
}
589+
590+
directive @test(arg: Int) on FIELD
591+
`);
592+
const query = schema.getType('Query');
593+
const testInput = schema.getType('TestInput');
594+
const testEnum = schema.getType('TestEnum');
595+
const testUnion = schema.getType('TestUnion');
596+
const testInterface = schema.getType('TestInterface');
597+
const testType = schema.getType('TestType');
598+
const testDirective = schema.getDirective('test');
599+
600+
const restoredIDL = printSchema(buildSchema(
601+
print(schema.astNode) + '\n' +
602+
print(query.astNode) + '\n' +
603+
print(testInput.astNode) + '\n' +
604+
print(testEnum.astNode) + '\n' +
605+
print(testUnion.astNode) + '\n' +
606+
print(testInterface.astNode) + '\n' +
607+
print(testType.astNode) + '\n' +
608+
print(testDirective.astNode)
609+
));
610+
expect(restoredIDL).to.be.equal(printSchema(schema));
611+
612+
const testField = query.getFields().testField;
613+
expect(print(testField.astNode)).to.equal(
614+
'testField(testArg: TestInput): TestUnion'
615+
);
616+
expect(print(testField.args[0].astNode)).to.equal(
617+
'testArg: TestInput'
618+
);
619+
expect(print(testInput.getFields().testInputField.astNode)).to.equal(
620+
'testInputField: TestEnum'
621+
);
622+
expect(print(testEnum.getValue('TEST_VALUE').astNode)).to.equal(
623+
'TEST_VALUE'
624+
);
625+
expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
626+
'interfaceField: String'
627+
);
628+
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
629+
'interfaceField: String'
630+
);
631+
expect(print(testDirective.args[0].astNode)).to.equal('arg: Int');
632+
});
561633
});
562634

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

src/utilities/buildASTSchema.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
272272
null,
273273
types,
274274
directives,
275+
astNode: schemaDef,
275276
});
276277

277278
function getDirective(
@@ -284,6 +285,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
284285
node => ((node.value: any): DirectiveLocationEnum)
285286
),
286287
args: directiveNode.arguments && makeInputValues(directiveNode.arguments),
288+
astNode: directiveNode,
287289
});
288290
}
289291

@@ -368,6 +370,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
368370
description: getDescription(def),
369371
fields: () => makeFieldDefMap(def),
370372
interfaces: () => makeImplementedInterfaces(def),
373+
astNode: def,
371374
});
372375
}
373376

@@ -381,7 +384,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
381384
type: produceOutputType(field.type),
382385
description: getDescription(field),
383386
args: makeInputValues(field.arguments),
384-
deprecationReason: getDeprecationReason(field.directives)
387+
deprecationReason: getDeprecationReason(field.directives),
388+
astNode: field,
385389
})
386390
);
387391
}
@@ -400,7 +404,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
400404
return {
401405
type,
402406
description: getDescription(value),
403-
defaultValue: valueFromAST(value.defaultValue, type)
407+
defaultValue: valueFromAST(value.defaultValue, type),
408+
astNode: value,
404409
};
405410
}
406411
);
@@ -412,6 +417,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
412417
name: typeName,
413418
description: getDescription(def),
414419
fields: () => makeFieldDefMap(def),
420+
astNode: def,
415421
resolveType: cannotExecuteSchema,
416422
});
417423
}
@@ -425,9 +431,11 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
425431
enumValue => enumValue.name.value,
426432
enumValue => ({
427433
description: getDescription(enumValue),
428-
deprecationReason: getDeprecationReason(enumValue.directives)
434+
deprecationReason: getDeprecationReason(enumValue.directives),
435+
astNode: enumValue
429436
})
430437
),
438+
astNode: def,
431439
});
432440

433441
return enumType;
@@ -439,13 +447,15 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
439447
description: getDescription(def),
440448
types: def.types.map(t => produceObjectType(t)),
441449
resolveType: cannotExecuteSchema,
450+
astNode: def,
442451
});
443452
}
444453

445454
function makeScalarDef(def: ScalarTypeDefinitionNode) {
446455
return new GraphQLScalarType({
447456
name: def.name.value,
448457
description: getDescription(def),
458+
astNode: def,
449459
serialize: () => null,
450460
// Note: validation calls the parse functions to determine if a
451461
// literal value is correct. Returning null would cause use of custom
@@ -461,6 +471,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
461471
name: def.name.value,
462472
description: getDescription(def),
463473
fields: () => makeInputValues(def.fields),
474+
astNode: def,
464475
});
465476
}
466477
}

0 commit comments

Comments
 (0)