UNPKG

@graphql-eslint/eslint-plugin

Version:
222 lines (211 loc) • 5.8 kB
import { TypeInfo, visit, visitWithTypeInfo } from "graphql"; import { ModuleCache } from "../../cache.js"; import { eslintSelectorsTip, requireGraphQLOperations, requireGraphQLSchema } from "../../utils.js"; const RULE_ID = "no-unused-fields", RELAY_SCHEMA = ( /* GraphQL */ ` # Root Query Type type Query { user: User } # User Type type User { id: ID! name: String! friends(first: Int, after: String): FriendConnection! } # FriendConnection Type (Relay Connection) type FriendConnection { edges: [FriendEdge] pageInfo: PageInfo! } # FriendEdge Type type FriendEdge { cursor: String! node: Friend! } # Friend Type type Friend { id: ID! name: String! } # PageInfo Type (Relay Pagination) type PageInfo { hasPreviousPage: Boolean! hasNextPage: Boolean! startCursor: String endCursor: String } ` ), RELAY_QUERY = ( /* GraphQL */ ` query { user { id name friends(first: 10) { edges { node { id name } } } } } ` ), RELAY_DEFAULT_IGNORED_FIELD_SELECTORS = [ "[parent.name.value=PageInfo][name.value=/(endCursor|startCursor|hasNextPage|hasPreviousPage)/]", "[parent.name.value=/Edge$/][name.value=cursor]", "[parent.name.value=/Connection$/][name.value=pageInfo]" ], schema = { type: "array", maxItems: 1, items: { type: "object", additionalProperties: !1, properties: { ignoredFieldSelectors: { type: "array", uniqueItems: !0, minItems: 1, description: [ "Fields that will be ignored and are allowed to be unused.", "", "E.g. The following selector will ignore all the relay pagination fields for every connection exposed in the schema:", "```json", JSON.stringify(RELAY_DEFAULT_IGNORED_FIELD_SELECTORS, null, 2), "```", eslintSelectorsTip ].join(` `), items: { type: "string", pattern: "^\\[(.+)]$" } } } } }, usedFieldsCache = new ModuleCache(); function getUsedFields(schema2, operations) { const cachedValue = usedFieldsCache.get(schema2); if (process.env.NODE_ENV !== "test" && cachedValue) return cachedValue; const usedFields = /* @__PURE__ */ Object.create(null), typeInfo = new TypeInfo(schema2), visitor = visitWithTypeInfo(typeInfo, { Field(node) { if (!typeInfo.getFieldDef()) return !1; const parentTypeName = typeInfo.getParentType().name, fieldName = node.name.value; usedFields[parentTypeName] ??= /* @__PURE__ */ new Set(), usedFields[parentTypeName].add(fieldName); } }), allDocuments = [...operations.getOperations(), ...operations.getFragments()]; for (const { document } of allDocuments) visit(document, visitor); return usedFieldsCache.set(schema2, usedFields), usedFields; } const rule = { meta: { messages: { [RULE_ID]: 'Field "{{fieldName}}" is unused' }, docs: { description: "Requires all fields to be used at some level by siblings operations.", category: "Schema", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSiblings: !0, requiresSchema: !0, // Requires documents to be set isDisabledForAllConfig: !0, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type User { id: ID! name: String someUnusedField: String } type Query { me: User } query { me { id name } } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type User { id: ID! name: String } type Query { me: User } query { me { id name } } ` ) }, { title: "Correct (ignoring fields)", usage: [{ ignoredFieldSelectors: RELAY_DEFAULT_IGNORED_FIELD_SELECTORS }], code: ( /* GraphQL */ ` ### 1\uFE0F\u20E3 YOUR SCHEMA ${RELAY_SCHEMA} ### 2\uFE0F\u20E3 YOUR QUERY ${RELAY_QUERY} ` ) } ] }, type: "suggestion", schema, hasSuggestions: !0 }, create(context) { const schema2 = requireGraphQLSchema(RULE_ID, context), siblingsOperations = requireGraphQLOperations(RULE_ID, context), usedFields = getUsedFields(schema2, siblingsOperations), { ignoredFieldSelectors } = context.options[0] || {}; return { [(ignoredFieldSelectors || []).reduce( (acc, selector2) => `${acc}:not(${selector2})`, "FieldDefinition" )](node) { const fieldName = node.name.value, parentTypeName = node.parent.name.value; usedFields[parentTypeName]?.has(fieldName) || context.report({ node: node.name, messageId: RULE_ID, data: { fieldName }, suggest: [ { desc: `Remove \`${fieldName}\` field`, fix(fixer) { const sourceCode = context.getSourceCode(), tokenBefore = sourceCode.getTokenBefore(node), tokenAfter = sourceCode.getTokenAfter(node), isEmptyType = tokenBefore.type === "{" && tokenAfter.type === "}"; return fixer.remove(isEmptyType ? node.parent : node); } } ] }); } }; } }; export { rule };