Skip to content

Commit

Permalink
Do not enforce query @directive locations.
Browse files Browse the repository at this point in the history
Since SchemaDirectiveVisitor cannot visit query types, we should not throw
an exception if a SchemaDirectiveVisitor fails to implement a visitor
method for a query type location.

Fixes #680.
  • Loading branch information
benjamn committed Mar 16, 2018
1 parent 247067c commit 55ff203
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/schemaVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export abstract class SchemaVisitor {
return false;
}

if (this === SchemaVisitor) {
// The SchemaVisitor class implements every visitor method.
return true;
}

const stub = SchemaVisitor.prototype[methodName];
if (method === stub) {
// If this.prototype[methodName] was just inherited from SchemaVisitor,
Expand Down Expand Up @@ -642,7 +647,8 @@ export class SchemaDirectiveVisitor extends SchemaVisitor {

each(decl.locations, loc => {
const visitorMethodName = directiveLocationToVisitorMethodName(loc);
if (! visitorClass.implementsVisitorMethod(visitorMethodName)) {
if (SchemaVisitor.implementsVisitorMethod(visitorMethodName) &&
! visitorClass.implementsVisitorMethod(visitorMethodName)) {
// While visitor subclasses may implement extra visitor methods,
// it's definitely a mistake if the GraphQLDirective declares itself
// applicable to certain schema locations, and the visitor subclass
Expand Down
26 changes: 26 additions & 0 deletions src/test/testDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,4 +1233,30 @@ describe('@directives', () => {
Human
);
});

it('does not enforce query directive locations (issue #680)', () => {
const visited = new Set<GraphQLObjectType>();
const schema = makeExecutableSchema({
typeDefs: `
directive @hasScope(scope: [String]) on QUERY | FIELD
type Query @hasScope {
oyez: String
}`,

schemaDirectives: {
hasScope: class extends SchemaDirectiveVisitor {
public visitObject(object: GraphQLObjectType) {
assert.strictEqual(object.name, 'Query');
visited.add(object);
}
}
}
});

assert.strictEqual(visited.size, 1);
visited.forEach(object => {
assert.strictEqual(schema.getType('Query'), object);
});
});
});

0 comments on commit 55ff203

Please sign in to comment.