@graphql-eslint/eslint-plugin 3.13.1-alpha-20221105230632-80b7067 → 3.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. package/README.md +266 -0
  2. package/docs/README.md +76 -0
  3. package/docs/custom-rules.md +148 -0
  4. package/docs/deprecated-rules.md +21 -0
  5. package/docs/parser-options.md +85 -0
  6. package/docs/parser.md +49 -0
  7. package/docs/rules/alphabetize.md +178 -0
  8. package/docs/rules/description-style.md +54 -0
  9. package/docs/rules/executable-definitions.md +17 -0
  10. package/docs/rules/fields-on-correct-type.md +17 -0
  11. package/docs/rules/fragments-on-composite-type.md +17 -0
  12. package/docs/rules/input-name.md +76 -0
  13. package/docs/rules/known-argument-names.md +17 -0
  14. package/docs/rules/known-directives.md +44 -0
  15. package/docs/rules/known-fragment-names.md +69 -0
  16. package/docs/rules/known-type-names.md +17 -0
  17. package/docs/rules/lone-anonymous-operation.md +17 -0
  18. package/docs/rules/lone-schema-definition.md +17 -0
  19. package/docs/rules/match-document-filename.md +156 -0
  20. package/docs/rules/naming-convention.md +300 -0
  21. package/docs/rules/no-anonymous-operations.md +39 -0
  22. package/docs/rules/no-case-insensitive-enum-values-duplicates.md +43 -0
  23. package/docs/rules/no-deprecated.md +85 -0
  24. package/docs/rules/no-duplicate-fields.md +65 -0
  25. package/docs/rules/no-fragment-cycles.md +17 -0
  26. package/docs/rules/no-hashtag-description.md +59 -0
  27. package/docs/rules/no-root-type.md +53 -0
  28. package/docs/rules/no-scalar-result-type-on-mutation.md +37 -0
  29. package/docs/rules/no-typename-prefix.md +39 -0
  30. package/docs/rules/no-undefined-variables.md +17 -0
  31. package/docs/rules/no-unreachable-types.md +49 -0
  32. package/docs/rules/no-unused-fields.md +62 -0
  33. package/docs/rules/no-unused-fragments.md +17 -0
  34. package/docs/rules/no-unused-variables.md +17 -0
  35. package/docs/rules/one-field-subscriptions.md +17 -0
  36. package/docs/rules/overlapping-fields-can-be-merged.md +17 -0
  37. package/docs/rules/possible-fragment-spread.md +17 -0
  38. package/docs/rules/possible-type-extension.md +15 -0
  39. package/docs/rules/provided-required-arguments.md +17 -0
  40. package/docs/rules/relay-arguments.md +57 -0
  41. package/docs/rules/relay-connection-types.md +42 -0
  42. package/docs/rules/relay-edge-types.md +56 -0
  43. package/docs/rules/relay-page-info.md +32 -0
  44. package/docs/rules/require-deprecation-date.md +57 -0
  45. package/docs/rules/require-deprecation-reason.md +47 -0
  46. package/docs/rules/require-description.md +115 -0
  47. package/docs/rules/require-field-of-type-query-in-mutation-result.md +47 -0
  48. package/docs/rules/require-id-when-available.md +88 -0
  49. package/docs/rules/scalar-leafs.md +17 -0
  50. package/docs/rules/selection-set-depth.md +76 -0
  51. package/docs/rules/strict-id-in-types.md +130 -0
  52. package/docs/rules/unique-argument-names.md +17 -0
  53. package/docs/rules/unique-directive-names-per-location.md +17 -0
  54. package/docs/rules/unique-directive-names.md +17 -0
  55. package/docs/rules/unique-enum-value-names.md +15 -0
  56. package/docs/rules/unique-field-definition-names.md +17 -0
  57. package/docs/rules/unique-fragment-name.md +51 -0
  58. package/docs/rules/unique-input-field-names.md +17 -0
  59. package/docs/rules/unique-operation-name.md +55 -0
  60. package/docs/rules/unique-operation-types.md +17 -0
  61. package/docs/rules/unique-type-names.md +17 -0
  62. package/docs/rules/unique-variable-names.md +17 -0
  63. package/docs/rules/value-literals-of-correct-type.md +17 -0
  64. package/docs/rules/variables-are-input-types.md +17 -0
  65. package/docs/rules/variables-in-allowed-position.md +17 -0
  66. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,266 @@
