@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
90 lines (85 loc) • 2.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true});var _nodepath = require('node:path');
var _graphql = require('graphql');
var _utilsjs = require('../../utils.js');
const RULE_ID = "no-one-place-fragments";
const rule = {
meta: {
type: "suggestion",
docs: {
category: "Operations",
description: "Disallow fragments that are used only in one place.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
examples: [
{
title: "Incorrect",
code: (
/* GraphQL */
`
fragment UserFields on User {
id
}
{
user {
...UserFields
}
}
`
)
},
{
title: "Correct",
code: (
/* GraphQL */
`
fragment UserFields on User {
id
}
{
user {
...UserFields
friends {
...UserFields
}
}
}
`
)
}
],
requiresSiblings: true
},
messages: {
[RULE_ID]: 'Fragment `{{fragmentName}}` used only once. Inline him in "{{filePath}}".'
},
schema: []
},
create(context) {
const operations = _utilsjs.requireGraphQLOperations.call(void 0, RULE_ID, context);
const allDocuments = [...operations.getOperations(), ...operations.getFragments()];
const usedFragmentsMap = /* @__PURE__ */ Object.create(null);
for (const { document, filePath } of allDocuments) {
const relativeFilePath = _nodepath.relative.call(void 0, _utilsjs.CWD, filePath);
_graphql.visit.call(void 0, document, {
FragmentSpread({ name }) {
const spreadName = name.value;
usedFragmentsMap[spreadName] ||= [];
usedFragmentsMap[spreadName].push(relativeFilePath);
}
});
}
return {
"FragmentDefinition > Name"(node) {
const fragmentName = node.value;
const fragmentUsage = usedFragmentsMap[fragmentName];
if (fragmentUsage.length === 1) {
context.report({
node,
messageId: RULE_ID,
data: { fragmentName, filePath: fragmentUsage[0] }
});
}
}
};
}
};
exports.rule = rule;
;