UNPKG

@graphql-eslint/eslint-plugin

Version:
222 lines (209 loc) • 6.51 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _graphql = require('graphql'); var _cachejs = require('../../cache.js'); var _utilsjs = require('../../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), "```", _utilsjs.eslintSelectorsTip ].join(` `), items: { type: "string", pattern: "^\\[(.+)]$" } } } } }, usedFieldsCache = new (0, _cachejs.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 (0, _graphql.TypeInfo)(schema2), visitor = _graphql.visitWithTypeInfo.call(void 0, 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) _graphql.visit.call(void 0, 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 = _utilsjs.requireGraphQLSchema.call(void 0, RULE_ID, context), siblingsOperations = _utilsjs.requireGraphQLOperations.call(void 0, 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; _optionalChain([usedFields, 'access', _ => _[parentTypeName], 'optionalAccess', _2 => _2.has, 'call', _3 => _3(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); } } ] }); } }; } }; exports.rule = rule;