Skip to content

Commit

Permalink
Make 'buildASTSchema' assign 'astNode' to types
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 21, 2017
1 parent 9c87fba commit 496ac0d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
74 changes: 73 additions & 1 deletion src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language';
import { parse, print } from '../../language';
import { printSchema } from '../schemaPrinter';
import { buildASTSchema, buildSchema } from '../buildASTSchema';
import dedent from '../../jsutils/dedent';
Expand Down Expand Up @@ -560,6 +560,78 @@ describe('Schema Builder', () => {
expect(rootFields.field2.isDeprecated).to.equal(true);
expect(rootFields.field2.deprecationReason).to.equal('Because I said so');
});

it('Correctly assign AST nodes', () => {
const schema = buildSchema(`
schema {
query: Query
}
type Query {
testField(testArg: TestInput): TestUnion
}
input TestInput {
testInputField: TestEnum
}
enum TestEnum {
TEST_VALUE
}
union TestUnion = TestType
interface TestInterface {
interfaceField: String
}
type TestType implements TestInterface {
interfaceField: String
}
directive @test(arg: Int) on FIELD
`);
const query = schema.getType('Query');
const testInput = schema.getType('TestInput');
const testEnum = schema.getType('TestEnum');
const testUnion = schema.getType('TestUnion');
const testInterface = schema.getType('TestInterface');
const testType = schema.getType('TestType');
const testDirective = schema.getDirective('test');

const restoredIDL = printSchema(buildSchema(
print(schema.astNode) + '\n' +
print(query.astNode) + '\n' +
print(testInput.astNode) + '\n' +
print(testEnum.astNode) + '\n' +
print(testUnion.astNode) + '\n' +
print(testInterface.astNode) + '\n' +
print(testType.astNode) + '\n' +
print(testDirective.astNode)
));
expect(restoredIDL).to.be.equal(printSchema(schema));

const testField = query.getFields().testField;
expect(print(testField.astNode)).to.equal(
'testField(testArg: TestInput): TestUnion'
);
expect(print(testField.args[0].astNode)).to.equal(
'testArg: TestInput'
);
expect(print(testInput.getFields().testInputField.astNode)).to.equal(
'testInputField: TestEnum'
);
expect(print(testEnum.getValue('TEST_VALUE').astNode)).to.equal(
'TEST_VALUE'
);
expect(print(testInterface.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String'
);
expect(print(testType.getFields().interfaceField.astNode)).to.equal(
'interfaceField: String'
);
expect(print(testDirective.args[0].astNode)).to.equal('arg: Int');
});
});

describe('Failures', () => {
Expand Down
17 changes: 14 additions & 3 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
null,
types,
directives,
astNode: schemaDef,
});

function getDirective(
Expand All @@ -275,6 +276,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
node => ((node.value: any): DirectiveLocationEnum)
),
args: directiveNode.arguments && makeInputValues(directiveNode.arguments),
astNode: directiveNode,
});
}

Expand Down Expand Up @@ -359,6 +361,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
description: getDescription(def),
fields: () => makeFieldDefMap(def),
interfaces: () => makeImplementedInterfaces(def),
astNode: def,
});
}

Expand All @@ -372,7 +375,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
type: produceOutputType(field.type),
description: getDescription(field),
args: makeInputValues(field.arguments),
deprecationReason: getDeprecationReason(field)
deprecationReason: getDeprecationReason(field),
astNode: field,
})
);
}
Expand All @@ -391,7 +395,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
return {
type,
description: getDescription(value),
defaultValue: valueFromAST(value.defaultValue, type)
defaultValue: valueFromAST(value.defaultValue, type),
astNode: value,
};
}
);
Expand All @@ -403,6 +408,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
name: typeName,
description: getDescription(def),
fields: () => makeFieldDefMap(def),
astNode: def,
resolveType: cannotExecuteSchema,
});
}
Expand All @@ -416,9 +422,11 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
enumValue => enumValue.name.value,
enumValue => ({
description: getDescription(enumValue),
deprecationReason: getDeprecationReason(enumValue)
deprecationReason: getDeprecationReason(enumValue),
astNode: enumValue,
})
),
astNode: def,
});

return enumType;
Expand All @@ -430,13 +438,15 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
description: getDescription(def),
types: def.types.map(t => produceObjectType(t)),
resolveType: cannotExecuteSchema,
astNode: def,
});
}

function makeScalarDef(def: ScalarTypeDefinitionNode) {
return new GraphQLScalarType({
name: def.name.value,
description: getDescription(def),
astNode: def,
serialize: () => null,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
Expand All @@ -452,6 +462,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
name: def.name.value,
description: getDescription(def),
fields: () => makeInputValues(def.fields),
astNode: def,
});
}
}
Expand Down

0 comments on commit 496ac0d

Please sign in to comment.