Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTypeMap() does not include input types used in directives #1189

Merged
merged 3 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLString,
GraphQLInputObjectType,
GraphQLDirective,
} from '../';

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

const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
Expand All @@ -26,6 +29,37 @@ const ImplementingType = new GraphQLObjectType({
fields: { fieldName: { type: GraphQLString, resolve: () => '' } },
});

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

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

const Directive = new GraphQLDirective({
name: 'dir',
locations: ['OBJECT'],
args: {
arg: {
type: DirectiveInputType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to also test wrapped type maybe as one more argument

},
argList: {
type: new GraphQLList(WrappedDirectiveInputType),
},
},
});

const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand All @@ -38,6 +72,7 @@ const Schema = new GraphQLSchema({
},
},
}),
directives: [Directive],
});

describe('Type System: Schema', () => {
Expand All @@ -53,4 +88,11 @@ 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yogu Looks great now!

One last issue from me: Can you please remove this line?
It cause Travis build to fail:
image

} 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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test that validateSchema throws a specific error when directives is invalid (a string instead of a proper instance). new GraphQLSchema() does not perform this validation, so for the mentioned test to pass, the constructor needs to accept the invalid directive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yogu Make sense 👍

return [];
}
return directive.args.map(arg => arg.type);
}