Skip to content

Commit 08007cb

Browse files
committed
fixes issues with require-id-when-available and fragment spreads parent
1 parent 003fbef commit 08007cb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/plugin/src/rules/require-id-when-available.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = {
8282
}
8383
}
8484

85-
if (!found) {
85+
const { parent } = node as any;
86+
const hasIdFieldInInterfaceSelectionSet =
87+
parent &&
88+
parent.kind === 'InlineFragment' &&
89+
parent.parent &&
90+
parent.parent.kind === 'SelectionSet' &&
91+
!!parent.parent.selections.find(s => s.kind === 'Field' && s.name.value === fieldName);
92+
93+
if (!found && !hasIdFieldInInterfaceSelectionSet) {
8694
context.report({
8795
loc: {
8896
start: {

packages/plugin/tests/require-id-when-available.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ ruleTester.runGraphQLTests('require-id-when-available', rule, {
6363
code: `query { hasId { name } }`,
6464
options: [{ fieldName: 'name' }],
6565
},
66-
],
67-
invalid: [
6866
{
6967
...WITH_SCHEMA,
7068
code: `query { vehicles { id ...on Car { mileage } } }`,
71-
errors: [{ messageId: 'REQUIRE_ID_WHEN_AVAILABLE' }],
7269
},
70+
],
71+
invalid: [
7372
{
7473
...WITH_SCHEMA,
7574
code: `query { hasId { name } }`,

0 commit comments

Comments
 (0)