If you’re defining GraphQL schemas or operations directly within code files (e.g., .js, .jsx,
.ts, .tsx files), check out the usage with .js/.jsx files.
Providing GraphQL Schema (Optional)
Some linting rules require access to the entire GraphQL schema. For example, the
no-unreachable-types rule checks that all types are reachable through
root-level fields.
To enable these rules, you need to inform ESLint how to identify and load your complete schema.
The GraphQL ESLint plugin integrates seamlessly with
GraphQL Config, which it uses to automatically load your
schema.
Example of providing GraphQL schema:
graphql.config.js
export default { // a path to a local `.graphql` file schema: './schema.graphql', // a glob expression to load multiple files schema: './src/**/*.graphql', // paths to multiple `.graphql` files schema: ['./src/schema-a.graphql', './src/schema-b.graphql', './src/schema-c.graphql'], // a path to a local `.json` (introspection result) file schema: './schema.json', // a URL endpoint schema: 'https://my-server/graphql'}
While implementing this tool, we had to find solutions for a better integration of the GraphQL
ecosystem and ESLint core.
GraphQL’s operations can be distributed across many files, while ESLint operates on one file at a
time. If you are using GraphQL fragments in separate files, some rules might yield incorrect
results, due the missing information.
To workaround that, we allow you to provide additional information on your GraphQL operations,
making it available for rules while doing the actual linting.
The GraphQL ESLint plugin integrates seamlessly with
GraphQL Config, which it uses to automatically load your
sibling operations and fragments.
Example of providing GraphQL operations:
graphql.config.js
export default { // a path to a local `.graphql` file documents: './users.graphql', // a glob expression to load multiple files documents: './src/**/*.graphql', // paths to multiple `.graphql` files documents: ['./src/users.graphql', './src/posts.graphql', './src/user-fields.graphql']}