What is eslint-plugin-graphql?
eslint-plugin-graphql is an ESLint plugin that provides GraphQL linting rules for your JavaScript and TypeScript projects. It helps ensure that your GraphQL queries, mutations, and subscriptions are syntactically correct and follow best practices.
What are eslint-plugin-graphql's main functionalities?
Linting GraphQL Queries
This feature allows you to lint GraphQL queries within template strings. The configuration specifies the environment as 'literal' and uses a local schema file to validate the queries.
{
"rules": {
"graphql/template-strings": [
"error",
{
"env": "literal",
"schemaJson": require('./schema.json')
}
]
}
}
Linting GraphQL Files
This feature allows you to lint GraphQL operations in .graphql files. The configuration uses a local schema file to validate the operations and ensures that all operations are named.
{
"rules": {
"graphql/named-operations": [
"error",
{
"schemaJson": require('./schema.json')
}
]
}
}
Custom Validation Rules
This feature allows you to enforce custom validation rules on your GraphQL queries. The example configuration ensures that every query includes the 'id' and '__typename' fields.
{
"rules": {
"graphql/required-fields": [
"error",
{
"env": "apollo",
"schemaJson": require('./schema.json'),
"requiredFields": ["id", "__typename"]
}
]
}
}
Other packages similar to eslint-plugin-graphql
graphql-eslint
graphql-eslint is another ESLint plugin that provides a comprehensive set of rules for linting GraphQL schema and operations. It offers more granular control over linting rules and supports both GraphQL schema and operations in various file types. Compared to eslint-plugin-graphql, graphql-eslint provides a more extensive set of rules and better integration with GraphQL tools like Apollo and Relay.