Skip to content

Commit f20f7de

Browse files
authored
feat: remove the need of #import comments in favor of parserOptions.operations (#832)
* feat: remove the need of `#import` comments in favor of `parserOptions.operations` refactor `no-undefined-variables` and `no-unused-variables` rules refactor `known-fragment-names` refactor `no-unused-fragments` add changeset fix false positive `Unknown fragment` error fix: should not throw an error on undefined fragment * update snapshot with fixed `graphql-js` report location
1 parent 3622fea commit f20f7de

23 files changed

+312
-195
lines changed

.changeset/old-jeans-push.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': minor
3+
---
4+
5+
feat: remove the need of `#import` comments in favor of `parserOptions.operations`

docs/rules/known-fragment-names.md

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
- Category: `Operations`
66
- Rule name: `@graphql-eslint/known-fragment-names`
77
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
8-
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
8+
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
99

1010
A GraphQL document is only valid if all `...Fragment` fragment spreads refer to fragments defined in the same document.
1111

1212
> This rule is a wrapper around a `graphql-js` validation function. [You can find its source code here](https://github.com/graphql/graphql-js/blob/main/src/validation/rules/KnownFragmentNamesRule.ts).
1313
1414
## Usage Examples
1515

16-
### Incorrect (fragment not defined in the document)
16+
### Incorrect
1717

1818
```graphql
1919
# eslint @graphql-eslint/known-fragment-names: 'error'
2020

2121
query {
2222
user {
2323
id
24-
...UserFields
24+
...UserFields # fragment not defined in the document
2525
}
2626
}
2727
```
@@ -44,44 +44,23 @@ query {
4444
}
4545
```
4646

47-
### Correct (existing import to UserFields fragment)
47+
### Correct (`UserFields` fragment located in a separate file)
4848

4949
```graphql
5050
# eslint @graphql-eslint/known-fragment-names: 'error'
5151

52-
#import '../UserFields.gql'
53-
52+
# user.gql
5453
query {
5554
user {
5655
id
5756
...UserFields
5857
}
5958
}
60-
```
61-
62-
### False positive case
6359

64-
For extracting documents from code under the hood we use [graphql-tag-pluck](https://graphql-tools.com/docs/graphql-tag-pluck) that [don't support string interpolation](https://stackoverflow.com/questions/62749847/graphql-codegen-dynamic-fields-with-interpolation/62751311#62751311) for this moment.
65-
66-
```js
67-
const USER_FIELDS = gql`
68-
fragment UserFields on User {
69-
id
70-
}
71-
`
72-
73-
const GET_USER = /* GraphQL */ `
74-
# eslint @graphql-eslint/known-fragment-names: 'error'
75-
76-
query User {
77-
user {
78-
...UserFields
79-
}
80-
}
81-
82-
# Will give false positive error 'Unknown fragment "UserFields"'
83-
${USER_FIELDS}
84-
`
60+
# user-fields.gql
61+
fragment UserFields on User {
62+
id
63+
}
8564
```
8665

8766
## Resources

docs/rules/no-undefined-variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Category: `Operations`
66
- Rule name: `@graphql-eslint/no-undefined-variables`
77
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
8-
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
8+
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
99

1010
A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.
1111

docs/rules/no-unused-variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Category: `Operations`
66
- Rule name: `@graphql-eslint/no-unused-variables`
77
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
8-
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
8+
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
99

1010
A GraphQL operation is only valid if all variables defined by an operation are used, either directly or within a spread fragment.
1111

packages/plugin/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"repository": "https://github.com/dotansimha/graphql-eslint",
55
"author": "Dotan Simha <[email protected]>",
66
"license": "MIT",
7+
"description": "GraphQL plugin for ESLint",
8+
"keywords": [
9+
"eslint",
10+
"eslintplugin",
11+
"eslint-plugin",
12+
"graphql"
13+
],
714
"sideEffects": false,
815
"main": "dist/index.js",
916
"module": "dist/index.mjs",
@@ -30,7 +37,6 @@
3037
"@babel/code-frame": "7.16.0",
3138
"@graphql-tools/code-file-loader": "7.2.3",
3239
"@graphql-tools/graphql-tag-pluck": "7.1.4",
33-
"@graphql-tools/import": "6.6.2",
3440
"@graphql-tools/utils": "8.5.4",
3541
"graphql-config": "4.1.0",
3642
"graphql-depth-limit": "1.1.0",

0 commit comments

Comments
 (0)