@graphql-eslint/eslint-plugin 3.14.4-alpha-20230111221804-f30b422 → 3.14.4-alpha-20230111225020-02d9c28
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 +5 -288
- package/cjs/documents.js +2 -105
- package/cjs/graphql-config.js +1 -1
- package/cjs/parser.js +9 -3
- package/cjs/rules/alphabetize.js +1 -1
- package/cjs/rules/description-style.js +1 -1
- package/cjs/rules/graphql-js-validation.js +1 -1
- package/cjs/rules/input-name.js +4 -4
- package/cjs/rules/lone-executable-definition.js +1 -1
- package/cjs/rules/match-document-filename.js +2 -3
- package/cjs/rules/naming-convention.js +1 -1
- package/cjs/rules/no-anonymous-operations.js +1 -1
- package/cjs/rules/no-case-insensitive-enum-values-duplicates.js +1 -1
- package/cjs/rules/no-deprecated.js +1 -1
- package/cjs/rules/no-duplicate-fields.js +1 -1
- package/cjs/rules/no-hashtag-description.js +1 -1
- package/cjs/rules/no-one-place-fragments.js +1 -1
- package/cjs/rules/no-root-type.js +1 -1
- package/cjs/rules/no-scalar-result-type-on-mutation.js +1 -1
- package/cjs/rules/no-typename-prefix.js +1 -1
- package/cjs/rules/no-unreachable-types.js +1 -1
- package/cjs/rules/no-unused-fields.js +1 -1
- package/cjs/rules/relay-arguments.js +4 -4
- package/cjs/rules/relay-connection-types.js +1 -1
- package/cjs/rules/relay-edge-types.js +1 -1
- package/cjs/rules/relay-page-info.js +1 -1
- package/cjs/rules/require-deprecation-date.js +1 -1
- package/cjs/rules/require-deprecation-reason.js +1 -1
- package/cjs/rules/require-description.js +1 -1
- package/cjs/rules/require-field-of-type-query-in-mutation-result.js +1 -1
- package/cjs/rules/require-id-when-available.js +1 -1
- package/cjs/rules/require-nullable-fields-with-oneof.js +1 -1
- package/cjs/rules/require-type-pattern-with-oneof.js +1 -1
- package/cjs/rules/selection-set-depth.js +1 -1
- package/cjs/rules/strict-id-in-types.js +1 -1
- package/cjs/rules/unique-fragment-name.js +1 -1
- package/cjs/rules/unique-operation-name.js +1 -1
- package/cjs/siblings.js +113 -0
- package/cjs/utils.js +3 -2
- package/docs/README.md +1 -85
- package/docs/custom-rules.md +1 -184
- package/docs/deprecated-rules.md +1 -24
- package/docs/parser-options.md +1 -107
- package/docs/parser.md +1 -67
- package/esm/documents.js +2 -105
- package/esm/graphql-config.js +1 -1
- package/esm/parser.js +10 -4
- package/esm/rules/alphabetize.js +1 -1
- package/esm/rules/description-style.js +1 -1
- package/esm/rules/graphql-js-validation.js +1 -1
- package/esm/rules/input-name.js +4 -4
- package/esm/rules/lone-executable-definition.js +1 -1
- package/esm/rules/match-document-filename.js +3 -4
- package/esm/rules/naming-convention.js +1 -1
- package/esm/rules/no-anonymous-operations.js +1 -1
- package/esm/rules/no-case-insensitive-enum-values-duplicates.js +1 -1
- package/esm/rules/no-deprecated.js +1 -1
- package/esm/rules/no-duplicate-fields.js +1 -1
- package/esm/rules/no-hashtag-description.js +1 -1
- package/esm/rules/no-one-place-fragments.js +1 -1
- package/esm/rules/no-root-type.js +1 -1
- package/esm/rules/no-scalar-result-type-on-mutation.js +1 -1
- package/esm/rules/no-typename-prefix.js +1 -1
- package/esm/rules/no-unreachable-types.js +1 -1
- package/esm/rules/no-unused-fields.js +1 -1
- package/esm/rules/relay-arguments.js +4 -4
- package/esm/rules/relay-connection-types.js +1 -1
- package/esm/rules/relay-edge-types.js +1 -1
- package/esm/rules/relay-page-info.js +1 -1
- package/esm/rules/require-deprecation-date.js +1 -1
- package/esm/rules/require-deprecation-reason.js +1 -1
- package/esm/rules/require-description.js +1 -1
- package/esm/rules/require-field-of-type-query-in-mutation-result.js +1 -1
- package/esm/rules/require-id-when-available.js +1 -1
- package/esm/rules/require-nullable-fields-with-oneof.js +1 -1
- package/esm/rules/require-type-pattern-with-oneof.js +1 -1
- package/esm/rules/selection-set-depth.js +1 -1
- package/esm/rules/strict-id-in-types.js +1 -1
- package/esm/rules/unique-fragment-name.js +1 -1
- package/esm/rules/unique-operation-name.js +1 -1
- package/esm/siblings.js +109 -0
- package/esm/utils.js +2 -1
- package/package.json +1 -1
- package/typings/documents.d.cts +2 -20
- package/typings/documents.d.ts +2 -20
- package/typings/rules/input-name.d.cts +1 -1
- package/typings/rules/input-name.d.ts +1 -1
- package/typings/siblings.d.cts +22 -0
- package/typings/siblings.d.ts +22 -0
- package/typings/types.d.cts +3 -2
- package/typings/types.d.ts +3 -2
- package/typings/utils.d.cts +2 -1
- package/typings/utils.d.ts +2 -1
- package/docs/rules/alphabetize.md +0 -194
- package/docs/rules/description-style.md +0 -57
- package/docs/rules/executable-definitions.md +0 -20
- package/docs/rules/fields-on-correct-type.md +0 -23
- package/docs/rules/fragments-on-composite-type.md +0 -20
- package/docs/rules/input-name.md +0 -80
- package/docs/rules/known-argument-names.md +0 -23
- package/docs/rules/known-directives.md +0 -48
- package/docs/rules/known-fragment-names.md +0 -72
- package/docs/rules/known-type-names.md +0 -24
- package/docs/rules/lone-anonymous-operation.md +0 -20
- package/docs/rules/lone-executable-definition.md +0 -59
- package/docs/rules/lone-schema-definition.md +0 -19
- package/docs/rules/match-document-filename.md +0 -181
- package/docs/rules/naming-convention.md +0 -320
- package/docs/rules/no-anonymous-operations.md +0 -43
- package/docs/rules/no-case-insensitive-enum-values-duplicates.md +0 -46
- package/docs/rules/no-deprecated.md +0 -88
- package/docs/rules/no-duplicate-fields.md +0 -69
- package/docs/rules/no-fragment-cycles.md +0 -19
- package/docs/rules/no-hashtag-description.md +0 -62
- package/docs/rules/no-one-place-fragments.md +0 -51
- package/docs/rules/no-root-type.md +0 -55
- package/docs/rules/no-scalar-result-type-on-mutation.md +0 -39
- package/docs/rules/no-typename-prefix.md +0 -42
- package/docs/rules/no-undefined-variables.md +0 -20
- package/docs/rules/no-unreachable-types.md +0 -52
- package/docs/rules/no-unused-fields.md +0 -64
- package/docs/rules/no-unused-fragments.md +0 -20
- package/docs/rules/no-unused-variables.md +0 -20
- package/docs/rules/one-field-subscriptions.md +0 -19
- package/docs/rules/overlapping-fields-can-be-merged.md +0 -20
- package/docs/rules/possible-fragment-spread.md +0 -21
- package/docs/rules/possible-type-extension.md +0 -19
- package/docs/rules/provided-required-arguments.md +0 -21
- package/docs/rules/relay-arguments.md +0 -59
- package/docs/rules/relay-connection-types.md +0 -43
- package/docs/rules/relay-edge-types.md +0 -60
- package/docs/rules/relay-page-info.md +0 -34
- package/docs/rules/require-deprecation-date.md +0 -59
- package/docs/rules/require-deprecation-reason.md +0 -49
- package/docs/rules/require-description.md +0 -147
- package/docs/rules/require-field-of-type-query-in-mutation-result.md +0 -50
- package/docs/rules/require-id-when-available.md +0 -91
- package/docs/rules/require-nullable-fields-with-oneof.md +0 -38
- package/docs/rules/require-type-pattern-with-oneof.md +0 -39
- package/docs/rules/scalar-leafs.md +0 -23
- package/docs/rules/selection-set-depth.md +0 -86
- package/docs/rules/strict-id-in-types.md +0 -129
- package/docs/rules/unique-argument-names.md +0 -19
- package/docs/rules/unique-directive-names-per-location.md +0 -21
- package/docs/rules/unique-directive-names.md +0 -19
- package/docs/rules/unique-enum-value-names.md +0 -16
- package/docs/rules/unique-field-definition-names.md +0 -19
- package/docs/rules/unique-fragment-name.md +0 -52
- package/docs/rules/unique-input-field-names.md +0 -19
- package/docs/rules/unique-operation-name.md +0 -56
- package/docs/rules/unique-operation-types.md +0 -19
- package/docs/rules/unique-type-names.md +0 -19
- package/docs/rules/unique-variable-names.md +0 -19
- package/docs/rules/value-literals-of-correct-type.md +0 -22
- package/docs/rules/variables-are-input-types.md +0 -20
- package/docs/rules/variables-in-allowed-position.md +0 -19
package/docs/custom-rules.md
CHANGED
@@ -1,184 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
To get started with your own rules, start by understanding how
|
4
|
-
[ESLint custom rules works](https://eslint.org/docs/developer-guide/working-with-rules).
|
5
|
-
|
6
|
-
`graphql-eslint` converts the [GraphQL AST](https://graphql.org/graphql-js/language) into
|
7
|
-
[ESTree structure](https://github.com/estree/estree), so it allows you to easily travel the GraphQL
|
8
|
-
AST tree easily.
|
9
|
-
|
10
|
-
You can visit any GraphQL AST node in your custom rules, and report this as error. You don't need to
|
11
|
-
have special handlers for code-files, since `graphql-eslint` extracts usages of `gql` and magic
|
12
|
-
`/* GraphQL */` comments automatically, and runs it through the parser, and eventually it knows to
|
13
|
-
adjust errors location to fit in your code files original location.
|
14
|
-
|
15
|
-
You can explore the GraphQL AST for a given input on
|
16
|
-
[astexplorer.net](https://astexplorer.net/#/gist/e72aaccc1e57cbba41659d73cabbf75c/f10ee29317a31ff8012a21762a382c4a03c34d40).
|
17
|
-
|
18
|
-
## Getting Started
|
19
|
-
|
20
|
-
Start by creating a
|
21
|
-
[simple ESLint rule file](https://eslint.org/docs/developer-guide/working-with-rules), and choose
|
22
|
-
the AST nodes you wish to visit. It can either be a
|
23
|
-
[simple AST node `Kind`](https://github.com/graphql/graphql-js/blob/master/src/language/kinds.d.ts)
|
24
|
-
or a complex [ESLint selector](https://eslint.org/docs/developer-guide/selectors) that allows you to
|
25
|
-
travel and filter AST nodes.
|
26
|
-
|
27
|
-
We recommend you to read the [graphql-eslint parser documentation](parser.md) before getting
|
28
|
-
started, to understand the differences between the AST structures.
|
29
|
-
|
30
|
-
The `graphql-eslint` comes with a TypeScript wrapper for ESLint rules, and provides a testkit to
|
31
|
-
simplify testing process with GraphQL schemas, so you can use that by importing `GraphQLESLintRule`
|
32
|
-
type. But if you wish to use JavaScript - that's fine :)
|
33
|
-
|
34
|
-
Here's an example for a simple rule that reports on anonymous GraphQL operations:
|
35
|
-
|
36
|
-
```ts
|
37
|
-
import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin'
|
38
|
-
|
39
|
-
const rule: GraphQLESLintRule = {
|
40
|
-
create(context) {
|
41
|
-
return {
|
42
|
-
OperationDefinition(node) {
|
43
|
-
if (!node.name?.value) {
|
44
|
-
context.report({
|
45
|
-
node,
|
46
|
-
message: 'Oops, name is required!'
|
47
|
-
})
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|
52
|
-
}
|
53
|
-
```
|
54
|
-
|
55
|
-
So what happens here?
|
56
|
-
|
57
|
-
1. `@graphql-eslint/eslint-plugin` handles the parsing process for your GraphQL content. It will
|
58
|
-
load the GraphQL files (either from code files or from `.graphql` files with SDL), parse it using
|
59
|
-
GraphQL parser, converts it to ESTree structure and let ESLint do the rest.
|
60
|
-
1. Your rule is being loaded by ESLint, and executes just like any other ESLint rule.
|
61
|
-
1. Our custom rule asks ESLint to run our function for every `OperationDefinition` found.
|
62
|
-
1. If the `OperationDefinition` node doesn't have a valid `name` - we report an error to ESLint.
|
63
|
-
|
64
|
-
#### More Examples
|
65
|
-
|
66
|
-
You can scan the `packages/plugin/src/rules` directory in this repo for references for implementing
|
67
|
-
rules. It coverts most of the use-cases and concepts of rules.
|
68
|
-
|
69
|
-
## Accessing original GraphQL AST nodes
|
70
|
-
|
71
|
-
Since our parser converts GraphQL AST to ESTree structure, there are some minor differences in the
|
72
|
-
structure of the objects. If you are using TypeScript, and you typed your rule with
|
73
|
-
`GraphQLESLintRule` - you'll see that each `node` is a bit different from the AST nodes of GraphQL
|
74
|
-
(you can read more about that in [graphql-eslint parser documentation](parser.md)).
|
75
|
-
|
76
|
-
If you need access to the original GraphQL AST `node`, you can use `.rawNode()` method on each node
|
77
|
-
you get from the AST structure of ESLint.
|
78
|
-
|
79
|
-
This is useful if you wish to use other GraphQL tools that works with the original GraphQL AST
|
80
|
-
objects.
|
81
|
-
|
82
|
-
Here's an example for using original `graphql-js` validate method to validate `OperationDefinition`:
|
83
|
-
|
84
|
-
```ts
|
85
|
-
import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
|
86
|
-
import { validate } from 'graphql'
|
87
|
-
|
88
|
-
export const rule = {
|
89
|
-
create(context) {
|
90
|
-
return {
|
91
|
-
OperationDefinition(node) {
|
92
|
-
const schema = requireGraphQLSchemaFromContext(context)
|
93
|
-
|
94
|
-
validate(context, schema, {
|
95
|
-
kind: Kind.DOCUMENT,
|
96
|
-
definitions: [node.rawNode()]
|
97
|
-
})
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}
|
101
|
-
}
|
102
|
-
```
|
103
|
-
|
104
|
-
## `TypeInfo` / `GraphQLSchema`
|
105
|
-
|
106
|
-
If you provide GraphQL schema in your ESLint configuration, it will get loaded automatically, and
|
107
|
-
become available in your rules in two ways:
|
108
|
-
|
109
|
-
1. You'll be able to access the loaded `GraphQLSchema` object.
|
110
|
-
2. In every visited node, you'll be able to use `.typeInfo()` method to get an object with complete
|
111
|
-
type information on your visited node (see
|
112
|
-
[TypeInfo documentation](https://graphql.org/graphql-js/utilities/#typeinfo)).
|
113
|
-
|
114
|
-
#### Getting `GraphQLSchema`
|
115
|
-
|
116
|
-
To mark your ESLint rules as a rule that needs access to GraphQL schema, start by running
|
117
|
-
`requireGraphQLSchemaFromContext` from the plugin package, it will make sure to return a schema, or
|
118
|
-
throw an error for the user about the missing schema.
|
119
|
-
|
120
|
-
```ts
|
121
|
-
const schema = requireGraphQLSchemaFromContext(context)
|
122
|
-
```
|
123
|
-
|
124
|
-
#### Accessing TypeInfo
|
125
|
-
|
126
|
-
If schema is provided and loaded successfully, the `typeInfo` will be available to use. Otherwise -
|
127
|
-
it will be `undefined`. If your plugin requires `typeInfo` in order to operate and run, make sure to
|
128
|
-
call `requireGraphQLSchemaFromContext` - it will validate that the schema is loaded.
|
129
|
-
|
130
|
-
`typeInfo` is provided on every node, based on the type of that node, for example, to access the
|
131
|
-
`GraphQLOutputType` while you are visiting a `SelectionSet` node, you can do:
|
132
|
-
|
133
|
-
```ts
|
134
|
-
import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'
|
135
|
-
|
136
|
-
export const rule = {
|
137
|
-
create(context) {
|
138
|
-
requireGraphQLSchemaFromContext('your-rule-name', context)
|
139
|
-
|
140
|
-
return {
|
141
|
-
SelectionSet(node) {
|
142
|
-
const typeInfo = node.typeInfo()
|
143
|
-
if (typeInfo.gqlType) {
|
144
|
-
console.log(`The GraphQLOutputType is: ${typeInfo.gqlType}`)
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
}
|
149
|
-
}
|
150
|
-
```
|
151
|
-
|
152
|
-
The structure of the return value of `.typeInfo()` is
|
153
|
-
[defined here](https://github.com/B2o5T/graphql-eslint/blob/master/packages/plugin/src/estree-converter/converter.ts#L32-L40).
|
154
|
-
So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.
|
155
|
-
|
156
|
-
## Testing your rules
|
157
|
-
|
158
|
-
To test your rules, you can either use the wrapped `GraphQLRuleTester` from this library, or use the
|
159
|
-
built-it [`RuleTester`](https://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests)
|
160
|
-
of ESLint.
|
161
|
-
|
162
|
-
The wrapped `GraphQLRuleTester` provides built-in configured parser, and a schema loader, if you
|
163
|
-
need to test your rule with a loaded schema.
|
164
|
-
|
165
|
-
```ts
|
166
|
-
import { GraphQLRuleTester } from '@graphql-eslint/eslint-plugin'
|
167
|
-
import { rule } from './my-rule'
|
168
|
-
|
169
|
-
const ruleTester = new GraphQLRuleTester()
|
170
|
-
|
171
|
-
ruleTester.runGraphQLTests('my-rule', rule, {
|
172
|
-
valid: [
|
173
|
-
{
|
174
|
-
code: 'query something { foo }'
|
175
|
-
}
|
176
|
-
],
|
177
|
-
invalid: [
|
178
|
-
{
|
179
|
-
code: 'query invalid { foo }',
|
180
|
-
errors: [{ message: 'Your error message.' }]
|
181
|
-
}
|
182
|
-
]
|
183
|
-
})
|
184
|
-
```
|
1
|
+
website/src/pages/docs/custom-rules.mdx
|
package/docs/deprecated-rules.md
CHANGED
@@ -1,24 +1 @@
|
|
1
|
-
|
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
|
10
|
-
[`no-scalar-result-type-on-mutation`](rules/no-scalar-result-type-on-mutation.md).
|
11
|
-
|
12
|
-
## avoid-typename-prefix
|
13
|
-
|
14
|
-
This rule was renamed to [`no-typename-prefix`](rules/no-typename-prefix.md).
|
15
|
-
|
16
|
-
## avoid-operation-name-prefix
|
17
|
-
|
18
|
-
This rule was removed because the same things can be validated using
|
19
|
-
[`naming-convention`](rules/naming-convention.md).
|
20
|
-
|
21
|
-
## no-operation-name-suffix
|
22
|
-
|
23
|
-
This rule was removed because the same things can be validated using
|
24
|
-
[`naming-convention`](rules/naming-convention.md).
|
1
|
+
website/src/pages/rules/deprecated-rules.mdx
|
package/docs/parser-options.md
CHANGED
@@ -1,107 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
## `graphQLParserOptions`
|
4
|
-
|
5
|
-
With this configuration, you can specify custom configurations for GraphQL's `parse` method. By
|
6
|
-
default, `graphql-eslint` parser just adds `noLocation: false` to make sure all parsed AST has
|
7
|
-
`location` set, since we need this for tokenizing and for converting the GraphQL AST into ESTree.
|
8
|
-
|
9
|
-
You can find the
|
10
|
-
[complete set of options for this object here](https://github.com/graphql/graphql-js/blob/6e48d16f92b9a6df8638b1486354c6be2537033b/src/language/parser.ts#L73)
|
11
|
-
|
12
|
-
## `skipGraphQLConfig`
|
13
|
-
|
14
|
-
If you are using [`graphql-config`](https://graphql-config.com) in your project, the parser will
|
15
|
-
automatically use that to load your default GraphQL schema.
|
16
|
-
|
17
|
-
You can disable this behaviour using `skipGraphQLConfig: true` in the `parserOptions`:
|
18
|
-
|
19
|
-
```json
|
20
|
-
{
|
21
|
-
"parserOptions": {
|
22
|
-
"skipGraphQLConfig": true
|
23
|
-
}
|
24
|
-
}
|
25
|
-
```
|
26
|
-
|
27
|
-
## `schema`
|
28
|
-
|
29
|
-
You can specify `parserOptions.schema` to load your GraphQL schema. The parser uses `graphql-tools`
|
30
|
-
and it's loaders, that means you can either specify a URL, a path to a local `.json` (introspection)
|
31
|
-
file, or a path to a local `.graphql` file(s). You can also use Glob expressions to load multiple
|
32
|
-
files.
|
33
|
-
|
34
|
-
Here are a few examples for a valid setup:
|
35
|
-
|
36
|
-
```json
|
37
|
-
{
|
38
|
-
"parserOptions": {
|
39
|
-
"schema": "./schema.graphql"
|
40
|
-
}
|
41
|
-
}
|
42
|
-
```
|
43
|
-
|
44
|
-
```json
|
45
|
-
{
|
46
|
-
"parserOptions": {
|
47
|
-
"schema": "./schema.json"
|
48
|
-
}
|
49
|
-
}
|
50
|
-
```
|
51
|
-
|
52
|
-
```json
|
53
|
-
{
|
54
|
-
"parserOptions": {
|
55
|
-
"schema": "http://my-server/graphql"
|
56
|
-
}
|
57
|
-
}
|
58
|
-
```
|
59
|
-
|
60
|
-
```json
|
61
|
-
{
|
62
|
-
"parserOptions": {
|
63
|
-
"schema": "./src/**/*.graphql"
|
64
|
-
}
|
65
|
-
}
|
66
|
-
```
|
67
|
-
|
68
|
-
```json
|
69
|
-
{
|
70
|
-
"parserOptions": {
|
71
|
-
"schema": ["src/schema-a.graphql", "src/schema-b.graphql", "src/schema-c.graphql"]
|
72
|
-
}
|
73
|
-
}
|
74
|
-
```
|
75
|
-
|
76
|
-
## `schemaOptions`
|
77
|
-
|
78
|
-
If you wish to send additional configuration for the `graphql-tools` loaders that loads your schema,
|
79
|
-
you can specify `schemaOptions` object:
|
80
|
-
|
81
|
-
```json
|
82
|
-
{
|
83
|
-
"parserOptions": {
|
84
|
-
"schema": "http://my-server/graphql",
|
85
|
-
"schemaOptions": {
|
86
|
-
"headers": {
|
87
|
-
"Authorization": "Bearer MY_TOKEN"
|
88
|
-
}
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}
|
92
|
-
```
|
93
|
-
|
94
|
-
```json
|
95
|
-
{
|
96
|
-
"parserOptions": {
|
97
|
-
"schema": "./src/**/*.graphql",
|
98
|
-
"schemaOptions": {
|
99
|
-
"assumeValid": true
|
100
|
-
}
|
101
|
-
}
|
102
|
-
}
|
103
|
-
```
|
104
|
-
|
105
|
-
> The configuration here is flexible, and will be sent to `graphql-tools` and it's loaders. So
|
106
|
-
> depends on the schema source, the options may vary.
|
107
|
-
> [You can read more about these loaders and their configuration here](https://graphql-tools.com/docs/api/interfaces/loaders_graphql_file_src.GraphQLFileLoaderOptions#properties).
|
1
|
+
website/src/pages/docs/getting-started/parser-options.mdx
|
package/docs/parser.md
CHANGED
@@ -1,67 +1 @@
|
|
1
|
-
|
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
|
6
|
-
[ESLint `processor`](https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins)
|
7
|
-
to find in code-files).
|
8
|
-
1. Is uses `graphql-js` (and `graphql-tools`) to parse the found string into a `DocumentNode`.
|
9
|
-
1. Extracts all comments (marked as `# ...`) from the parsed AST, and provides to ESLint as
|
10
|
-
directives hints.
|
11
|
-
1. If `graphql-config` is used, or `schema` field is provided, the schema is being loaded and
|
12
|
-
provided to the rules using `parserServices`.
|
13
|
-
1. Converts the `DocumentNode` to ESTree structure (and enrich the nodes with `typeInfo`, if schema
|
14
|
-
is loaded).
|
15
|
-
|
16
|
-
## ESTree Conversion
|
17
|
-
|
18
|
-
The GraphQL AST structure is very similar to ESTree structure, but there are a few differences that
|
19
|
-
the `parser` does.
|
20
|
-
|
21
|
-
Here's a list of changes that the parser performs, in order to make the GraphQL AST compatible with
|
22
|
-
ESTree:
|
23
|
-
|
24
|
-
---
|
25
|
-
|
26
|
-
**Problem**: GraphQL uses `kind` field to define the kind of the AST node, while ESTree uses `type`.
|
27
|
-
|
28
|
-
**Solution**: The parser adds `type` field on each node, and just copies the value from `kind`
|
29
|
-
field.
|
30
|
-
|
31
|
-
---
|
32
|
-
|
33
|
-
**Problem**: Some GraphQL AST nodes are using `type` field (which conflicts with the ESTree kind).
|
34
|
-
|
35
|
-
**Solution**: AST nodes that has `type` field are being transformed, and the `type` field changes to
|
36
|
-
`gqlType`.
|
37
|
-
|
38
|
-
---
|
39
|
-
|
40
|
-
**Problem**: GraphQL AST structure allows circular JSON links (while ESTree might fail on
|
41
|
-
`Maximum call stack exceeded`).
|
42
|
-
|
43
|
-
**Solution**: The parser removes circular JSONs (specifically around GraphQL `Location` and the
|
44
|
-
`Lexer`)
|
45
|
-
|
46
|
-
---
|
47
|
-
|
48
|
-
**Problem**: GraphQL uses `location` field to store the AST locations, while ESTree also uses it in
|
49
|
-
a different structure.
|
50
|
-
|
51
|
-
**Solution**: The parser creates a new `location` field that is compatible with ESTree.
|
52
|
-
|
53
|
-
### Loading GraphQL Schema
|
54
|
-
|
55
|
-
If you are using [`graphql-config`](https://graphql-config.com) in your project, the parser will
|
56
|
-
automatically use that to load your default GraphQL schema (you can disable this behaviour using
|
57
|
-
`skipGraphQLConfig: true` in the `parserOptions`).
|
58
|
-
|
59
|
-
If you are not using `graphql-config`, you can specify `parserOptions.schema` to load your GraphQL
|
60
|
-
schema. The parser uses `graphql-tools` and it's loaders, that means you can either specify a URL, a
|
61
|
-
path to a local `.json` (introspection) file, or a path to a local `.graphql` file(s). You can also
|
62
|
-
use Glob expressions to load multiple files.
|
63
|
-
|
64
|
-
[You can find more detail on the `parserOptions` config here](parser-options.md)
|
65
|
-
|
66
|
-
Providing the schema will make sure that rules that needs it will be able to access it, and it
|
67
|
-
enriches every converted AST node with `typeInfo`.
|
1
|
+
website/src/pages/docs/getting-started/parser.mdx
|
package/esm/documents.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
import { resolve } from 'path';
|
2
2
|
import debugFactory from 'debug';
|
3
3
|
import fg from 'fast-glob';
|
4
|
-
import { Kind, visit, } from 'graphql';
|
5
4
|
import { ModuleCache } from './cache.js';
|
6
|
-
import { logger } from './utils.js';
|
7
5
|
const debug = debugFactory('graphql-eslint:operations');
|
6
|
+
const operationsCache = new ModuleCache();
|
8
7
|
const handleVirtualPath = (documents) => {
|
9
8
|
const filepathMap = Object.create(null);
|
10
9
|
return documents.map(source => {
|
@@ -21,9 +20,7 @@ const handleVirtualPath = (documents) => {
|
|
21
20
|
};
|
22
21
|
});
|
23
22
|
};
|
24
|
-
const
|
25
|
-
const siblingOperationsCache = new Map();
|
26
|
-
const getSiblings = (project) => {
|
23
|
+
export const getDocuments = (project) => {
|
27
24
|
const documentsKey = project.documents;
|
28
25
|
if (!documentsKey) {
|
29
26
|
return [];
|
@@ -45,103 +42,3 @@ const getSiblings = (project) => {
|
|
45
42
|
}
|
46
43
|
return siblings;
|
47
44
|
};
|
48
|
-
export function getDocuments(project) {
|
49
|
-
const siblings = getSiblings(project);
|
50
|
-
if (siblings.length === 0) {
|
51
|
-
let printed = false;
|
52
|
-
const noopWarn = () => {
|
53
|
-
if (!printed) {
|
54
|
-
logger.warn('getSiblingOperations was called without any operations. Make sure to set "parserOptions.operations" to make this feature available!');
|
55
|
-
printed = true;
|
56
|
-
}
|
57
|
-
return [];
|
58
|
-
};
|
59
|
-
return {
|
60
|
-
available: false,
|
61
|
-
getFragment: noopWarn,
|
62
|
-
getFragments: noopWarn,
|
63
|
-
getFragmentByType: noopWarn,
|
64
|
-
getFragmentsInUse: noopWarn,
|
65
|
-
getOperation: noopWarn,
|
66
|
-
getOperations: noopWarn,
|
67
|
-
getOperationByType: noopWarn,
|
68
|
-
};
|
69
|
-
}
|
70
|
-
// Since the siblings array is cached, we can use it as cache key.
|
71
|
-
// We should get the same array reference each time we get
|
72
|
-
// to this point for the same graphql project
|
73
|
-
const value = siblingOperationsCache.get(siblings);
|
74
|
-
if (value) {
|
75
|
-
return value;
|
76
|
-
}
|
77
|
-
let fragmentsCache = null;
|
78
|
-
const getFragments = () => {
|
79
|
-
var _a;
|
80
|
-
if (fragmentsCache === null) {
|
81
|
-
const result = [];
|
82
|
-
for (const source of siblings) {
|
83
|
-
for (const definition of ((_a = source.document) === null || _a === void 0 ? void 0 : _a.definitions) || []) {
|
84
|
-
if (definition.kind === Kind.FRAGMENT_DEFINITION) {
|
85
|
-
result.push({
|
86
|
-
filePath: source.location,
|
87
|
-
document: definition,
|
88
|
-
});
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}
|
92
|
-
fragmentsCache = result;
|
93
|
-
}
|
94
|
-
return fragmentsCache;
|
95
|
-
};
|
96
|
-
let cachedOperations = null;
|
97
|
-
const getOperations = () => {
|
98
|
-
var _a;
|
99
|
-
if (cachedOperations === null) {
|
100
|
-
const result = [];
|
101
|
-
for (const source of siblings) {
|
102
|
-
for (const definition of ((_a = source.document) === null || _a === void 0 ? void 0 : _a.definitions) || []) {
|
103
|
-
if (definition.kind === Kind.OPERATION_DEFINITION) {
|
104
|
-
result.push({
|
105
|
-
filePath: source.location,
|
106
|
-
document: definition,
|
107
|
-
});
|
108
|
-
}
|
109
|
-
}
|
110
|
-
}
|
111
|
-
cachedOperations = result;
|
112
|
-
}
|
113
|
-
return cachedOperations;
|
114
|
-
};
|
115
|
-
const getFragment = (name) => getFragments().filter(f => { var _a; return ((_a = f.document.name) === null || _a === void 0 ? void 0 : _a.value) === name; });
|
116
|
-
const collectFragments = (selectable, recursive, collected = new Map()) => {
|
117
|
-
visit(selectable, {
|
118
|
-
FragmentSpread(spread) {
|
119
|
-
const fragmentName = spread.name.value;
|
120
|
-
const [fragment] = getFragment(fragmentName);
|
121
|
-
if (!fragment) {
|
122
|
-
logger.warn(`Unable to locate fragment named "${fragmentName}", please make sure it's loaded using "parserOptions.operations"`);
|
123
|
-
return;
|
124
|
-
}
|
125
|
-
if (!collected.has(fragmentName)) {
|
126
|
-
collected.set(fragmentName, fragment.document);
|
127
|
-
if (recursive) {
|
128
|
-
collectFragments(fragment.document, recursive, collected);
|
129
|
-
}
|
130
|
-
}
|
131
|
-
},
|
132
|
-
});
|
133
|
-
return collected;
|
134
|
-
};
|
135
|
-
const siblingOperations = {
|
136
|
-
available: true,
|
137
|
-
getFragment,
|
138
|
-
getFragments,
|
139
|
-
getFragmentByType: typeName => getFragments().filter(f => { var _a, _b; return ((_b = (_a = f.document.typeCondition) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.value) === typeName; }),
|
140
|
-
getFragmentsInUse: (selectable, recursive = true) => Array.from(collectFragments(selectable, recursive).values()),
|
141
|
-
getOperation: name => getOperations().filter(o => { var _a; return ((_a = o.document.name) === null || _a === void 0 ? void 0 : _a.value) === name; }),
|
142
|
-
getOperations,
|
143
|
-
getOperationByType: type => getOperations().filter(o => o.document.operation === type),
|
144
|
-
};
|
145
|
-
siblingOperationsCache.set(siblings, siblingOperations);
|
146
|
-
return siblingOperations;
|
147
|
-
}
|
package/esm/graphql-config.js
CHANGED
@@ -28,7 +28,7 @@ export function loadGraphQLConfig(options) {
|
|
28
28
|
? { projects: options.projects }
|
29
29
|
: {
|
30
30
|
schema: (options.schema || ''),
|
31
|
-
documents: options.documents
|
31
|
+
documents: options.documents,
|
32
32
|
extensions: options.extensions,
|
33
33
|
include: options.include,
|
34
34
|
exclude: options.exclude,
|
package/esm/parser.js
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
import { parseGraphQLSDL } from '@graphql-tools/utils';
|
2
2
|
import debugFactory from 'debug';
|
3
|
-
import { GraphQLError, GraphQLSchema } from 'graphql';
|
4
|
-
import { getDocuments } from './documents.js';
|
3
|
+
import { buildSchema, GraphQLError, GraphQLSchema } from 'graphql';
|
5
4
|
import { convertToESTree, extractComments, extractTokens } from './estree-converter/index.js';
|
6
5
|
import { loadGraphQLConfig } from './graphql-config.js';
|
7
6
|
import { getSchema } from './schema.js';
|
7
|
+
import { getSiblings } from './siblings.js';
|
8
8
|
import { CWD, VIRTUAL_DOCUMENT_REGEX } from './utils.js';
|
9
9
|
const debug = debugFactory('graphql-eslint:parser');
|
10
10
|
debug('cwd %o', CWD);
|
11
11
|
export function parseForESLint(code, options) {
|
12
12
|
try {
|
13
13
|
const { filePath } = options;
|
14
|
+
// TODO: remove in graphql-eslint v4
|
15
|
+
options.documents || (options.documents = options.operations);
|
14
16
|
// First parse code from file, in case of syntax error do not try load schema,
|
15
17
|
// documents or even graphql-config instance
|
16
18
|
const { document } = parseGraphQLSDL(filePath, code, {
|
@@ -20,12 +22,16 @@ export function parseForESLint(code, options) {
|
|
20
22
|
const gqlConfig = loadGraphQLConfig(options);
|
21
23
|
const realFilepath = filePath.replace(VIRTUAL_DOCUMENT_REGEX, '');
|
22
24
|
const project = gqlConfig.getProjectForFile(realFilepath);
|
23
|
-
const schema =
|
25
|
+
const schema = project
|
26
|
+
? getSchema(project, options.schemaOptions)
|
27
|
+
: typeof options.schema === 'string'
|
28
|
+
? buildSchema(options.schema)
|
29
|
+
: null;
|
24
30
|
const rootTree = convertToESTree(document, schema instanceof GraphQLSchema ? schema : undefined);
|
25
31
|
return {
|
26
32
|
services: {
|
27
33
|
schema,
|
28
|
-
siblingOperations:
|
34
|
+
siblingOperations: getSiblings(project, options.documents),
|
29
35
|
},
|
30
36
|
ast: {
|
31
37
|
comments: extractComments(document.loc),
|
package/esm/rules/alphabetize.js
CHANGED
@@ -83,7 +83,7 @@ export const rule = {
|
|
83
83
|
docs: {
|
84
84
|
category: ['Schema', 'Operations'],
|
85
85
|
description: 'Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.',
|
86
|
-
url: `https://
|
86
|
+
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
|
87
87
|
examples: [
|
88
88
|
{
|
89
89
|
title: 'Incorrect',
|
@@ -42,7 +42,7 @@ export const rule = {
|
|
42
42
|
],
|
43
43
|
description: 'Require all comments to follow the same style (either block or inline).',
|
44
44
|
category: 'Schema',
|
45
|
-
url: 'https://
|
45
|
+
url: 'https://the-guild.dev/graphql/eslint/rules/description-style',
|
46
46
|
recommended: true,
|
47
47
|
},
|
48
48
|
schema,
|
@@ -115,7 +115,7 @@ const validationToRule = ({ ruleId, ruleName, getDocumentNode, schema = [], hasD
|
|
115
115
|
recommended: true,
|
116
116
|
...docs,
|
117
117
|
graphQLJSRuleName: ruleName,
|
118
|
-
url: `https://
|
118
|
+
url: `https://the-guild.dev/graphql/eslint/rules/${ruleId}`,
|
119
119
|
description: `${docs.description}\n> This rule is a wrapper around a \`graphql-js\` validation function.`,
|
120
120
|
},
|
121
121
|
schema,
|
package/esm/rules/input-name.js
CHANGED
@@ -9,7 +9,7 @@ const schema = {
|
|
9
9
|
checkInputType: {
|
10
10
|
type: 'boolean',
|
11
11
|
default: false,
|
12
|
-
description: 'Check that the input type name follows the convention
|
12
|
+
description: 'Check that the input type name follows the convention \\<mutationName>Input',
|
13
13
|
},
|
14
14
|
caseSensitiveInputType: {
|
15
15
|
type: 'boolean',
|
@@ -41,7 +41,7 @@ export const rule = {
|
|
41
41
|
docs: {
|
42
42
|
description: 'Require mutation argument to be always called "input" and input type to be called Mutation name + "Input".\nUsing the same name for all input parameters will make your schemas easier to consume and more predictable. Using the same name as mutation for InputType will make it easier to find mutations that InputType belongs to.',
|
43
43
|
category: 'Schema',
|
44
|
-
url: 'https://
|
44
|
+
url: 'https://the-guild.dev/graphql/eslint/rules/input-name',
|
45
45
|
examples: [
|
46
46
|
{
|
47
47
|
title: 'Incorrect',
|
@@ -53,7 +53,7 @@ export const rule = {
|
|
53
53
|
`,
|
54
54
|
},
|
55
55
|
{
|
56
|
-
title: 'Correct (with checkInputType)',
|
56
|
+
title: 'Correct (with `checkInputType`)',
|
57
57
|
usage: [{ checkInputType: true }],
|
58
58
|
code: /* GraphQL */ `
|
59
59
|
type Mutation {
|
@@ -62,7 +62,7 @@ export const rule = {
|
|
62
62
|
`,
|
63
63
|
},
|
64
64
|
{
|
65
|
-
title: 'Correct (without checkInputType)',
|
65
|
+
title: 'Correct (without `checkInputType`)',
|
66
66
|
usage: [{ checkInputType: false }],
|
67
67
|
code: /* GraphQL */ `
|
68
68
|
type Mutation {
|
@@ -26,7 +26,7 @@ export const rule = {
|
|
26
26
|
docs: {
|
27
27
|
category: 'Operations',
|
28
28
|
description: 'Require queries, mutations, subscriptions or fragments to be located in separate files.',
|
29
|
-
url: `https://
|
29
|
+
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
|
30
30
|
examples: [
|
31
31
|
{
|
32
32
|
title: 'Incorrect',
|
@@ -1,7 +1,6 @@
|
|
1
|
-
import { existsSync } from 'fs';
|
2
1
|
import { basename, extname } from 'path';
|
3
2
|
import { Kind } from 'graphql';
|
4
|
-
import { convertCase, REPORT_ON_FIRST_CHARACTER } from '../utils.js';
|
3
|
+
import { convertCase, REPORT_ON_FIRST_CHARACTER, VIRTUAL_DOCUMENT_REGEX, } from '../utils.js';
|
5
4
|
const MATCH_EXTENSION = 'MATCH_EXTENSION';
|
6
5
|
const MATCH_STYLE = 'MATCH_STYLE';
|
7
6
|
const CASE_STYLES = [
|
@@ -54,7 +53,7 @@ export const rule = {
|
|
54
53
|
docs: {
|
55
54
|
category: 'Operations',
|
56
55
|
description: 'This rule allows you to enforce that the file name should match the operation name.',
|
57
|
-
url: 'https://
|
56
|
+
url: 'https://the-guild.dev/graphql/eslint/rules/match-document-filename',
|
58
57
|
examples: [
|
59
58
|
{
|
60
59
|
title: 'Correct',
|
@@ -166,7 +165,7 @@ export const rule = {
|
|
166
165
|
fileExtension: null,
|
167
166
|
};
|
168
167
|
const filePath = context.getFilename();
|
169
|
-
const isVirtualFile =
|
168
|
+
const isVirtualFile = VIRTUAL_DOCUMENT_REGEX.test(filePath);
|
170
169
|
if (process.env.NODE_ENV !== 'test' && isVirtualFile) {
|
171
170
|
// Skip validation for code files
|
172
171
|
return {};
|
@@ -97,7 +97,7 @@ export const rule = {
|
|
97
97
|
description: 'Require names to follow specified conventions.',
|
98
98
|
category: ['Schema', 'Operations'],
|
99
99
|
recommended: true,
|
100
|
-
url: 'https://
|
100
|
+
url: 'https://the-guild.dev/graphql/eslint/rules/naming-convention',
|
101
101
|
examples: [
|
102
102
|
{
|
103
103
|
title: 'Incorrect',
|