From 5593891c8c65a0bf75eb24a8d284b891fe1f17a7 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Wed, 1 Jun 2022 17:10:13 -0700 Subject: [PATCH] fix(known-directives): support `ignoreClientDirectives` for `OperationDefinition` (#1080) --- .changeset/young-stingrays-invite.md | 5 +++++ .../plugin/src/rules/graphql-js-validation.ts | 15 +++++++++------ packages/plugin/tests/known-directives.spec.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 .changeset/young-stingrays-invite.md diff --git a/.changeset/young-stingrays-invite.md b/.changeset/young-stingrays-invite.md new file mode 100644 index 00000000000..ac84d198705 --- /dev/null +++ b/.changeset/young-stingrays-invite.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +fix(known-directives): support `ignoreClientDirectives` for `OperationDefinition` diff --git a/packages/plugin/src/rules/graphql-js-validation.ts b/packages/plugin/src/rules/graphql-js-validation.ts index e8bf94d915d..9f3c7dcc96f 100644 --- a/packages/plugin/src/rules/graphql-js-validation.ts +++ b/packages/plugin/src/rules/graphql-js-validation.ts @@ -10,6 +10,7 @@ import { validate, ASTVisitor, ExecutableDefinitionNode, + DirectiveNode, } from 'graphql'; import { validateSDL } from 'graphql/validation/validate'; import type { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types'; @@ -231,13 +232,15 @@ export const GRAPHQL_JS_VALIDATIONS: Record = Object. if (ignoreClientDirectives.length === 0) { return documentNode; } + + const filterDirectives = (node: { directives?: ReadonlyArray }) => ({ + ...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, }); }, { diff --git a/packages/plugin/tests/known-directives.spec.ts b/packages/plugin/tests/known-directives.spec.ts index 60b98232f0e..bdeaa3f40a4 100644 --- a/packages/plugin/tests/known-directives.spec.ts +++ b/packages/plugin/tests/known-directives.spec.ts @@ -34,6 +34,14 @@ ruleTester.runGraphQLTests<[{ ignoreClientDirectives: string[] }]>('known-direct `, options: [{ ignoreClientDirectives: ['rest'] }], }, + { + code: /* GraphQL */ ` + query @api { + test + } + `, + options: [{ ignoreClientDirectives: ['api'] }], + }, ], invalid: [ {