How Does a GraphQL-ESLint Parser Work?
The graphql-eslint
parser is works in the following way:
- Loads all relevant GraphQL code using ESLint core (either from
.graphql
files, or using ESLintprocessor
to find in code-files). - Is uses
graphql-js
(andgraphql-tools
) to parse the found string into aDocumentNode
. - Extracts all comments (marked as
# ...
) from the parsed AST, and provides to ESLint as directives hints. - If
graphql-config
is used, orschema
field is provided, the schema is being loaded and provided to the rules usingparserServices
. - Converts the
DocumentNode
to ESTree structure (and enrich the nodes withtypeInfo
, if schema is loaded).
ESTree Conversion
The GraphQL AST structure is very similar to ESTree structure, but there are a few differences that
the parser
does.
Here’s a list of changes that the parser performs, in order to make the GraphQL AST compatible with ESTree:
-
❌ Problem: GraphQL uses
kind
field to define the kind of the AST node, while ESTree usestype
.✅ Solution: The parser adds
type
field on each node, and just copies the value fromkind
field.
-
❌ Problem: Some GraphQL AST nodes are using
type
field (which conflicts with the ESTree kind).✅ Solution: AST nodes that has
type
field are being transformed, and thetype
field changes togqlType
.
-
❌ Problem: GraphQL AST structure allows circular JSON links (while ESTree might fail on
Maximum call stack exceeded
).✅ Solution: The parser removes circular JSONs (specifically around GraphQL
Location
and theLexer
).
-
❌ Problem: GraphQL uses
location
field to store the AST locations, while ESTree also uses it in a different structure.✅ Solution: The parser creates a new
location
field that is compatible with ESTree.
Loading GraphQL Schema
If you are using graphql-config
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.
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
.