Parser
The graphql-eslint parser is works in the following way:
- Loads all relevant GraphQL code using ESLint core (either from
.graphqlfiles, or using ESLintprocessorto 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-configis used, orschemafield is provided, the schema is being loaded and provided to the rules usingparserServices. - Converts the
DocumentNodeto 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
kindfield to define the kind of the AST node, while ESTree usestype.✅ Solution: The parser adds
typefield on each node, and just copies the value fromkindfield.
-
❌ Problem: Some GraphQL AST nodes are using
typefield (which conflicts with the ESTree kind).✅ Solution: AST nodes that has
typefield are being transformed, and thetypefield 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
Locationand theLexer).
-
❌ Problem: GraphQL uses
locationfield to store the AST locations, while ESTree also uses it in a different structure.✅ Solution: The parser creates a new
locationfield 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.