Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-types': 'off',
'unicorn/prefer-array-some': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/no-useless-fallback-in-spread': 'error',
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This project integrates GraphQL and ESLint, for a better developer experience.
## Key Features

- 🚀 Integrates with ESLint core (as a ESTree parser)
- 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments
- 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with non-breaking space, so on mobile, this must look better

before

image

after

image

- 🚀 Lints both GraphQL schema and GraphQL operations
- 🚀 Extended type info for more advanced usages
- 🚀 Supports ESLint directives (for example: `eslint-disable-next-line`)
Expand Down Expand Up @@ -189,10 +189,10 @@ See [docs/deprecated-rules.md](docs/deprecated-rules.md).
<!-- prettier-ignore-start -->
|Name|Description|
|:-:|-|
|[`schema-recommended`](packages/plugin/src/configs/schema-recommended.ts)|enables recommended rules for schema (SDL) development|
|[`schema-all`](packages/plugin/src/configs/schema-all.ts)|enables all rules for schema (SDL) development, except for those that require `parserOptions.operations` option|
|[`operations-recommended`](packages/plugin/src/configs/operations-recommended.ts) |enables recommended rules for consuming GraphQL (operations) development|
|[`operations-all`](packages/plugin/src/configs/operations-all.ts)|enables all rules for consuming GraphQL (operations) development|
|[`schema-recommended`](packages/plugin/src/configs/schema-recommended.json)|enables recommended rules for schema (SDL) development|
|[`schema-all`](packages/plugin/src/configs/schema-all.json)|enables all rules for schema (SDL) development, except for those that require `parserOptions.operations` option|
|[`operations-recommended`](packages/plugin/src/configs/operations-recommended.json) |enables recommended rules for consuming GraphQL (operations) development|
|[`operations-all`](packages/plugin/src/configs/operations-all.json)|enables all rules for consuming GraphQL (operations) development|
<!-- prettier-ignore-end -->

> If you are in a project that develops the GraphQL schema, you'll need `schema` rules.
Expand Down
16 changes: 8 additions & 8 deletions docs/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

To get started with your own rules, start by understanding how [ESLint custom rules works](https://eslint.org/docs/developer-guide/working-with-rules).

`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.
`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.

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.

## Getting Started

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.

We recommend you to read the [graphql-eslint parser documentation](./parser.md) before getting started, to understand the differences between the AST structures.
We recommend you to read the [graphql-eslint parser documentation](parser.md) before getting started, to understand the differences between the AST structures.

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 :)

Expand Down Expand Up @@ -49,7 +49,7 @@ You can scan the `packages/plugin/src/rules` directory in this repo for referenc
## Accessing original GraphQL AST nodes

Since our parser converts GraphQL AST to ESTree structure, there are some minor differences in the structure of the objects.
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)).
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)).

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.

Expand Down Expand Up @@ -104,13 +104,12 @@ import { requireGraphQLSchemaFromContext } from '@graphql-eslint/eslint-plugin'

