Skip to content

Commit 89a1892

Browse files
committed
Add an ignoredFields option for relay/unused-fields
1 parent f9e57b4 commit 89a1892

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,42 @@ The following rules support suppression within graphql tags:
5252
Supported rules can be suppressed by adding `# eslint-disable-next-line relay/name-of-rule` to the preceding line:
5353

5454
```js
55-
graphql`fragment foo on Page {
56-
# eslint-disable-next-line relay/must-colocate-fragment-spreads
57-
...unused1
58-
}`
55+
graphql`
56+
fragment foo on Page {
57+
# eslint-disable-next-line relay/must-colocate-fragment-spreads
58+
...unused1
59+
}
60+
`;
5961
```
6062

6163
Note that only the `eslint-disable-next-line` form of suppression works. `eslint-disable-line` doesn't currently work until graphql-js provides support for [parsing Comment nodes](https://github.com/graphql/graphql-js/issues/2241) in their AST.
6264

65+
## Ignoring fields with `unused-fields`
66+
67+
Sometimes you might have fields that are accessed by library code (outside the current file), for example:
68+
69+
```js
70+
import getNodes from 'some/lib';
71+
72+
graphql`
73+
fragment foo on Page {
74+
edges { # not directly accessed by this file
75+
node { # not directly accessed by this file
76+
name
77+
}
78+
}
79+
}
80+
`;
81+
82+
getNodes(foo).map(node => node.name);
83+
```
84+
85+
You can ignore the `unused-fields` rule for these fields by providing the `ignoredFields` option.
86+
87+
```js
88+
'relay/unused-fields': ['warn', {ignoredFields: ['edges', 'node']}],
89+
```
90+
6391
## Contribute
6492

6593
We actively welcome pull requests, learn how to [contribute](./CONTRIBUTING.md).

src/rule-unused-fields.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ function isPageInfoField(field) {
9292
}
9393
}
9494

95+
function isIgnoredField(field, options = []) {
96+
const {ignoredFields = []} = options[0] || {};
97+
return ignoredFields.includes(field);
98+
}
99+
95100
function rule(context) {
96101
let currentMethod = [];
97102
let foundMemberAccesses = {};
@@ -147,6 +152,7 @@ function rule(context) {
147152
if (
148153
!foundMemberAccesses[field] &&
149154
!isPageInfoField(field) &&
155+
!isIgnoredField(field, context.options) &&
150156
// Do not warn for unused __typename which can be a workaround
151157
// when only interested in existence of an object.
152158
field !== '__typename'
@@ -205,4 +211,20 @@ function rule(context) {
205211
};
206212
}
207213

208-
module.exports = rule;
214+
module.exports = module.exports = {
215+
meta: {
216+
schema: [
217+
{
218+
type: 'object',
219+
properties: {
220+
ignoredFields: {
221+
type: 'array',
222+
items: {type: 'string'}
223+
}
224+
},
225+
additionalProperties: false
226+
}
227+
]
228+
},
229+
create: rule
230+
};

test/unused-fields.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
8989
# eslint-disable-next-line relay/unused-fields
9090
name
9191
}\`;
92-
`
92+
`,
93+
{
94+
code: `
95+
graphql\`fragment foo on Page {
96+
edges { node { name } }
97+
}\`;
98+
getCollectionNodes(foo)[0]?.name;
99+
`,
100+
options: [{ignoredFields: ['node', 'edges']}]
101+
}
93102
],
94103
invalid: [
95104
{
@@ -163,6 +172,15 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
163172
line: 4
164173
}
165174
]
175+
},
176+
{
177+
code: `
178+
graphql\`fragment foo on Page {
179+
edges { node { name } }
180+
}\`;
181+
getCollectionNodes(foo)[0]?.name;
182+
`,
183+
errors: [unusedFieldsWarning('edges'), unusedFieldsWarning('node')]
166184
}
167185
]
168186
});

0 commit comments

Comments
 (0)