Skip to content

Commit ad8b6a5

Browse files
authored
Merge pull request #30 from dotansimha/docs
Docs & README
2 parents 2a672ec + f6c5355 commit ad8b6a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+755
-357
lines changed

README.md

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,106 @@
1-
# graphql-eslint
2-
graphql-eslint
1+
This project integrates GraphQL AST parser and ESLint.
2+
3+
## Key Features:
4+
5+
- 🚀 Integrates with ESLint core (as a ESTree parser).
6+
- 🚀 Works on `.graphql` files, `gql` usages and `/* GraphQL */` magic comments.
7+
- 🚀 Lints both GraphQL schema and GraphQL operations.
8+
- 🚀 Extended type info for more advanced usages
9+
- 🚀 Supports ESLint directives (for example: `disable-next-line`)
10+
- 🚀 Easily extendable - supports custom rules.
11+
12+
Special thanks to [ilyavolodin](https://github.com/ilyavolodin) for his work on a similar project!
13+
14+
## Getting Started
15+
16+
### Installation
17+
18+
Start by installing the plugin package, which includes everything you need:
19+
20+
```
21+
yarn add -D @graphql-eslint/eslint-plugin
22+
```
23+
24+
Or, with NPM:
25+
26+
```
27+
npm install --save-dev @graphql-eslint/eslint-plugin
28+
```
29+
30+
> Also, make sure you have `graphql` dependency in your project.
31+
32+
### Configuration
33+
34+
To get started, create an override configuration for your ESLint, while applying it to to `.graphql` files (do that even if you are declaring your operations in code files):
35+
36+
```json
37+
{
38+
"overrides": [
39+
{
40+
"files": ["*.graphql"],
41+
"parser": "@graphql-eslint/eslint-plugin",
42+
"plugins": ["@graphql-eslint"],
43+
"rules": {
44+
...
45+
}
46+
}
47+
]
48+
}
49+
```
50+
51+
If you are using code files to store your GraphQL schema or your GraphQL operations, you can extend the behaviour of ESLint and extract those, by adding the following to your setup:
52+
53+
```json
54+
{
55+
"processor": "@graphql-eslint/graphql",
56+
"overrides": [ ... ]
57+
}
58+
```
59+
60+
#### Extended linting with GraphQL Schema
61+
62+
If you are using [`graphql-config`](https://graphql-config.com/) - you are good to go. This package integrates with it automatically, and will use it to load your schema!
63+
64+
Linting process can be enriched and extended with GraphQL type information, if you are able to provide your GraphQL schema.
65+
66+
The parser allow you to specify a json file / graphql files(s) / url / raw string to locate your schema (We are using `graphql-tools` to do that). Just add `parserOptions.schema` to your configuration file:
67+
68+
```json
69+
{
70+
"files": ["*.graphql"],
71+
"parser": "@graphql-eslint/eslint-plugin",
72+
"plugins": ["@graphql-eslint"],
73+
"parserOptions": {
74+
"schema": "./schema.graphql"
75+
}
76+
}
77+
```
78+
79+
> Some rules requires type information to operate, it's marked in the docs of each plugin!
80+
81+
### VSCode Integration
82+
83+
By default, [ESLint VSCode plugin](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) will not lint files with extensions other then js, jsx, ts, tsx.
84+
85+
In order to enable it processing other extensions, add the following section in `settings.json` or workspace configuration.
86+
87+
```json
88+
{
89+
"eslint.validate": [
90+
"javascript",
91+
"javascriptreact",
92+
"typescript",
93+
"typescriptreact",
94+
"graphql"
95+
],
96+
"eslint.options": {
97+
"extentions": [".js", ".graphql"]
98+
}
99+
}
100+
```
101+
102+
## Available Rules
103+
104+
You can find a complete list of [all available plugins here](./docs/README.md)
105+
106+
> This repo doesn't exports a "recommended" set of rules - feel free to recommend us!

docs/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Available Rules
2+
3+
- [`avoid-operation-name-prefix`](./avoid-operation-name-prefix.md)
4+
- [`input-name`](./input-name.md)
5+
- [`description-style`](./description-style.md)
6+
- [`naming-convention`](./naming-convention.md)
7+
- [`no-anonymous-operations`](./no-anonymous-operations.md)
8+
- [`require-deprecation-reason`](./require-deprecation-reason.md)
9+
- [`no-case-insensitive-enum-values-duplicates`](./no-case-insensitive-enum-values-duplicates.md)
10+
- [`no-operation-name-suffix`](./no-operation-name-suffix.md)
11+
- [`require-id-when-available`](./require-id-when-available.md)
12+
- [`validate-against-schema`](./validate-against-schema.md)
13+
- [`prettier`](./prettier.md)
14+
- [`require-description`](./require-description.md)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Avoid Prefix in GraphQL operation name
2+
3+
- Name: `avoid-operation-name-prefix`
4+
- Requires GraphQL Schema: `false`
5+
6+
Allow you to enforce and avoid operation name prefix, useful if you wish to avoid prefix in your root fields,like `getSomething`.
7+
8+
### Usage Example
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```graphql
13+
# eslint @graphql-eslint/avoid-operation-name-prefix: ["error", { keywords: "get" }]
14+
15+
query getUserDetails {
16+
# ...
17+
}
18+
```
19+
20+
Examples of **correct** code for this rule:
21+
22+
```graphql
23+
# eslint @graphql-eslint/avoid-operation-name-prefix: ["error", { keywords: "get" }]
24+
25+
query userDetails {
26+
# ...
27+
}
28+
```
29+
30+
### Configuration
31+
32+
- `keywords`: array of prefixes you with to avoid in operation names.

docs/rules/description-style.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Enforce style of the description comments
2+
3+
- Name: `description-style`
4+
- Requires GraphQL Schema: `false`
5+
6+
Following the same style for description comments in your code will make them easier to read.
7+
8+
## Rule Details
9+
10+
This rule enforces same style for all description comments in your code. This rule has one option: `style` that can be set to either `block` or `inline` (default).
11+
12+
Examples of **incorrect** code for this rule:
13+
14+
```graphql
15+
# eslint @graphql-eslint/description-style: ["error", { style: "inline" }]
16+
17+
""" Description """
18+
type someTypeName {
19+
...
20+
}
21+
```
22+
23+
Examples of **correct** code for this rule:
24+
25+
```graphql
26+
# eslint @graphql-eslint/description-style: ["error", { style: "inline" }]
27+
28+
" Description "
29+
type SomeTypeName {
30+
someFieldName: String
31+
}
32+
```
33+
34+
## Options
35+
36+
This rule accepts configuration object with one option:
37+
38+
- `style: 'block'|'inline'` - Will require all comments to be either in block style (`"""`) or inline style (`"`)

docs/rules/input-name.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Enforce names of input parameters and input types
2+
3+
- Name: `input-name`
4+
- Requires GraphQL Schema: `false`
5+
6+
Using 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.
7+
8+
## Rule Details
9+
10+
This rule enforces all input parameters to be named `input`, and all InputTypes to use the name of the mutation + `'Input'`.
11+
12+
Examples of **incorrect** code for this rule:
13+
14+
```graphql
15+
# eslint @graphql-eslint/input-name: ["error", { checkInputType: true }]
16+
17+
type Mutation {
18+
SetMessage(message: InputMessage): String
19+
}
20+
```
21+
22+
Examples of **correct** code for this rule:
23+
24+
```graphql
25+
# eslint @graphql-eslint/input-name: ["error", { checkInputType: true }]
26+
27+
type Mutation {
28+
SetMessage(input: SetMessageInput): String
29+
}
30+
```
31+
32+
```graphql
33+
# eslint @graphql-eslint/input-name: ["error", { checkInputType: false }]
34+
35+
type Mutation {
36+
SetMessage(input: AnyInputTypeName): String
37+
}
38+
```
39+
40+
## Configuration
41+
42+
This rule accepts one option `checkInputType`. If `true` (default) it will verify that the name of the input type matches name of the mutation + 'Input'. If `false` it will not check InputType name.

docs/rules/naming-convention.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Enforce naming conventions patterns in your typeDefs
2+
3+
- Name: `naming-convention`
4+
- Requires GraphQL Schema: `false`
5+
6+
Following the same naming conventions in all your type definitions will make them easier to read and more predictable.
7+
8+
## Rule Details
9+
10+
This rule enforces naming conventions patterns for your type definitions. It can control if the names should be in PascalCase, camelCase, snake_case or UPPER_CASE. It can also allow or disallow trailing and leading underscores.
11+
12+
Examples of **incorrect** code for this rule:
13+
14+
```graphql
15+
# eslint @graphql-eslint/naming-convention: ["error", { ObjectTypeDefinition: "PascalCase" }]
16+
17+
type someTypeName {
18+
...
19+
}
20+
```
21+
22+
Examples of **correct** code for this rule:
23+
24+
```graphql
25+
# eslint @graphql-eslint/naming-convention: ["error", { FieldDefinition: "camelCase", ObjectTypeDefinition: "PascalCase" }]
26+
27+
type SomeTypeName {
28+
someFieldName: String
29+
}
30+
```
31+
32+
## Options
33+
34+
This rule accepts configuration object with multiple options:
35+
36+
- `ObjectTypeDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of types, input objects, enums and interfaces
37+
- `FieldDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of type fields
38+
- `EnumValueDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of enumeration values
39+
- `InputValueDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of input properties
40+
- `FragmentDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of fragments
41+
- `ScalarTypeDefinition: 'PascalCase'|'camelCase'|'snake_case'|'UPPER_CASE'` - Will affect names of scalars
42+
- `leadingUnderscore: 'allow'|'forbid'` - Will allow or forbid leading underscores in all names. Default value is `forbid`.
43+
- `trailingUnderscore: 'allow'|'forbid'` - Will allow of forbid trailing underscores in all names. Default value is `forbid`.

docs/rules/no-anonymous-operations.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Avoid Anonymous GraphQL operations
2+
3+
- Name: `no-anonymous-operations`
4+
- Requires GraphQL Schema: `false`
5+
6+
Allow you to require name for your GraphQL operations. This is useful since most GraphQL client libraries are using the operation name for caching purposes.
7+
8+
### Usage Example
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```graphql
13+
# eslint @graphql-eslint/no-anonymous-operations: ["error"]
14+
15+
query {
16+
# ...
17+
}
18+
```
19+
20+
Examples of **correct** code for this rule:
21+
22+
```graphql
23+
# eslint @graphql-eslint/no-anonymous-operations: ["error"]
24+
25+
query something {
26+
# ...
27+
}
28+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Avoid Duplicates In Enum Values
2+
3+
- Name: `no-case-insensitive-enum-values-duplicates`
4+
- Requires GraphQL Schema: `false`
5+
6+
Checks enums values for duplicate values defined (case-insensitive).
7+
8+
### Usage Example
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```graphql
13+
# eslint @graphql-eslint/no-case-insensitive-enum-values-duplicates: ["error"]
14+
15+
enum MyEnum {
16+
Value
17+
VALUE
18+
ValuE
19+
}
20+
```
21+
22+
Examples of **correct** code for this rule:
23+
24+
```graphql
25+
# eslint @graphql-eslint/no-case-insensitive-enum-values-duplicates: ["error"]
26+
27+
enum MyEnum {
28+
Value1
29+
Value2
30+
Value3
31+
}
32+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## No Operation Name Suffix
2+
3+
- Name: `no-operation-name-suffix`
4+
- Requires GraphQL Schema: `false`
5+
6+
Makes sure you are not adding the operation type to the name of the operation.
7+
8+
### Usage Example
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```graphql
13+
# eslint @graphql-eslint/no-operation-name-suffix: ["error"]
14+
15+
query userQuery {
16+
# ...
17+
}
18+
```
19+
20+
Examples of **correct** code for this rule:
21+
22+
```graphql
23+
# eslint @graphql-eslint/no-operation-name-suffix: ["error"]
24+
25+
query user {
26+
# ...
27+
}
28+
```

docs/rules/prettier.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Enforces styling consistency in your type definitions
2+
3+
- Name: `prettier`
4+
- Requires GraphQL Schema: `false`
5+
6+
Having all your code follow the same styling guidelines makes it easier to read and understand the code
7+
8+
## Rule Details
9+
10+
This rule is a copy of the rule in [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier) adjusted to work for GraphQL.
11+
12+
Please see [documentation](https://github.com/prettier/eslint-plugin-prettier#options) for available options and configurations.

0 commit comments

Comments
 (0)