Skip to content

Commit

Permalink
backport(v16): Require non-empty directive locations (#4100) (#4137)
Browse files Browse the repository at this point in the history
Co-authored-by: James Bellenger <[email protected]>
Co-authored-by: Saihajpreet Singh <[email protected]>
  • Loading branch information
3 people committed Sep 18, 2024
1 parent 0517368 commit 993d7ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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

0 comments on commit 993d7ce

Please sign in to comment.