Skip to Content
This is documentation for v4 of the plugin. For v3 click here.
ESLGraphQL-ESLint
DocumentationUsageCustom GraphQL Rules

Usage with custom GraphQL rules

Custom GraphQL Rule

my-rule.js
export const rule = {
  create(context) {
    return {
      OperationDefinition(node) {
        if (!node.name?.value) {
          context.report({
            node,
            message: 'Oops, name is required!',
          });
        }
      },
    };
  },
};

ESLint Flat Config

eslint.config.js
import graphqlPlugin from '@graphql-eslint/eslint-plugin';
import { rule } from './my-rule.js';
 
export default [
  {
    files: ['**/*.graphql'],
    languageOptions: {
      parser: graphqlPlugin.parser,
    },
    plugins: {
      '@internal': {
        rules: {
          'my-rule': rule,
        },
      },
    },
    rules: {
      '@internal/my-rule': 'error',
    },
  },
];
💡
Tip

Check out the custom rules guide to learn how to create your own rules.

Last updated on