Config for GraphQL servers implemented in Node.js.
-
If you haven't already, make sure to install
@code-pushup/eslint-config
and its required peer dependencies. -
Since this plugin requires additional peer dependencies, you have to install them as well:
npm install -D @graphql-eslint/eslint-plugin eslint-plugin-n
-
The GraphQL ESLint plugin needs to know where your GraphQL schema is located. For more information, refer to Extended Linting Rules with GraphQL Schema in GraphQL ESLint docs.
-
If you're using graphql-config, then your GraphQL schema will be loaded automatically from your
.graphqlrc.yml
(or equivalent) file. So no extra setup is required in this case. -
Otherwise, you can use
parserOptions.schema
, e.g.:export default tseslint.config( // ... { files: ['**/*.graphql'], languageOptions: { parserOptions: { schema: './schema.graphql' // globs are also supported: // schema: './src/schema/**/*.graphql.ts' } } } );
-
-
Add to your
eslint.config.js
file:import graphql from '@code-pushup/eslint-config/graphql.js'; import tseslint from 'typescript-eslint'; export default tseslint.config(...graphql);
294 rules are included from node
config. For brevity, only the 23 additional rules are listed in this document.
🔧 Automatically fixable by the
--fix
CLI option.
💡 Manually fixable by editor suggestions.
🧪🚫 Disabled for test files.
🧪⚠️ Severity lessened to warning for test files.
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
known-argument-names A GraphQL field is only valid if all supplied arguments are defined by that field. > This rule is a wrapper around a graphql-js validation function. |
💡 | |||
known-directives A GraphQL document is only valid if all @directive s are known by the schema and legally positioned.> This rule is a wrapper around a graphql-js validation function. |
||||
known-type-names A GraphQL document is only valid if referenced types (specifically variable definitions and fragment conditions) are defined by the type schema. > This rule is a wrapper around a graphql-js validation function. |
💡 | |||
lone-schema-definition A GraphQL document is only valid if it contains only one schema definition. > This rule is a wrapper around a graphql-js validation function. |
||||
naming-convention Require names to follow specified conventions. |
types: PascalCase, FieldDef...{
"types": "PascalCase",
"FieldDefinition": "camelCase",
"InputValueDefinition": "camelCase",
"Argument": "camelCase",
"DirectiveDefinition": "camelCase",
"EnumValueDefinition": "UPPER_CASE",
"FieldDefinition[parent.name.value=Query]": {
"forbiddenPrefixes": [
"query",
"get"
],
"forbiddenSuffixes": [
"Query"
]
},
"FieldDefinition[parent.name.value=Mutation]": {
"forbiddenPrefixes": [
"mutation"
],
"forbiddenSuffixes": [
"Mutation"
]
},
"FieldDefinition[parent.name.value=Subscription]": {
"forbiddenPrefixes": [
"subscription"
],
"forbiddenSuffixes": [
"Subscription"
]
}
} |
💡 | ||
no-case-insensitive-enum-values-duplicates Disallow case-insensitive enum values duplicates. |
💡 | |||
no-typename-prefix Enforces users to avoid using the type name in a field name while defining your schema. |
💡 | |||
no-unreachable-types Requires all types to be reachable at some level by root level fields. |
💡 | |||
provided-required-arguments A field or directive is only valid if all required (non-null without a default value) field arguments have been provided. > This rule is a wrapper around a graphql-js validation function. |
||||
relay-arguments Set of rules to follow Relay specification for Arguments. - A field that returns a Connection type must include forward pagination arguments ( first and after ), backward pagination arguments (last and before ), or bothForward pagination arguments - first takes a non-negative integer- after takes the Cursor typeBackward pagination arguments - last takes a non-negative integer- before takes the Cursor type |
||||
relay-connection-types Set of rules to follow Relay specification for Connection types. - Any type whose name ends in "Connection" is considered by spec to be a Connection type - Connection type must be an Object type - Connection type must contain a field edges that return a list type that wraps an edge type- Connection type must contain a field pageInfo that return a non-null PageInfo Object type |
||||
relay-edge-types Set of rules to follow Relay specification for Edge types. - A type that is returned in list form by a connection type's edges field is considered by this spec to be an Edge type- Edge type must be an Object type - Edge type must contain a field node that return either Scalar, Enum, Object, Interface, Union, or a non-null wrapper around one of those types. Notably, this field cannot return a list- Edge type must contain a field cursor that return either String, Scalar, or a non-null wrapper around one of those types- Edge type name must end in "Edge" (optional) - Edge type's field node must implement Node interface (optional)- A list type should only wrap an edge type (optional) |
listTypeCanWrapOnlyEdgeType...{
"listTypeCanWrapOnlyEdgeType": false
} |
|||
relay-page-info Set of rules to follow Relay specification for PageInfo object.- PageInfo must be an Object type- PageInfo must contain fields hasPreviousPage and hasNextPage , that return non-null Boolean- PageInfo must contain fields startCursor and endCursor , that return either String or Scalar, which can be null if there are no results |
||||
require-deprecation-reason Require all deprecation directives to specify a reason. |
||||
require-description Enforce descriptions in type definitions and operations. |
types: true, DirectiveDefin...{
"types": true,
"DirectiveDefinition": true
} |
|||
strict-id-in-types 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. |
||||
unique-directive-names A GraphQL document is only valid if all defined directives have unique names. > This rule is a wrapper around a graphql-js validation function. |
||||
unique-directive-names-per-location A GraphQL document is only valid if all non-repeatable directives at a given location are uniquely named. > This rule is a wrapper around a graphql-js validation function. |
||||
unique-field-definition-names A GraphQL complex type is only valid if all its fields are uniquely named. > This rule is a wrapper around a graphql-js validation function. |
||||
unique-operation-types A GraphQL document is only valid if it has only one type per operation. > This rule is a wrapper around a graphql-js validation function. |
||||
unique-type-names A GraphQL document is only valid if all defined types have unique names. > This rule is a wrapper around a graphql-js validation function. |
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
description-style Require all comments to follow the same style (either block or inline). |
style: inline{
"style": "inline"
} |
💡 | ||
no-hashtag-description Requires to use """ or " for adding a GraphQL description instead of # .Allows to use hashtag for comments, as long as it's not attached to an AST definition. |
💡 |