From af5ef3c3c8f922de907f6f5403b054acf28911de Mon Sep 17 00:00:00 2001 From: Raymond Wang Date: Sat, 24 Jan 2026 15:00:31 -0500 Subject: [PATCH] Ignore client directives on fragments Fixes https://github.com/graphql-hive/graphql-eslint/issues/2941 --- .../__snapshots__/known-directives.spec.md | 2 +- .../plugin/__tests__/known-directives.spec.ts | 42 ++++++++++++++++++- .../plugin/src/rules/graphql-js-validation.ts | 3 ++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/plugin/__tests__/__snapshots__/known-directives.spec.md b/packages/plugin/__tests__/__snapshots__/known-directives.spec.md index 7e37884d714..44fa464baa8 100644 --- a/packages/plugin/__tests__/__snapshots__/known-directives.spec.md +++ b/packages/plugin/__tests__/__snapshots__/known-directives.spec.md @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`known-directives > invalid > should work only with Kind.FIELD 1`] = ` +exports[`known-directives > invalid > should not ignore directives on schema definitions 1`] = ` #### ⌨️ Code 1 | scalar Foo @bad diff --git a/packages/plugin/__tests__/known-directives.spec.ts b/packages/plugin/__tests__/known-directives.spec.ts index e3d02702b00..e33f9e5d652 100644 --- a/packages/plugin/__tests__/known-directives.spec.ts +++ b/packages/plugin/__tests__/known-directives.spec.ts @@ -1,6 +1,7 @@ import { RuleTester } from '@theguild/eslint-rule-tester'; import { GRAPHQL_JS_VALIDATIONS } from '../src/rules/graphql-js-validation.js'; -import { DEFAULT_CONFIG, ParserOptionsForTests } from './test-utils.js'; +import type { ParserOptionsForTests } from './test-utils.js'; +import { DEFAULT_CONFIG } from './test-utils.js'; const ruleTester = new RuleTester({ languageOptions: { @@ -55,10 +56,47 @@ ruleTester.run<[{ ignoreClientDirectives: string[] }]>( `, options: [{ ignoreClientDirectives: ['api'] }], }, + { + name: 'should ignore client directives on FragmentDefinition', + code: /* GraphQL */ ` + fragment UserFields on User @client { + id + } + `, + options: [{ ignoreClientDirectives: ['client'] }], + }, + { + name: 'should ignore client directives on InlineFragment', + code: /* GraphQL */ ` + { + user { + ... on User @client { + id + } + } + } + `, + options: [{ ignoreClientDirectives: ['client'] }], + }, + { + name: 'should ignore client directives on FragmentSpread', + code: /* GraphQL */ ` + fragment UserFields on User { + id + } + + { + user { + ...UserFields @client + } + } + `, + options: [{ ignoreClientDirectives: ['client'] }], + }, ], invalid: [ { - name: 'should work only with Kind.FIELD', + name: 'should not ignore directives on schema definitions', code: 'scalar Foo @bad', options: [{ ignoreClientDirectives: ['bad'] }], errors: [{ message: 'Unknown directive "@bad".' }], diff --git a/packages/plugin/src/rules/graphql-js-validation.ts b/packages/plugin/src/rules/graphql-js-validation.ts index 6c7918f6d7b..23ee9f99dae 100644 --- a/packages/plugin/src/rules/graphql-js-validation.ts +++ b/packages/plugin/src/rules/graphql-js-validation.ts @@ -309,6 +309,9 @@ export const GRAPHQL_JS_VALIDATIONS: Record = Object. return visit(documentNode, { Field: filterDirectives, + FragmentDefinition: filterDirectives, + FragmentSpread: filterDirectives, + InlineFragment: filterDirectives, OperationDefinition: filterDirectives, }); },