Skip to content

Commit b15df66

Browse files
authored
Reenable running require-id-when-available on FragmentDefinition (#2711)
* aa * aa
1 parent 5d63d08 commit b15df66

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

.changeset/neat-cows-worry.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+
Reenable running `require-id-when-available` on `FragmentDefinition`

packages/plugin/src/rules/require-selections/index.test.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,6 @@ const DOCUMENT_WITH_UNION = /* GraphQL */ `
8989

9090
ruleTester.run<RuleOptions, true>('require-selections', rule, {
9191
valid: [
92-
{
93-
name: 'should completely ignore FragmentDefinition',
94-
code: /* GraphQL */ `
95-
fragment UserFields on User {
96-
name
97-
posts {
98-
title
99-
}
100-
}
101-
`,
102-
parserOptions: {
103-
graphQLConfig: {
104-
schema: USER_POST_SCHEMA,
105-
documents: '{ foo }',
106-
},
107-
},
108-
},
10992
{
11093
name: "should ignore checking selections on OperationDefinition as it's redundant check",
11194
code: '{ foo }',
@@ -500,5 +483,23 @@ ruleTester.run<RuleOptions, true>('require-selections', rule, {
500483
},
501484
},
502485
},
486+
{
487+
name: 'should not ignore FragmentDefinition',
488+
code: /* GraphQL */ `
489+
fragment UserFields on User {
490+
name
491+
posts {
492+
title
493+
}
494+
}
495+
`,
496+
parserOptions: {
497+
graphQLConfig: {
498+
schema: USER_POST_SCHEMA,
499+
documents: '{ foo }',
500+
},
501+
},
502+
errors: 2,
503+
},
503504
],
504505
});

packages/plugin/src/rules/require-selections/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ export const rule: GraphQLESLintRule<RuleOptions, true> = {
118118

119119
// Check selections only in OperationDefinition,
120120
// skip selections of OperationDefinition and InlineFragment
121-
const selector =
122-
'OperationDefinition SelectionSet[parent.kind!=/(^OperationDefinition|InlineFragment)$/]';
121+
const selector = 'SelectionSet[parent.kind!=/(^OperationDefinition|InlineFragment)$/]';
123122
const typeInfo = new TypeInfo(schema);
124123

125124
function checkFragments(node: GraphQLESTreeNode<SelectionSetNode>): void {

packages/plugin/src/rules/require-selections/snapshot.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,50 @@ exports[`require-selections > invalid > Invalid #2 1`] = `
3838
1 | { hasId { name id } }
3939
`;
4040

41+
exports[`require-selections > invalid > should not ignore FragmentDefinition 1`] = `
42+
#### ⌨️ Code
43+
44+
1 | fragment UserFields on User {
45+
2 | name
46+
3 | posts {
47+
4 | title
48+
5 | }
49+
6 | }
50+
51+
#### ❌ Error 1/2
52+
53+
> 1 | fragment UserFields on User {
54+
| ^ Field \`UserFields.id\` must be selected when it's available on a type.
55+
Include it in your selection set.
56+
2 | name
57+
58+
#### 💡 Suggestion: Add \`id\` selection
59+
60+
1 | fragment UserFields on User {
61+
2 | id name
62+
3 | posts {
63+
4 | title
64+
5 | }
65+
6 | }
66+
67+
#### ❌ Error 2/2
68+
69+
2 | name
70+
> 3 | posts {
71+
| ^ Field \`posts.id\` must be selected when it's available on a type.
72+
Include it in your selection set.
73+
4 | title
74+
75+
#### 💡 Suggestion: Add \`id\` selection
76+
77+
1 | fragment UserFields on User {
78+
2 | name
79+
3 | posts {
80+
4 | id title
81+
5 | }
82+
6 | }
83+
`;
84+
4185
exports[`require-selections > invalid > should not work with n nested fragments if you never get the id 1`] = `
4286
#### ⌨️ Code
4387

0 commit comments

Comments
 (0)