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

Require non-empty directive locations #4100

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ describe('Type System: A Schema must have Object root types', () => {
},
]);
});

it('rejects a Schema whose directives have empty locations', () => {
const badDirective = new GraphQLDirective({
name: 'BadDirective',
args: {},
locations: [],
});
const schema = new GraphQLSchema({
query: SomeObjectType,
directives: [badDirective],
});
expectJSON(validateSchema(schema)).toDeepEqual([
{
message: 'Directive @BadDirective must include 1 or more locations.',
},
]);
});
});

describe('Type System: Root types must all be different if provided', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ function validateDirectives(context: SchemaValidationContext): void {
// Ensure they are named correctly.
validateName(context, directive);

// TODO: Ensure proper locations.
if (directive.locations.length === 0) {
context.reportError(
`Directive @${directive.name} must include 1 or more locations.`,
directive.astNode,
);
}

// Ensure the arguments are valid.
for (const arg of directive.args) {
Expand Down
Loading