Skip to content

Commit

Permalink
Include directive argument types in getTypeMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogu committed Jan 3, 2018
1 parent 2616ced commit afe4735
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { GraphQLList } from '../wrappers';

const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
Expand All @@ -37,13 +38,25 @@ const DirectiveInputType = new GraphQLInputObjectType({
},
});

const WrappedDirectiveInputType = new GraphQLInputObjectType({
name: 'WrappedDirInput',
fields: {
field: {
type: GraphQLString,
},
},
});

const Directive = new GraphQLDirective({
name: 'dir',
locations: ['OBJECT'],
args: {
arg: {
type: DirectiveInputType,
},
argList: {
type: new GraphQLList(WrappedDirectiveInputType),
},
},
});

Expand Down Expand Up @@ -79,6 +92,7 @@ describe('Type System: Schema', () => {
describe('Type Map', () => {
it('includes input types only used in directives', () => {
expect(Schema.getTypeMap()).to.include.key('DirInput');
expect(Schema.getTypeMap()).to.include.key('WrappedDirInput');
});
});
});
19 changes: 18 additions & 1 deletion src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isUnionType,
isInputObjectType,
isWrappingType,
getNamedType,
} from './definition';
import type {
GraphQLType,
Expand All @@ -22,7 +23,11 @@ import type {
GraphQLInterfaceType,
} from './definition';
import type { SchemaDefinitionNode } from '../language/ast';
import { GraphQLDirective, specifiedDirectives } from './directives';
import {
GraphQLDirective,
isDirective,
specifiedDirectives,
} from './directives';
import type { GraphQLError } from '../error/GraphQLError';
import { __Schema } from './introspection';
import find from '../jsutils/find';
Expand Down Expand Up @@ -121,6 +126,10 @@ export class GraphQLSchema {
initialTypes = initialTypes.concat(types);
}

initialTypes = initialTypes.concat(
...this._directives.map(directive => getDirectiveArgTypes(directive)),
);

this._typeMap = initialTypes.reduce(
typeMapReducer,
(Object.create(null): TypeMap),
Expand Down Expand Up @@ -269,3 +278,11 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {

return reducedMap;
}

function getDirectiveArgTypes(directive: GraphQLDirective) {
// directives are not validated until validateSchema() is called, so be defensive
if (!isDirective(directive)) {
return [];
}
return directive.args.map(arg => arg.type);
}

0 comments on commit afe4735

Please sign in to comment.