@graphql-eslint/eslint-plugin 2.4.1 → 2.5.0-alpha-14532ce.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,18 +6,18 @@ This project integrates GraphQL and ESLint, for a better developer experience.
6
6
 
7
7
  [![npm version](https://badge.fury.io/js/%40graphql-eslint%2Feslint-plugin.svg)](https://badge.fury.io/js/%40graphql-eslint%2Feslint-plugin)
8
8
 
9
- > Created and maintained by [The Guild](http://the-guild.dev/)
9
+ > Created and maintained by [The Guild](http://the-guild.dev)
10
10
 
11
11
  ## Key Features
12
12
 
13
- - 🚀 Integrates with ESLint core (as a ESTree parser).
14
- - 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments.
15
- - 🚀 Lints both GraphQL schema and GraphQL operations.
13
+ - 🚀 Integrates with ESLint core (as a ESTree parser)
14
+ - 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments
15
+ - 🚀 Lints both GraphQL schema and GraphQL operations
16
16
  - 🚀 Extended type info for more advanced usages
17
- - 🚀 Supports ESLint directives (for example: `disable-next-line`)
18
- - 🚀 Easily extendable - supports custom rules based on GraphQL's AST and ESLint API.
19
- - 🚀 Validates, lints, prettifies and checks for best practices across GraphQL schema and GraphQL operations.
20
- - 🚀 Integrates with [`graphql-config`](https://graphql-config.com/)
17
+ - 🚀 Supports ESLint directives (for example: `eslint-disable-next-line`)
18
+ - 🚀 Easily extendable - supports custom rules based on GraphQL's AST and ESLint API
19
+ - 🚀 Validates, lints, prettifies and checks for best practices across GraphQL schema and GraphQL operations
20
+ - 🚀 Integrates with [`graphql-config`](https://graphql-config.com)
21
21
  - 🚀 Integrates and visualizes lint issues in popular IDEs (VSCode / WebStorm)
22
22
 
23
23
  > Special thanks to [ilyavolodin](https://github.com/ilyavolodin) for his work on a similar project!
@@ -42,11 +42,13 @@ Or, with NPM:
42
42
  npm install --save-dev @graphql-eslint/eslint-plugin
43
43
  ```
44
44
 
45
- > Also, make sure you have `graphql` dependency in your project.
45
+ > Make sure you have `graphql` dependency in your project.
46
46
 
47
47
  ### Configuration
48
48
 
49
- To get started, create an override configuration for your ESLint, while applying it to `.graphql` files (do that even if you are declaring your operations in code files):
49
+ To get started, define an override in your ESLint config to apply this plugin to `.graphql` files. Add the [rules](docs/README.md) you want applied.
50
+
51
+ > 🚨 Important! This step is necessary even if you are declaring operations and/or schema in code files.
50
52
 
51
53
  ```json
52
54
  {
@@ -56,74 +58,90 @@ To get started, create an override configuration for your ESLint, while applying
56
58
  "parser": "@graphql-eslint/eslint-plugin",
57
59
  "plugins": ["@graphql-eslint"],
58
60
  "rules": {
59
- ...
61
+ "@graphql-eslint/known-type-names": "error"
60
62
  }
61
63
  }
62
64
  ]
63
65
  }
64
66
  ```
65
67
 
66
- If you are using code files to store your GraphQL schema or your GraphQL operations, you can extend the behaviour of ESLint and extract those, by adding **an additional `override`** that does that extraction processes:
68
+ If your GraphQL definitions are defined only in `.graphql` files, and you're only using rules that apply to individual files, you should be good to go 👍. If you would like use a remote schema or use rules that apply across the entire collection of definitions at once, see [here](#using-a-remote-schema-or-rules-with-constraints-that-span-the-entire-schema).
67
69
 
68
- ```json
70
+ #### Tell ESLint to apply this plugin to GraphQL definitions defined in code files
71
+
72
+ If you are defining GraphQL schema or GraphQL operations in code files, you'll want to define an additional override to extend the functionality of this plugin to the schema and operations in those files.
73
+
74
+ ```diff
69
75
  {
70
76
  "overrides": [
71
- {
72
- "files": ["*.tsx", "*.ts", "*.jsx", "*.js"],
73
- "processor": "@graphql-eslint/graphql"
74
- },
77
+ + {
78
+ + "files": ["*.js"],
79
+ + "processor": "@graphql-eslint/graphql"
80
+ + },
75
81
  {
76
82
  "files": ["*.graphql"],
77
83
  "parser": "@graphql-eslint/eslint-plugin",
78
84
  "plugins": ["@graphql-eslint"],
79
85
  "rules": {
80
- ...
86
+ "@graphql-eslint/known-type-names": "error"
81
87
  }
82
88
  }
83
89
  ]
84
90
  }
85
91
  ```
86
92
 
87
- #### Extended linting rules with GraphQL Schema
93
+ Under the hood, specifying the `@graphql-eslint/graphql` processor for code files will cause `graphql-eslint/graphql` to extract the schema and operation definitions from these files into virtual GraphQL documents with `.graphql` extensions. This will allow the overrides you've defined for `.graphql` files, via `"files": ["*.graphql"]`, to get applied to the definitions defined in your code files.
88
94
 
89
- If you are using [`graphql-config`](https://graphql-config.com/) - you are good to go. This package integrates with it automatically, and will use it to load your schema!
95
+ #### Using a remote schema or rules with constraints that span the entire schema
90
96
 
91
- Linting process can be enriched and extended with GraphQL type information, if you are able to provide your GraphQL schema.
97
+ Some rules require an understanding of the entire schema at once. For example, [no-unreachable-types](https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-unreachable-types.md#no-unreachable-types) checks that all types are reachable by root-level fields.
92
98
 
93
- The parser allow you to specify a json file / graphql files(s) / url / raw string to locate your schema (We are using `graphql-tools` to do that). Just add `parserOptions.schema` to your configuration file:
99
+ To use these rules, you'll need to tell ESLint how to identify the entire set of schema definitions.
94
100
 
95
- ```json
101
+ If you are using [`graphql-config`](https://graphql-config.com), you are good to go. `graphql-eslint` integrates with it automatically and will use it to load your schema!
102
+
103
+ Alternatively, you can define `parserOptions.schema` in the `*.graphql` override in your ESLint config.
104
+
105
+ The parser allows you to specify a json file / graphql files(s) / url / raw string to locate your schema (We are using `graphql-tools` to do that). Just add `parserOptions.schema` to your configuration file:
106
+
107
+ ```diff
96
108
  {
97
109
  "files": ["*.graphql"],
98
110
  "parser": "@graphql-eslint/eslint-plugin",
99
111
  "plugins": ["@graphql-eslint"],
100
- "parserOptions": {
101
- "schema": "./schema.graphql"
102
- }
112
+ "rules": {
113
+ "@graphql-eslint/no-unreachable-types": "error"
114
+ },
115
+ + "parserOptions": {
116
+ + "schema": "./schema.graphql"
117
+ + }
103
118
  }
104
119
  ```
105
120
 
106
- > You can find a complete [documentation of the `parserOptions` here](./docs/parser-options.md)
121
+ > You can find a complete [documentation of the `parserOptions` here](docs/parser-options.md).
107
122
 
108
- > Some rules requires type information to operate, it's marked in the docs of each plugin!
123
+ > Some rules require type information to operate, it's marked in the docs for each rule!
109
124
 
110
125
  #### Extended linting rules with siblings operations
111
126
 
112
127
  While implementing this tool, we had to find solutions for a better integration of the GraphQL ecosystem and ESLint core.
113
128
 
114
- GraphQL operations can be distributed across many files, while ESLint operates on one file at a time. If you are using GraphQL fragments in separate files, some rules might yield incorrect results, due the the missing information.
129
+ GraphQL operations can be distributed across many files, while ESLint operates on one file at a time. If you are using GraphQL fragments in separate files, some rules might yield incorrect results, due the missing information.
115
130
 
116
131
  To workaround that, we allow you to provide additional information on your GraphQL operations, making it available for rules while doing the actual linting.
117
132
 
118
- To provide that, we are using `@graphql-tools` loaders to load your sibling operations and fragments, just specify a glob expression(s) that points to your code/.graphql files:
133
+ To provide that, we are using `graphql-tools` loaders to load your sibling operations and fragments, just specify a glob expression(s) that points to your code/`.graphql` files:
119
134
 
120
- ```json
135
+ ```diff
121
136
  {
122
137
  "files": ["*.graphql"],
123
138
  "parser": "@graphql-eslint/eslint-plugin",
124
139
  "plugins": ["@graphql-eslint"],
140
+ "rules": {
141
+ "@graphql-eslint/unique-operation-name": "error"
142
+ },
125
143
  "parserOptions": {
126
- "operations": ["./src/**/*.graphql"],
144
+ + "operations": "./src/**/*.graphql",
127
145
  "schema": "./schema.graphql"
128
146
  }
129
147
  }
@@ -131,13 +149,13 @@ To provide that, we are using `@graphql-tools` loaders to load your sibling oper
131
149
 
132
150
  ### VSCode Integration
133
151
 
134
- By default, [ESLint VSCode plugin](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) will not lint files with extensions other then js, jsx, ts, tsx.
152
+ By default, [ESLint VSCode plugin](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) will not lint files with extensions other than `js`, `jsx`, `ts` and `tsx`.
135
153
 
136
154
  In order to enable it processing other extensions, add the following section in `settings.json` or workspace configuration.
137
155
 
138
156
  ```json
139
157
  {
140
- "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "graphql"],
158
+ "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "graphql"]
141
159
  }
142
160
  ```
143
161
 
@@ -156,13 +174,13 @@ type Query {
156
174
  }
157
175
  ```
158
176
 
159
- You can also specify specific rules to disable, apply it over the entire file, `next-line` or (current) `line`.
177
+ You can also specify specific rules to disable, apply it over the entire file, `eslint-disable-next-line` or current `eslint-disable-line`.
160
178
 
161
179
  You can find a list of [ESLint directives here](https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments).
162
180
 
163
181
  ## Available Rules
164
182
 
165
- You can find a complete list of [all available rules here](./docs/README.md)
183
+ You can find a complete list of [all available rules here](docs/README.md).
166
184
 
167
185
  ## Available Configs
168
186
 
@@ -177,19 +195,13 @@ Enable it in your `.eslintrc` file with the `extends` option.
177
195
  "overrides": [
178
196
  {
179
197
  "files": ["*.js"],
180
- "processor": "@graphql-eslint/graphql",
181
- "rules": {
182
- // your rules for JavaScript files
183
- }
198
+ "processor": "@graphql-eslint/graphql"
184
199
  },
185
200
  {
186
201
  "files": ["*.graphql"],
187
202
  - "parser": "@graphql-eslint/eslint-plugin",
188
203
  - "plugins": ["@graphql-eslint"],
189
- + "extends": "plugin:@graphql-eslint/recommended", // or plugin:@graphql-eslint/all
190
- "rules": {
191
- // your rules for GraphQL files
192
- }
204
+ + "extends": "plugin:@graphql-eslint/recommended" // or plugin:@graphql-eslint/all
193
205
  }
194
206
  ]
195
207
  }
@@ -197,26 +209,23 @@ Enable it in your `.eslintrc` file with the `extends` option.
197
209
 
198
210
  ### `prettier` rule
199
211
 
200
- The original `prettier` rule has been removed because `eslint-plugin-prettier` supports `.graphql` files well actually.
201
-
202
- All you need to do is like the following for now:
212
+ `eslint-plugin-prettier` supports `.graphql` files. You need to do the following:
203
213
 
204
214
  ```js
205
- // .eslintrc.js
206
215
  module.exports = {
207
216
  overrides: [
208
217
  {
209
218
  files: ['*.js'],
210
219
  processor: '@graphql-eslint/graphql',
211
- extends: ['plugin:prettier/recommended'],
220
+ extends: ['plugin:prettier/recommended']
212
221
  },
213
222
  {
214
223
  files: ['*.graphql'],
215
224
  parser: '@graphql-eslint/eslint-plugin',
216
225
  plugins: ['@graphql-eslint'],
217
226
  rules: {
218
- 'prettier/prettier': 'error',
219
- },
227
+ 'prettier/prettier': 'error'
228
+ }
220
229
  },
221
230
  // the following is required for `eslint-plugin-prettier@<=3.4.0` temporarily
222
231
  // after https://github.com/prettier/eslint-plugin-prettier/pull/415
@@ -224,22 +233,22 @@ module.exports = {
224
233
  {
225
234
  files: ['*.js/*.graphql'],
226
235
  rules: {
227
- 'prettier/prettier': 'off',
228
- },
229
- },
230
- ],
231
- };
236
+ 'prettier/prettier': 'off'
237
+ }
238
+ }
239
+ ]
240
+ }
232
241
  ```
233
242
 
234
243
  You can take [`examples/prettier`](examples/prettier/.eslintrc.js) as example.
235
244
 
236
- It could be better to remove the unnecessary `*.js/*.graphql` overrides setting if <https://github.com/prettier/eslint-plugin-prettier/pull/415> will be merged and released.
245
+ It could be better to remove the unnecessary `*.js/*.graphql` override setting if <https://github.com/prettier/eslint-plugin-prettier/pull/415> will be merged and released.
237
246
 
238
247
  Please help to vote up if you want to speed up the progress.
239
248
 
240
249
  ## Further Reading
241
250
 
242
- If you wish to learn more about this project, how the parser works, how to add custom rules and more, [please refer to the docs directory](./docs/README.md))
251
+ If you wish to learn more about this project, how the parser works, how to add custom rules and more, [please refer to the docs directory](docs/README.md).
243
252
 
244
253
  ## Contributions
245
254
 
@@ -249,8 +258,8 @@ And if this is your first time contributing to this project, please do read our
249
258
 
250
259
  ### Code of Conduct
251
260
 
252
- Help us keep GraphQL ESLint open and inclusive. Please read and follow our [Code of Conduct](https://github.com/the-guild-org/Stack/blob/master/CODE_OF_CONDUCT.md) as adopted from [Contributor Covenant](https://www.contributor-covenant.org/)
261
+ Help us keep GraphQL ESLint open and inclusive. Please read and follow our [Code of Conduct](https://github.com/the-guild-org/Stack/blob/master/CODE_OF_CONDUCT.md) as adopted from [Contributor Covenant](https://contributor-covenant.org).
253
262
 
254
263
  ## License
255
264
 
256
- Released under the [MIT license](./LICENSE).
265
+ Released under the [MIT license](LICENSE).
@@ -17,22 +17,22 @@ The `graphql-eslint` comes with a TypeScript wrapper for ESLint rules, and provi
17
17
  Here's an example for a simple rule that reports on anonymous GraphQL operations:
18
18
 
19
19
  ```ts
20
- import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin';
20
+ import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin'
21
21
 
22
22
  const rule: GraphQLESLintRule = {
23
23
  create(context) {
24
24
  return {
25
25
  OperationDefinition(node) {
26
- if (node && (!node.name || node.name.value === '')) {
26
+ if (!node.name || !node.name.value) {
27
27
  context.report({
28
- node: node,
29
- message: `Oops, name is required!`,
30
- });
28
+ node,
29
+ message: 'Oops, name is required!'
30
+ })
31
31
  }
32
- },
33
- };
34
- },
35
- };
32
+ }
33
+ }
34
+ }
35
+ }
36
36
  ```
37
37
 
38
38
  So what happens here?
@@ -58,23 +58,23 @@ This is useful if you wish to use other GraphQL tools that works with the origin
58
58
  Here's an example for using original `graphql-js` validate method to validate `OperationDefinition`:
59
59
 
60
60
  ```ts
61
- import { validate } from 'graphql';
62
- import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin';
61
+ import { validate } from 'graphql'
62
+ import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
63
63
 
64
64
  export const rule = {
65
65
  create(context) {
66
66
  return {
67
67
  OperationDefinition(node) {
68
- const schema = requireGraphQLSchemaFromContext(context);
68
+ const schema = requireGraphQLSchemaFromContext(context)
69
69
 
70
70
  validate(context, schema, {
71
71
  kind: Kind.DOCUMENT,
72
- definitions: [node.rawNode()],
73
- });
74
- },
75
- };
76
- },
77
- };
72
+ definitions: [node.rawNode()]
73
+ })
74
+ }
75
+ }
76
+ }
77
+ }
78
78
  ```
79
79
 
80
80
  ## `TypeInfo` / `GraphQLSchema`
@@ -89,7 +89,7 @@ If you provide GraphQL schema in your ESLint configuration, it will get loaded a
89
89
  To mark your ESLint rules as a rule that needs access to GraphQL schema, start by running `requireGraphQLSchemaFromContext` from the plugin package, it will make sure to return a schema, or throw an error for the user about the missing schema.
90
90
 
91
91
  ```ts
92
- const schema = requireGraphQLSchemaFromContext(context);
92
+ const schema = requireGraphQLSchemaFromContext(context)
93
93
  ```
94
94
 
95
95
  #### Accessing TypeInfo
@@ -100,23 +100,23 @@ If your plugin requires `typeInfo` in order to operate and run, make sure to cal
100
100
  `typeInfo` is provided on every node, based on the type of that node, for example, to access the `GraphQLOutputType` while you are visiting a `SelectionSet` node, you can do:
101
101
 
102
102
  ```ts
103
- import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin';
103
+ import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
104
104
 
105
105
  export const rule = {
106
106
  create(context) {
107
- requireGraphQLSchemaFromContext(context);
107
+ requireGraphQLSchemaFromContext(context)
108
108
 
109
109
  return {
110
110
  SelectionSet(node) {
111
- const typeInfo = node.typeInfo();
111
+ const typeInfo = node.typeInfo()
112
112
 
113
113
  if (typeInfo && typeInfo.gqlType) {
114
- console.log(`The GraphQLOutputType is: ${typeInfo.gqlType}`);
114
+ console.log(`The GraphQLOutputType is: ${typeInfo.gqlType}`)
115
115
  }
116
- },
117
- };
118
- },
119
- };
116
+ }
117
+ }
118
+ }
119
+ }
120
120
  ```
121
121
 
122
122
  The structure of the return value of `.typeInfo()` is [defined here](https://github.com/dotansimha/graphql-eslint/blob/master/packages/plugin/src/estree-parser/converter.ts#L38-L46). So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.
@@ -128,21 +128,21 @@ To test your rules, you can either use the wrapped `GraphQLRuleTester` from this
128
128
  The wrapped `GraphQLRuleTester` provides built-in configured parser, and a schema loader, if you need to test your rule with a loaded schema.
129
129
 
130
130
  ```ts
131
- import { GraphQLRuleTester } from '@graphql-eslint/eslint-plugin';
132
- import { rule } from './my-rule';
131
+ import { GraphQLRuleTester } from '@graphql-eslint/eslint-plugin'
132
+ import { rule } from './my-rule'
133
133
 
134
- const ruleTester = new GraphQLRuleTester();
134
+ const ruleTester = new GraphQLRuleTester()
135
135
 
136
136
  ruleTester.runGraphQLTests('my-rule', rule, {
137
137
  valid: [
138
138
  {
139
- code: `query something { foo }`,
140
- },
139
+ code: 'query something { foo }'
140
+ }
141
141
  ],
142
142
  invalid: [
143
143
  {
144
- code: `query invalid { foo }`,
145
- },
146
- ],
147
- });
144
+ code: 'query invalid { foo }'
145
+ }
146
+ ]
147
+ })
148
148
  ```
package/index.js CHANGED
@@ -3634,7 +3634,12 @@ function getReachableTypes(schema) {
3634
3634
  Directive: collect,
3635
3635
  NamedType: collect,
3636
3636
  };
3637
- for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
3637
+ for (const type of [
3638
+ schema,
3639
+ schema.getQueryType(),
3640
+ schema.getMutationType(),
3641
+ schema.getSubscriptionType(),
3642
+ ]) {
3638
3643
  if (type) {
3639
3644
  graphql.visit(type.astNode, visitor);
3640
3645
  }
package/index.mjs CHANGED
@@ -3628,7 +3628,12 @@ function getReachableTypes(schema) {
3628
3628
  Directive: collect,
3629
3629
  NamedType: collect,
3630
3630
  };
3631
- for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
3631
+ for (const type of [
3632
+ schema,
3633
+ schema.getQueryType(),
3634
+ schema.getMutationType(),
3635
+ schema.getSubscriptionType(),
3636
+ ]) {
3632
3637
  if (type) {
3633
3638
  visit(type.astNode, visitor);
3634
3639
  }
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "2.4.1",
3
+ "version": "2.5.0-alpha-14532ce.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "@graphql-tools/code-file-loader": "^7.0.2",
10
- "@graphql-tools/graphql-tag-pluck": "^7.0.2",
11
- "@graphql-tools/import": "^6.3.1",
12
- "@graphql-tools/utils": "^8.0.2",
13
- "graphql-config": "^4.0.1",
9
+ "@babel/code-frame": "7.16.0",
10
+ "@graphql-tools/code-file-loader": "7.2.2",
11
+ "@graphql-tools/graphql-tag-pluck": "7.1.3",
12
+ "@graphql-tools/import": "6.6.1",
13
+ "@graphql-tools/utils": "8.5.3",
14
+ "graphql-config": "4.1.0",
14
15
  "graphql-depth-limit": "1.1.0",
15
- "lodash.lowercase": "^4.3.0"
16
+ "lodash.lowercase": "4.3.0"
16
17
  },
17
18
  "repository": "https://github.com/dotansimha/graphql-eslint",
18
19
  "author": "Dotan Simha <dotansimha@gmail.com>",