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

backport(v16): Require non-empty directive locations (#4100) #4137

Open
wants to merge 2 commits into
base: 16.x.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @graphql/graphql-js-reviewers
17 changes: 17 additions & 0 deletions src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,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: Objects must have fields', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,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