Skip to content

Commit 993d7ce

Browse files
benjiejbellengersaihaj
authored
backport(v16): Require non-empty directive locations (#4100) (#4137)
Co-authored-by: James Bellenger <[email protected]> Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent 0517368 commit 993d7ce

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @graphql/graphql-js-reviewers

src/type/__tests__/validation-test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ describe('Type System: A Schema must have Object root types', () => {
425425
},
426426
]);
427427
});
428+
429+
it('rejects a Schema whose directives have empty locations', () => {
430+
const badDirective = new GraphQLDirective({
431+
name: 'BadDirective',
432+
args: {},
433+
locations: [],
434+
});
435+
const schema = new GraphQLSchema({
436+
query: SomeObjectType,
437+
directives: [badDirective],
438+
});
439+
expectJSON(validateSchema(schema)).toDeepEqual([
440+
{
441+
message: 'Directive @BadDirective must include 1 or more locations.',
442+
},
443+
]);
444+
});
428445
});
429446

430447
describe('Type System: Objects must have fields', () => {

src/type/validate.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ function validateDirectives(context: SchemaValidationContext): void {
172172
// Ensure they are named correctly.
173173
validateName(context, directive);
174174

175-
// TODO: Ensure proper locations.
175+
if (directive.locations.length === 0) {
176+
context.reportError(
177+
`Directive @${directive.name} must include 1 or more locations.`,
178+
directive.astNode,
179+
);
180+
}
176181

177182
// Ensure the arguments are valid.
178183
for (const arg of directive.args) {

0 commit comments

Comments
 (0)