1
+ This project integrates GraphQL and ESLint, for a better developer experience.
2
+
3
+ <img height="150" src="./logo.png">
4
+
5
+ [![npm version](https://badge.fury.io/js/%40graphql-eslint%2Feslint-plugin.svg)](https://badge.fury.io/js/%40graphql-eslint%2Feslint-plugin)
6
+
7
+ > Created and maintained by [The Guild](https://the-guild.dev)
8
+
9
+ ## Key Features
10
+
11
+ - 🚀 Integrates with ESLint core (as a ESTree parser)
12
+ - 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments
13
+ - 🚀 Lints both GraphQL schema and GraphQL operations
14
+ - 🚀 Extended type info for more advanced usages
15
+ - 🚀 Supports ESLint directives (for example: `eslint-disable-next-line`)
16
+ - 🚀 Easily extendable - supports custom rules based on GraphQL's AST and ESLint API
17
+ - 🚀 Validates, lints, prettifies and checks for best practices across GraphQL schema and GraphQL operations
18
+ - 🚀 Integrates with [`graphql-config`](https://graphql-config.com)
19
+ - 🚀 Integrates and visualizes lint issues in popular IDEs (VSCode / WebStorm)
20
+
21
+ > Special thanks to [ilyavolodin](https://github.com/ilyavolodin) for his work on a similar project!
22
+
23
+ <img src="https://thumbs.gfycat.com/ActualTerrificDog-size_restricted.gif" />
24
+
25
+ ## Getting Started
26
+
27
+ - [Introducing GraphQL-ESLint!](https://the-guild.dev/blog/introducing-graphql-eslint) @ `the-guild.dev`
28
+
29
+ ### Installation
30
+
31
+ Start by installing the plugin package, which includes everything you need:
32
+
33
+ ```sh
34
+ yarn add -D @graphql-eslint/eslint-plugin
35
+ ```
36
+
37
+ Or, with NPM:
38
+
39
+ ```sh
40
+ npm install --save-dev @graphql-eslint/eslint-plugin
41
+ ```
42
+
43
+ > Make sure you have `graphql` dependency in your project.
44
+
45
+ ## Configuration
46
+
47
+ 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.
48
+
49
+ > 🚨 Important! This step is necessary even if you are declaring operations and/or schema in code files.
50
+
51
+ ```json
52
+ {
53
+ "overrides": [
54
+ {
55
+ "files": ["*.graphql"],
56
+ "parser": "@graphql-eslint/eslint-plugin",
57
+ "plugins": ["@graphql-eslint"],
58
+ "rules": {
59
+ "@graphql-eslint/known-type-names": "error"
60
+ }
61
+ }
62
+ ]
63
+ }
64
+ ```
65
+
66
+ 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](#extended-linting-rules-with-graphql-schema).
67
+
68
+ ### Apply this plugin to GraphQL definitions defined in code files
69
+
70
+ 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.
71
+
72
+ ```diff
73
+ {
74
+ "overrides": [
75
+ + {
76
+ + "files": ["*.js"],
77
+ + "processor": "@graphql-eslint/graphql"
78
+ + },
79
+ {
80
+ "files": ["*.graphql"],
81
+ "parser": "@graphql-eslint/eslint-plugin",
82
+ "plugins": ["@graphql-eslint"],
83
+ "rules": {
84
+ "@graphql-eslint/known-type-names": "error"
85
+ }
86
+ }
87
+ ]
88
+ }
89
+ ```
90
+
91
+ 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.
92
+
93
+ ### Extended linting rules with GraphQL Schema
94
+
95
+ Some rules require an understanding of the entire schema at once. For example, [no-unreachable-types](https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/no-unreachable-types.md#no-unreachable-types) checks that all types are reachable by root-level fields.
96
+
97
+ To use these rules, you'll need to tell ESLint how to identify the entire set of schema definitions.
98
+
99
+ 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!
100
+
101
+ Alternatively, you can define `parserOptions.schema` in the `*.graphql` override in your ESLint config.
102
+
103
+ 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:
104
+
105
+ ```diff
106
+ {
107
+ "files": ["*.graphql"],
108
+ "parser": "@graphql-eslint/eslint-plugin",
109
+ "plugins": ["@graphql-eslint"],
110
+ "rules": {
111
+ "@graphql-eslint/no-unreachable-types": "error"
112
+ },
113
+ + "parserOptions": {
114
+ + "schema": "./schema.graphql"
115
+ + }
116
+ }
117
+ ```
118
+
119
+ > You can find a complete [documentation of the `parserOptions` here](docs/parser-options.md).
120
+
121
+ > Some rules require type information to operate, it's marked in the docs for each rule!
122
+
123
+ ### Extended linting rules with siblings operations
124
+
125
+ While implementing this tool, we had to find solutions for a better integration of the GraphQL ecosystem and ESLint core.
126
+
127
+ 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.
128
+
129
+ To workaround that, we allow you to provide additional information on your GraphQL operations, making it available for rules while doing the actual linting.
130
+
131
+ 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:
132
+
133
+ ```diff
134
+ {
135
+ "files": ["*.graphql"],
136
+ "parser": "@graphql-eslint/eslint-plugin",
137
+ "plugins": ["@graphql-eslint"],
138
+ "rules": {
139
+ "@graphql-eslint/unique-operation-name": "error"
140
+ },
141
+ "parserOptions": {
142
+ + "operations": "./src/**/*.graphql",
143
+ "schema": "./schema.graphql"
144
+ }
145
+ }
146
+ ```
147
+
148
+ ### VSCode Integration
149
+
150
+ Use [ESLint VSCode extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) to integrate ESLint into VSCode.
151
+
152
+ For syntax highlighting you need a GraphQL extension (which may potentially have its own linting), for example [GraphQL (by GraphQL Foundation)](https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql).
153
+
154
+ ### Disabling Rules
155
+
156
+ The `graphql-eslint` parser looks for GraphQL comments syntax (marked with `#`) and will send it to ESLint as directives. That means, you can use ESLint directives syntax to hint ESLint, just like in any other type of files.
157
+
158
+ To disable ESLint for a specific line, you can do:
159
+
160
+ ```graphql
161
+ # eslint-disable-next-line
162
+ type Query {
163
+ foo: String!
164
+ }
165
+ ```
166
+
167
+ You can also specify specific rules to disable, apply it over the entire file, `eslint-disable-next-line` or current `eslint-disable-line`.
168
+
169
+ You can find a list of [ESLint directives here](https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments).
170
+
171
+ ## Available Rules
172
+
173
+ You can find a complete list of [all available rules here](docs/README.md).
174
+
175
+ ### Deprecated Rules
176
+
177
+ See [docs/deprecated-rules.md](docs/deprecated-rules.md).
178
+
179
+ ## Available Configs
180
+
181
+ <!-- prettier-ignore-start -->
182
+ |Name|Description|
183
+ |:-:|-|
184
+ |[`schema-recommended`](packages/plugin/src/configs/schema-recommended.json)|enables recommended rules for schema (SDL) development|
185
+ |[`schema-all`](packages/plugin/src/configs/schema-all.json)|enables all rules for schema (SDL) development, except for those that require `parserOptions.operations` option|
186
+ |[`operations-recommended`](packages/plugin/src/configs/operations-recommended.json) |enables recommended rules for consuming GraphQL (operations) development|
187
+ |[`operations-all`](packages/plugin/src/configs/operations-all.json)|enables all rules for consuming GraphQL (operations) development|
188
+ |[`relay`](packages/plugin/src/configs/relay.json)|enables rules from Relay specification for schema (SDL) development|
189
+ <!-- prettier-ignore-end -->
190
+
191
+ > If you are in a project that develops the GraphQL schema, you'll need `schema` rules.
192
+
193
+ > If you are in a project that develops GraphQL operations (query/mutation/subscription), you'll need `operations` rules.
194
+
195
+ > If you are in a monorepo project, you probably need both sets of rules, see [example of configuration](examples/monorepo/.eslintrc.cjs).
196
+
197
+ ### Config usage
198
+
199
+ For example, to enable the `schema-recommended` config, enable it in your `.eslintrc` file with the `extends` option:
200
+
201
+ > All configs under the hood set `parser` as `@graphql-eslint/eslint-plugin` and add `@graphql-eslint` to `plugins` array, so you don't need to specify them.
202
+
203
+ ```diff
204
+ {
205
+ "overrides": [
206
+ {
207
+ "files": ["*.js"],
208
+ "processor": "@graphql-eslint/graphql"
209
+ },
210
+ {
211
+ "files": ["*.graphql"],
212
+ - "parser": "@graphql-eslint/eslint-plugin",
213
+ - "plugins": ["@graphql-eslint"],
214
+ + "extends": "plugin:@graphql-eslint/schema-recommended"
215
+ }
216
+ ]
217
+ }
218
+ ```
219
+
220
+ ### `prettier` rule
221
+
222
+ `eslint-plugin-prettier` supports `.graphql` files, and `v4.1.0` supports `graphql` blocks even better. You need to do the following:
223
+
224
+ ```js
225
+ module.exports = {
226
+ overrides: [
227
+ {
228
+ files: ['*.js'],
229
+ processor: '@graphql-eslint/graphql',
230
+ extends: ['plugin:prettier/recommended']
231
+ },
232
+ {
233
+ files: ['*.graphql'],
234
+ parser: '@graphql-eslint/eslint-plugin',
235
+ plugins: ['@graphql-eslint'],
236
+ rules: {
237
+ 'prettier/prettier': 'error'
238
+ }
239
+ }
240
+ ]
241
+ }
242
+ ```
243
+
244
+ You can take [`examples/prettier`](examples/prettier/.eslintrc.cjs) as example.
245
+
246
+ ## Further Reading
247
+
248
+ If you wish to learn more about this project, how the parser works, how to add custom rules and more please refer to the below links:
249
+
250
+ - [Writing Custom Rules](docs/custom-rules.md)
251
+ - [How the parser works?](docs/parser.md)
252
+ - [`parserOptions`](docs/parser-options.md)
253
+
254
+ ## Contributions
255
+
256
+ Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!
257
+
258
+ And if this is your first time contributing to this project, please do read our [Contributor Workflow Guide](https://github.com/the-guild-org/Stack/blob/master/CONTRIBUTING.md) before you get started off.
259
+
260
+ ### Code of Conduct
261
+
262
+ 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).
263
+
264
+ ## License
265
+
266
+ Released under the [MIT license](LICENSE).
package/docs/README.md ADDED
@@ -0,0 +1,76 @@
1
+ ## Available Rules
2
+
3
+ Each rule has emojis denoting:
4
+
5
+ - 🚀 `graphql-eslint` rule
6
+ - 🔮 `graphql-js` rule
7
+ - 🔧 if some problems reported by the rule are automatically fixable by the `--fix` [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) option
8
+ - 💡 if some problems reported by the rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
9
+
10
+ <!-- 🚨 IMPORTANT! Do not manually modify this table. Run: `yarn generate:docs` -->
11
+ <!-- prettier-ignore-start -->
12
+ Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|Description|&nbsp;&nbsp;&nbsp;&nbsp;Config&nbsp;&nbsp;&nbsp;&nbsp;|🚀&nbsp;/&nbsp;🔮|🔧&nbsp;/&nbsp;💡
13
+ -|-|:-:|:-:|:-:
14
+ [alphabetize](rules/alphabetize.md)|Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.|![all][]|🚀|🔧
15
+ [description-style](rules/description-style.md)|Require all comments to follow the same style (either block or inline).|![recommended][]|🚀|💡
16
+ [executable-definitions](rules/executable-definitions.md)|A GraphQL document is only valid for execution if all definitions are either operation or fragment definitions.|![recommended][]|🔮|
17
+ [fields-on-correct-type](rules/fields-on-correct-type.md)|A GraphQL document is only valid if all fields selected are defined by the parent type, or are an allowed meta field such as `__typename`.|![recommended][]|🔮|
18
+ [fragments-on-composite-type](rules/fragments-on-composite-type.md)|Fragments use a type condition to determine if they apply, since fragments can only be spread into a composite type (object, interface, or union), the type condition must also be a composite type.|![recommended][]|🔮|
19
+ [input-name](rules/input-name.md)|Require mutation argument to be always called "input" and input type to be called Mutation name + "Input".|![all][]|🚀|💡
20
+ [known-argument-names](rules/known-argument-names.md)|A GraphQL field is only valid if all supplied arguments are defined by that field.|![recommended][]|🔮|
21
+ [known-directives](rules/known-directives.md)|A GraphQL document is only valid if all `@directive`s are known by the schema and legally positioned.|![recommended][]|🔮|
22
+ [known-fragment-names](rules/known-fragment-names.md)|A GraphQL document is only valid if all `...Fragment` fragment spreads refer to fragments defined in the same document.|![recommended][]|🔮|
23
+ [known-type-names](rules/known-type-names.md)|A GraphQL document is only valid if referenced types (specifically variable definitions and fragment conditions) are defined by the type schema.|![recommended][]|🔮|
24
+ [lone-anonymous-operation](rules/lone-anonymous-operation.md)|A GraphQL document is only valid if when it contains an anonymous operation (the query short-hand) that it contains only that one operation definition.|![recommended][]|🔮|
25
+ [lone-schema-definition](rules/lone-schema-definition.md)|A GraphQL document is only valid if it contains only one schema definition.|![recommended][]|🔮|
26
+ [match-document-filename](rules/match-document-filename.md)|This rule allows you to enforce that the file name should match the operation name.|![all][]|🚀|
27
+ [naming-convention](rules/naming-convention.md)|Require names to follow specified conventions.|![recommended][]|🚀|💡
28
+ [no-anonymous-operations](rules/no-anonymous-operations.md)|Require name for your GraphQL operations. This is useful since most GraphQL client libraries are using the operation name for caching purposes.|![recommended][]|🚀|💡
29
+ [no-case-insensitive-enum-values-duplicates](rules/no-case-insensitive-enum-values-duplicates.md)|Disallow case-insensitive enum values duplicates.|![recommended][]|🚀|💡
30
+ [no-deprecated](rules/no-deprecated.md)|Enforce that deprecated fields or enum values are not in use by operations.|![recommended][]|🚀|💡
31
+ [no-duplicate-fields](rules/no-duplicate-fields.md)|Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.|![recommended][]|🚀|💡
32
+ [no-fragment-cycles](rules/no-fragment-cycles.md)|A GraphQL fragment is only valid when it does not have cycles in fragments usage.|![recommended][]|🔮|
33
+ [no-hashtag-description](rules/no-hashtag-description.md)|Requires to use `"""` or `"` for adding a GraphQL description instead of `#`.|![recommended][]|🚀|💡
34
+ [no-root-type](rules/no-root-type.md)|Disallow using root types `mutation` and/or `subscription`.||🚀|💡
35
+ [no-scalar-result-type-on-mutation](rules/no-scalar-result-type-on-mutation.md)|Avoid scalar result type on mutation type to make sure to return a valid state.|![all][]|🚀|💡
36
+ [no-typename-prefix](rules/no-typename-prefix.md)|Enforces users to avoid using the type name in a field name while defining your schema.|![recommended][]|🚀|💡
37
+ [no-undefined-variables](rules/no-undefined-variables.md)|A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.|![recommended][]|🔮|
38
+ [no-unreachable-types](rules/no-unreachable-types.md)|Requires all types to be reachable at some level by root level fields.|![recommended][]|🚀|💡
39
+ [no-unused-fields](rules/no-unused-fields.md)|Requires all fields to be used at some level by siblings operations.||🚀|💡
40
+ [no-unused-fragments](rules/no-unused-fragments.md)|A GraphQL document is only valid if all fragment definitions are spread within operations, or spread within other fragments spread within operations.|![recommended][]|🔮|
41
+ [no-unused-variables](rules/no-unused-variables.md)|A GraphQL operation is only valid if all variables defined by an operation are used, either directly or within a spread fragment.|![recommended][]|🔮|
42
+ [one-field-subscriptions](rules/one-field-subscriptions.md)|A GraphQL subscription is valid only if it contains a single root field.|![recommended][]|🔮|
43
+ [overlapping-fields-can-be-merged](rules/overlapping-fields-can-be-merged.md)|A selection set is only valid if all fields (including spreading any fragments) either correspond to distinct response names or can be merged without ambiguity.|![recommended][]|🔮|
44
+ [possible-fragment-spread](rules/possible-fragment-spread.md)|A fragment spread is only valid if the type condition could ever possibly be true: if there is a non-empty intersection of the possible parent types, and possible types which pass the type condition.|![recommended][]|🔮|
45
+ [possible-type-extension](rules/possible-type-extension.md)|A type extension is only valid if the type is defined and has the same kind.||🔮|
46
+ [provided-required-arguments](rules/provided-required-arguments.md)|A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.|![recommended][]|🔮|
47
+ [relay-arguments](rules/relay-arguments.md)|Set of rules to follow Relay specification for Arguments.||🚀|
48
+ [relay-connection-types](rules/relay-connection-types.md)|Set of rules to follow Relay specification for Connection types.||🚀|
49
+ [relay-edge-types](rules/relay-edge-types.md)|Set of rules to follow Relay specification for Edge types.||🚀|
50
+ [relay-page-info](rules/relay-page-info.md)|Set of rules to follow Relay specification for `PageInfo` object.||🚀|
51
+ [require-deprecation-date](rules/require-deprecation-date.md)|Require deletion date on `@deprecated` directive. Suggest removing deprecated things after deprecated date.|![all][]|🚀|💡
52
+ [require-deprecation-reason](rules/require-deprecation-reason.md)|Require all deprecation directives to specify a reason.|![recommended][]|🚀|
53
+ [require-description](rules/require-description.md)|Enforce descriptions in type definitions and operations.|![recommended][]|🚀|
54
+ [require-field-of-type-query-in-mutation-result](rules/require-field-of-type-query-in-mutation-result.md)|Allow the client in one round-trip not only to call mutation but also to get a wagon of data to update their application.|![all][]|🚀|
55
+ [require-id-when-available](rules/require-id-when-available.md)|Enforce selecting specific fields when they are available on the GraphQL type.|![recommended][]|🚀|💡
56
+ [scalar-leafs](rules/scalar-leafs.md)|A GraphQL document is valid only if all leaf fields (fields without sub selections) are of scalar or enum types.|![recommended][]|🔮|
57
+ [selection-set-depth](rules/selection-set-depth.md)|Limit the complexity of the GraphQL operations solely by their depth. Based on [graphql-depth-limit](https://npmjs.com/package/graphql-depth-limit).|![recommended][]|🚀|💡
58
+ [strict-id-in-types](rules/strict-id-in-types.md)|Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.|![recommended][]|🚀|
59
+ [unique-argument-names](rules/unique-argument-names.md)|A GraphQL field or directive is only valid if all supplied arguments are uniquely named.|![recommended][]|🔮|
60
+ [unique-directive-names](rules/unique-directive-names.md)|A GraphQL document is only valid if all defined directives have unique names.|![recommended][]|🔮|
61
+ [unique-directive-names-per-location](rules/unique-directive-names-per-location.md)|A GraphQL document is only valid if all non-repeatable directives at a given location are uniquely named.|![recommended][]|🔮|
62
+ [unique-enum-value-names](rules/unique-enum-value-names.md)|A GraphQL enum type is only valid if all its values are uniquely named.||🔮|
63
+ [unique-field-definition-names](rules/unique-field-definition-names.md)|A GraphQL complex type is only valid if all its fields are uniquely named.|![recommended][]|🔮|
64
+ [unique-fragment-name](rules/unique-fragment-name.md)|Enforce unique fragment names across your project.|![all][]|🚀|
65
+ [unique-input-field-names](rules/unique-input-field-names.md)|A GraphQL input object value is only valid if all supplied fields are uniquely named.|![recommended][]|🔮|
66
+ [unique-operation-name](rules/unique-operation-name.md)|Enforce unique operation names across your project.|![all][]|🚀|
67
+ [unique-operation-types](rules/unique-operation-types.md)|A GraphQL document is only valid if it has only one type per operation.|![recommended][]|🔮|
68
+ [unique-type-names](rules/unique-type-names.md)|A GraphQL document is only valid if all defined types have unique names.|![recommended][]|🔮|
69
+ [unique-variable-names](rules/unique-variable-names.md)|A GraphQL operation is only valid if all its variables are uniquely named.|![recommended][]|🔮|
70
+ [value-literals-of-correct-type](rules/value-literals-of-correct-type.md)|A GraphQL document is only valid if all value literals are of the type expected at their position.|![recommended][]|🔮|
71
+ [variables-are-input-types](rules/variables-are-input-types.md)|A GraphQL operation is only valid if all the variables it defines are of input types (scalar, enum, or input object).|![recommended][]|🔮|
72
+ [variables-in-allowed-position](rules/variables-in-allowed-position.md)|Variables passed to field arguments conform to type.|![recommended][]|🔮|
73
+ <!-- prettier-ignore-end -->
74
+
75
+ [recommended]: https://img.shields.io/badge/-recommended-green.svg
76
+ [all]: https://img.shields.io/badge/-all-blue.svg
@@ -0,0 +1,148 @@
1
+ # Writing Custom Rules
2
+
3
+ To get started with your own rules, start by understanding how [ESLint custom rules works](https://eslint.org/docs/developer-guide/working-with-rules).
4
+
5
+ `graphql-eslint` converts the [GraphQL AST](https://graphql.org/graphql-js/language) into [ESTree structure](https://github.com/estree/estree), so it allows you to easily travel the GraphQL AST tree easily.
6
+
7
+ You can visit any GraphQL AST node in your custom rules, and report this as error. You don't need to have special handlers for code-files, since `graphql-eslint` extracts usages of `gql` and magic `/* GraphQL */` comments automatically, and runs it through the parser, and eventually it knows to adjust errors location to fit in your code files original location.
8
+
9
+ ## Getting Started
10
+
11
+ Start by creating a [simple ESLint rule file](https://eslint.org/docs/developer-guide/working-with-rules), and choose the AST nodes you wish to visit. It can either be a [simple AST node `Kind`](https://github.com/graphql/graphql-js/blob/master/src/language/kinds.d.ts) or a complex [ESLint selector](https://eslint.org/docs/developer-guide/selectors) that allows you to travel and filter AST nodes.
12
+
13
+ We recommend you to read the [graphql-eslint parser documentation](parser.md) before getting started, to understand the differences between the AST structures.
14
+
15
+ The `graphql-eslint` comes with a TypeScript wrapper for ESLint rules, and provides a testkit to simplify testing process with GraphQL schemas, so you can use that by importing `GraphQLESLintRule` type. But if you wish to use JavaScript - that's fine :)
16
+
17
+ Here's an example for a simple rule that reports on anonymous GraphQL operations:
18
+
19
+ ```ts
20
+ import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin'
21
+
22
+ const rule: GraphQLESLintRule = {
23
+ create(context) {
24
+ return {
25
+ OperationDefinition(node) {
26
+ if (!node.name || !node.name.value) {
27
+ context.report({
28
+ node,
29
+ message: 'Oops, name is required!'
30
+ })
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ So what happens here?
39
+
40
+ 1. `@graphql-eslint/eslint-plugin` handles the parsing process for your GraphQL content. It will load the GraphQL files (either from code files or from `.graphql` files with SDL), parse it using GraphQL parser, converts it to ESTree structure and let ESLint do the rest.
41
+ 1. Your rule is being loaded by ESLint, and executes just like any other ESLint rule.
42
+ 1. Our custom rule asks ESLint to run our function for every `OperationDefinition` found.
43
+ 1. If the `OperationDefinition` node doesn't have a valid `name` - we report an error to ESLint.
44
+
45
+ #### More Examples
46
+
47
+ You can scan the `packages/plugin/src/rules` directory in this repo for references for implementing rules. It coverts most of the use-cases and concepts of rules.
48
+
49
+ ## Accessing original GraphQL AST nodes
50
+
51
+ Since our parser converts GraphQL AST to ESTree structure, there are some minor differences in the structure of the objects.
52
+ If you are using TypeScript, and you typed your rule with `GraphQLESLintRule` - you'll see that each `node` is a bit different from the AST nodes of GraphQL (you can read more about that in [graphql-eslint parser documentation](parser.md)).
53
+
54
+ If you need access to the original GraphQL AST `node`, you can use `.rawNode()` method on each node you get from the AST structure of ESLint.
55
+
56
+ This is useful if you wish to use other GraphQL tools that works with the original GraphQL AST objects.
57
+
58
+ Here's an example for using original `graphql-js` validate method to validate `OperationDefinition`:
59
+
60
+ ```ts
61
+ import { validate } from 'graphql'
62
+ import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
63
+
64
+ export const rule = {
65
+ create(context) {
66
+ return {
67
+ OperationDefinition(node) {
68
+ const schema = requireGraphQLSchemaFromContext(context)
69
+
70
+ validate(context, schema, {
71
+ kind: Kind.DOCUMENT,
72
+ definitions: [node.rawNode()]
73
+ })
74
+ }
75
+ }
76
+ }
77
+ }
78
+ ```
79
+
80
+ ## `TypeInfo` / `GraphQLSchema`
81
+
82
+ If you provide GraphQL schema in your ESLint configuration, it will get loaded automatically, and become available in your rules in two ways:
83
+
84
+ 1. You'll be able to access the loaded `GraphQLSchema` object.
85
+ 2. In every visited node, you'll be able to use `.typeInfo()` method to get an object with complete type information on your visited node (see [TypeInfo documentation](https://graphql.org/graphql-js/utilities/#typeinfo)).
86
+
87
+ #### Getting `GraphQLSchema`
88
+
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
+
91
+ ```ts
92
+ const schema = requireGraphQLSchemaFromContext(context)
93
+ ```
94
+
95
+ #### Accessing TypeInfo
96
+
97
+ If schema is provided and loaded successfully, the `typeInfo` will be available to use. Otherwise - it will be `undefined`.
98
+ If your plugin requires `typeInfo` in order to operate and run, make sure to call `requireGraphQLSchemaFromContext` - it will validate that the schema is loaded.
99
+
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
+
102
+ ```ts
103
+ import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
104
+
105
+ export const rule = {
106
+ create(context) {
107
+ requireGraphQLSchemaFromContext('your-rule-name', context)
108
+
109
+ return {
110
+ SelectionSet(node) {
111
+ const typeInfo = node.typeInfo()
112
+ if (typeInfo.gqlType) {
113
+ console.log(`The GraphQLOutputType is: ${typeInfo.gqlType}`)
114
+ }
115
+ }
116
+ }
117
+ }
118
+ }
119
+ ```
120
+
121
+ The structure of the return value of `.typeInfo()` is [defined here](https://github.com/B2o5T/graphql-eslint/blob/master/packages/plugin/src/estree-converter/converter.ts#L32-L40). So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.
122
+
123
+ ## Testing your rules
124
+
125
+ To test your rules, you can either use the wrapped `GraphQLRuleTester` from this library, or use the built-it [`RuleTester`](https://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests) of ESLint.
126
+
127
+ The wrapped `GraphQLRuleTester` provides built-in configured parser, and a schema loader, if you need to test your rule with a loaded schema.
128
+
129
+ ```ts
130
+ import { GraphQLRuleTester } from '@graphql-eslint/eslint-plugin'
131
+ import { rule } from './my-rule'
132
+
133
+ const ruleTester = new GraphQLRuleTester()
134
+
135
+ ruleTester.runGraphQLTests('my-rule', rule, {
136
+ valid: [
137
+ {
138
+ code: 'query something { foo }'
139
+ }
140
+ ],
141
+ invalid: [
142
+ {
143
+ code: 'query invalid { foo }',
144
+ errors: [{ message: 'Your error message.' }]
145
+ }
146
+ ]
147
+ })
148
+ ```
@@ -0,0 +1,21 @@
1
+ # Deprecated Rules
2
+
3
+ ## avoid-duplicate-fields
4
+
5
+ This rule was renamed to [`no-duplicate-fields`](rules/no-duplicate-fields.md).
6
+
7
+ ## avoid-scalar-result-type-on-mutation
8
+
9
+ This rule was renamed to [`no-scalar-result-type-on-mutation`](rules/no-scalar-result-type-on-mutation.md).
10
+
11
+ ## avoid-typename-prefix
12
+
13
+ This rule was renamed to [`no-typename-prefix`](rules/no-typename-prefix.md).
14
+
15
+ ## avoid-operation-name-prefix
16
+
17
+ This rule was removed because the same things can be validated using [`naming-convention`](rules/naming-convention.md).
18
+
19
+ ## no-operation-name-suffix
20
+
21
+ This rule was removed because the same things can be validated using [`naming-convention`](rules/naming-convention.md).
@@ -0,0 +1,85 @@
1
+ ## Parser Options
2
+
3
+ ### `graphQLParserOptions`
4
+
5
+ With this configuration, you can specify custom configurations for GraphQL's `parse` method. By default, `graphql-eslint` parser just adds `noLocation: false` to make sure all parsed AST has `location` set, since we need this for tokenizing and for converting the GraphQL AST into ESTree.
6
+
7
+ You can find the [complete set of options for this object here](https://github.com/graphql/graphql-js/blob/6e48d16f92b9a6df8638b1486354c6be2537033b/src/language/parser.ts#L73)
8
+
9
+ ### `skipGraphQLConfig`
10
+
11
+ If you are using [`graphql-config`](https://graphql-config.com) in your project, the parser will automatically use that to load your default GraphQL schema.
12
+
13
+ You can disable this behaviour using `skipGraphQLConfig: true` in the `parserOptions`:
14
+
15
+ ```json
16
+ "parserOptions": {
17
+ "skipGraphQLConfig": true
18
+ }
19
+ ```
20
+
21
+ ### `schema`
22
+
23
+ You can specify `parserOptions.schema` to load your GraphQL schema. The parser uses `graphql-tools` and it's loaders, that means you can either specify a URL, a path to a local `.json` (introspection) file, or a path to a local `.graphql` file(s). You can also use Glob expressions to load multiple files.
24
+
25
+ Here are a few examples for a valid setup:
26
+
27
+ ```json
28
+ "parserOptions": {
29
+ "schema": "./schema.graphql"
30
+ }
31
+ ```
32
+
33
+ ```json
34
+ "parserOptions": {
35
+ "schema": "./schema.json"
36
+ }
37
+ ```
38
+
39
+ ```json
40
+ "parserOptions": {
41
+ "schema": "http://my-server/graphql"
42
+ }
43
+ ```
44
+
45
+ ```json
46
+ "parserOptions": {
47
+ "schema": "./src/**/*.graphql"
48
+ }
49
+ ```
50
+
51
+ ```json
52
+ "parserOptions": {
53
+ "schema": [
54
+ "src/schema-a.graphql",
55
+ "src/schema-b.graphql",
56
+ "src/schema-c.graphql"
57
+ ]
58
+ }
59
+ ```
60
+
61
+ ### `schemaOptions`
62
+
63
+ If you wish to send additional configuration for the `graphql-tools` loaders that loads your schema, you can specify `schemaOptions` object:
64
+
65
+ ```json
66
+ "parserOptions": {
67
+ "schema": "http://my-server/graphql",
68
+ "schemaOptions": {
69
+ "headers": {
70
+ "Authorization": "Bearer MY_TOKEN"
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
76
+ ```json
77
+ "parserOptions": {
78
+ "schema": "./src/**/*.graphql",
79
+ "schemaOptions": {
80
+ "assumeValid": true
81
+ }
82
+ }
83
+ ```
84
+
85
+ > The configuration here is flexible, and will be sent to `graphql-tools` and it's loaders. So depends on the schema source, the options may vary. [You can read more about these loaders and their configuration here](https://graphql-tools.com/docs/api/interfaces/loaders_graphql_file_src.GraphQLFileLoaderOptions#properties).
package/docs/parser.md ADDED
@@ -0,0 +1,49 @@
1
+ ## GraphQL-ESLint Parser
2
+
3
+ The `graphql-eslint` parser is works in the following way:
4
+
5
+ 1. Loads all relevant GraphQL code using ESLint core (either from `.graphql` files, or using [ESLint `processor`](https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins) to find in code-files).
6
+ 1. Is uses `graphql-js` (and `graphql-tools`) to parse the found string into a `DocumentNode`.
7
+ 1. Extracts all comments (marked as `# ...`) from the parsed AST, and provides to ESLint as directives hints.
8
+ 1. If `graphql-config` is used, or `schema` field is provided, the schema is being loaded and provided to the rules using `parserServices`.
9
+ 1. Converts the `DocumentNode` to ESTree structure (and enrich the nodes with `typeInfo`, if schema is loaded).
10
+
11
+ ### ESTree Conversion
12
+
13
+ The GraphQL AST structure is very similar to ESTree structure, but there are a few differences that the `parser` does.
14
+
15
+ Here's a list of changes that the parser performs, in order to make the GraphQL AST compatible with ESTree:
16
+
17
+ ---
18
+
19
+ **Problem**: GraphQL uses `kind` field to define the kind of the AST node, while ESTree uses `type`.
20
+
21
+ **Solution**: The parser adds `type` field on each node, and just copies the value from `kind` field.
22
+
23
+ ---
24
+
25
+ **Problem**: Some GraphQL AST nodes are using `type` field (which conflicts with the ESTree kind).
26
+
27
+ **Solution**: AST nodes that has `type` field are being transformed, and the `type` field changes to `gqlType`.
28
+
29
+ ---
30
+
31
+ **Problem**: GraphQL AST structure allows circular JSON links (while ESTree might fail on `Maximum call stack exceeded`).
32
+
33
+ **Solution**: The parser removes circular JSONs (specifically around GraphQL `Location` and the `Lexer`)
34
+
35
+ ---
36
+
37
+ **Problem**: GraphQL uses `location` field to store the AST locations, while ESTree also uses it in a different structure.
38
+
39
+ **Solution**: The parser creates a new `location` field that is compatible with ESTree.
40
+
41
+ ### Loading GraphQL Schema
42
+
43
+ If you are using [`graphql-config`](https://graphql-config.com) in your project, the parser will automatically use that to load your default GraphQL schema (you can disable this behaviour using `skipGraphQLConfig: true` in the `parserOptions`).
44
+
45
+ If you are not using `graphql-config`, you can specify `parserOptions.schema` to load your GraphQL schema. The parser uses `graphql-tools` and it's loaders, that means you can either specify a URL, a path to a local `.json` (introspection) file, or a path to a local `.graphql` file(s). You can also use Glob expressions to load multiple files.
46
+
47
+ [You can find more detail on the `parserOptions` config here](parser-options.md)
48
+
49
+ Providing the schema will make sure that rules that needs it will be able to access it, and it enriches every converted AST node with `typeInfo`.