export const rule = {
create(context) {
requireGraphQLSchemaFromContext(context)
requireGraphQLSchemaFromContext('your-rule-name', context)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


return {
SelectionSet(node) {
const typeInfo = node.typeInfo()

if (typeInfo && typeInfo.gqlType) {
if (typeInfo.gqlType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeInfo is always truthy

console.log(`The GraphQLOutputType is: ${typeInfo.gqlType}`)
}
}
Expand All @@ -119,7 +118,7 @@ export const rule = {
}
```

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.
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#L45-L53). So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.

## Testing your rules

Expand All @@ -141,7 +140,8 @@ ruleTester.runGraphQLTests('my-rule', rule, {
],
invalid: [
{
code: 'query invalid { foo }'
code: 'query invalid { foo }',
errors: [{ message: 'Your error message.' }],
}
]
})
Expand Down
8 changes: 4 additions & 4 deletions docs/parser-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

### `graphQLParserOptions`

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 tokening and for converting the GraphQL AST into ESTree.
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.

You can find the [complete set of options for this object here](https://github.com/graphql/graphql-js/blob/master/src/language/parser.d.ts#L7)
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed broken link with permanent link


### `skipGraphQLConfig`

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.
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`:

Expand Down Expand Up @@ -82,4 +82,4 @@ If you wish to send additional configuration for the `graphql-tools` loaders tha
}
```

> 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://www.graphql-tools.com/docs/api/interfaces/_loaders_graphql_file_src_index_.graphqlfileloaderoptions).
> 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).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed broken link

4 changes: 2 additions & 2 deletions docs/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Here's a list of changes that the parser performs, in order to make the GraphQL

### Loading GraphQL Schema

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`).
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`).

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.

[You can find more detail on the `parserOptions` config here](./parser-options.md)
[You can find more detail on the `parserOptions` config here](parser-options.md)

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`.
1 change: 1 addition & 0 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
},
"buildOptions": {
"input": "./src/index.ts",
"copy": "./src/configs",
"external": [
"eslint",
"graphql",
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin/src/configs/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"parser": "@graphql-eslint/eslint-plugin",
"plugins": ["@graphql-eslint"]
}
4 changes: 0 additions & 4 deletions packages/plugin/src/configs/base.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/plugin/src/configs/index.ts

This file was deleted.

24 changes: 24 additions & 0 deletions packages/plugin/src/configs/operations-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": ["./base.json", "./operations-recommended.json"],
"rules": {
"@graphql-eslint/alphabetize": [
"error",
{
"selections": ["OperationDefinition", "FragmentDefinition"],
"variables": ["OperationDefinition"],
"arguments": ["Field", "Directive"]
}
],
"@graphql-eslint/match-document-filename": [
"error",
{
"query": "kebab-case",
"mutation": "kebab-case",
"subscription": "kebab-case",
"fragment": "kebab-case"
}
],
"@graphql-eslint/unique-fragment-name": "error",
"@graphql-eslint/unique-operation-name": "error"
}
}
23 changes: 0 additions & 23 deletions packages/plugin/src/configs/operations-all.ts

This file was deleted.

50 changes: 50 additions & 0 deletions packages/plugin/src/configs/operations-recommended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"extends": "./base.json",
"rules": {
"@graphql-eslint/executable-definitions": "error",
"@graphql-eslint/fields-on-correct-type": "error",
"@graphql-eslint/fragments-on-composite-type": "error",
"@graphql-eslint/known-argument-names": "error",
"@graphql-eslint/known-directives": "error",
"@graphql-eslint/known-fragment-names": "error",
"@graphql-eslint/known-type-names": "error",
"@graphql-eslint/lone-anonymous-operation": "error",
"@graphql-eslint/naming-convention": [
"error",
{
"VariableDefinition": "camelCase",
"OperationDefinition": {
"style": "PascalCase",
"forbiddenPrefixes": ["Query", "Mutation", "Subscription", "Get"],
"forbiddenSuffixes": ["Query", "Mutation", "Subscription"]
},
"FragmentDefinition": {
"style": "PascalCase",
"forbiddenPrefixes": ["Fragment"],
"forbiddenSuffixes": ["Fragment"]
}
}
],
"@graphql-eslint/no-anonymous-operations": "error",
"@graphql-eslint/no-deprecated": "error",
"@graphql-eslint/no-duplicate-fields": "error",
"@graphql-eslint/no-fragment-cycles": "error",
"@graphql-eslint/no-undefined-variables": "error",
"@graphql-eslint/no-unused-fragments": "error",
"@graphql-eslint/no-unused-variables": "error",
"@graphql-eslint/one-field-subscriptions": "error",
"@graphql-eslint/overlapping-fields-can-be-merged": "error",
"@graphql-eslint/possible-fragment-spread": "error",
"@graphql-eslint/provided-required-arguments": "error",
"@graphql-eslint/require-id-when-available": "error",
"@graphql-eslint/scalar-leafs": "error",
"@graphql-eslint/selection-set-depth": ["error", { "maxDepth": 7 }],
"@graphql-eslint/unique-argument-names": "error",
"@graphql-eslint/unique-directive-names-per-location": "error",
"@graphql-eslint/unique-input-field-names": "error",
"@graphql-eslint/unique-variable-names": "error",
"@graphql-eslint/value-literals-of-correct-type": "error",
"@graphql-eslint/variables-are-input-types": "error",
"@graphql-eslint/variables-in-allowed-position": "error"
}
}
50 changes: 0 additions & 50 deletions packages/plugin/src/configs/operations-recommended.ts

This file was deleted.

26 changes: 26 additions & 0 deletions packages/plugin/src/configs/schema-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"extends": ["./base.json", "./schema-recommended.json"],
"rules": {
"@graphql-eslint/alphabetize": [
"error",
{
"fields": [
"ObjectTypeDefinition",
"InterfaceTypeDefinition",
"InputObjectTypeDefinition"
],
"values": ["EnumTypeDefinition"],
"arguments": [
"FieldDefinition",
"Field",
"DirectiveDefinition",
"Directive"
]
}
],
"@graphql-eslint/input-name": "error",
"@graphql-eslint/no-scalar-result-type-on-mutation": "error",
"@graphql-eslint/require-deprecation-date": "error",
"@graphql-eslint/require-field-of-type-query-in-mutation-result": "error"
}
}
21 changes: 0 additions & 21 deletions packages/plugin/src/configs/schema-all.ts

This file was deleted.

Loading