@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
65 lines (64 loc) • 2.49 kB
JavaScript
import { Kind } from 'graphql';
import { getLocation } from '../utils.js';
const RULE_ID = 'no-anonymous-operations';
export const rule = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
category: 'Operations',
description: 'Require name for your GraphQL operations. This is useful since most GraphQL client libraries are using the operation name for caching purposes.',
recommended: true,
url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
examples: [
{
title: 'Incorrect',
code: /* GraphQL */ `
query {
# ...
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
query user {
# ...
}
`,
},
],
},
messages: {
[RULE_ID]: 'Anonymous GraphQL operations are forbidden. Make sure to name your {{ operation }}!',
},
schema: [],
},
create(context) {
return {
'OperationDefinition[name=undefined]'(node) {
const [firstSelection] = node.selectionSet.selections;
const suggestedName = firstSelection.kind === Kind.FIELD
? (firstSelection.alias || firstSelection.name).value
: node.operation;
context.report({
loc: getLocation(node.loc.start, node.operation),
messageId: RULE_ID,
data: {
operation: node.operation,
},
suggest: [
{
desc: `Rename to \`${suggestedName}\``,
fix(fixer) {
const sourceCode = context.getSourceCode();
const hasQueryKeyword = sourceCode.getText({ range: [node.range[0], node.range[0] + 1] }) !== '{';
return fixer.insertTextAfterRange([node.range[0], node.range[0] + (hasQueryKeyword ? node.operation.length : 0)], `${hasQueryKeyword ? '' : 'query'} ${suggestedName}${hasQueryKeyword ? '' : ' '}`);
},
},
],
});
},
};
},
};