Skip to content

Commit daa04bb

Browse files
Support aliases in require-id-when-available (#1019)
* feat: support aliases in require-id-when-available Fixes #1017 * add changeset Co-authored-by: Dimitri POSTOLOV <[email protected]>
1 parent 2efc772 commit daa04bb

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

.changeset/dirty-rules-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
feat: support select `id` field with an alias in `require-id-when-available` rule

docs/rules/require-id-when-available.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ query {
5050
name
5151
}
5252
}
53+
54+
# Selecting `id` with an alias is also valid
55+
query {
56+
user {
57+
id: name
58+
}
59+
}
5360
```
5461

5562
## Config Schema

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ const rule: GraphQLESLintRule<[RequireIdWhenAvailableRuleConfig], true> = {
7070
name
7171
}
7272
}
73+
74+
# Selecting \`id\` with an alias is also valid
75+
query {
76+
user {
77+
id: name
78+
}
79+
}
7380
`,
7481
},
7582
],
@@ -164,6 +171,10 @@ const rule: GraphQLESLintRule<[RequireIdWhenAvailableRuleConfig], true> = {
164171
function hasIdField({ selections }: typeof node): boolean {
165172
return selections.some(selection => {
166173
if (selection.kind === Kind.FIELD) {
174+
if (selection.alias && idNames.includes(selection.alias.value)) {
175+
return true;
176+
}
177+
167178
return idNames.includes(selection.name.value);
168179
}
169180

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ ruleTester.runGraphQLTests<[RequireIdWhenAvailableRuleConfig], true>('require-id
273273
`,
274274
},
275275
},
276+
{name: 'should work when `id` is selected by an alias', ...WITH_SCHEMA, code: '{ hasId { id: name } }' },
276277
],
277278
invalid: [
278279
{

0 commit comments

Comments
 (0)