@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
75 lines (72 loc) • 2.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true});var _graphql = require('graphql');
var _utilsjs = require('../../utils.js');
const RULE_ID = "no-anonymous-operations";
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://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
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 === _graphql.Kind.FIELD ? (firstSelection.alias || firstSelection.name).value : node.operation;
context.report({
loc: _utilsjs.getLocation.call(void 0, 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 ? "" : " "}`
);
}
}
]
});
}
};
}
};
exports.rule = rule;
;