Skip to content

Commit

Permalink
fix(known-directives): support ignoreClientDirectives for `Operatio…
Browse files Browse the repository at this point in the history
…nDefinition` (#1080)
  • Loading branch information
dimaMachina authored Jun 2, 2022
1 parent 25049cb commit 5593891
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-stingrays-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix(known-directives): support `ignoreClientDirectives` for `OperationDefinition`
15 changes: 9 additions & 6 deletions packages/plugin/src/rules/graphql-js-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
validate,
ASTVisitor,
ExecutableDefinitionNode,
DirectiveNode,
} from 'graphql';
import { validateSDL } from 'graphql/validation/validate';
import type { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
Expand Down Expand Up @@ -231,13 +232,15 @@ export const GRAPHQL_JS_VALIDATIONS: Record<string, GraphQLESLintRule> = Object.
if (ignoreClientDirectives.length === 0) {
return documentNode;
}

const filterDirectives = (node: { directives?: ReadonlyArray<DirectiveNode> }) => ({
...node,
directives: node.directives.filter(directive => !ignoreClientDirectives.includes(directive.name.value)),
});

return visit(documentNode, {
Field(node) {
return {
...node,
directives: node.directives.filter(directive => !ignoreClientDirectives.includes(directive.name.value)),
};
},
Field: filterDirectives,
OperationDefinition: filterDirectives,
});
},
{
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin/tests/known-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ ruleTester.runGraphQLTests<[{ ignoreClientDirectives: string[] }]>('known-direct
`,
options: [{ ignoreClientDirectives: ['rest'] }],
},
{
code: /* GraphQL */ `
query @api {
test
}
`,
options: [{ ignoreClientDirectives: ['api'] }],
},
],
invalid: [
{
Expand Down

0 comments on commit 5593891

Please sign in to comment.