UNPKG

@graphql-eslint/eslint-plugin

Version:
97 lines (96 loc) 4.2 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var siblings_exports = {}; __export(siblings_exports, { getSiblings: () => getSiblings }); module.exports = __toCommonJS(siblings_exports); var import_graphql = require("graphql"), import_utils2 = require("./utils.js"); const siblingOperationsCache = /* @__PURE__ */ new Map(); function getSiblings(documents) { if (documents.length === 0) { let printed = !1; const noopWarn = () => (printed || (import_utils2.logger.warn( "getSiblingOperations was called without any operations. Make sure to set graphql-config `documents` field to make this feature available! See https://the-guild.dev/graphql/config/docs/user/documents for more info" ), printed = !0), []); return { available: !1, getFragment: noopWarn, getFragments: noopWarn, getFragmentByType: noopWarn, getFragmentsInUse: noopWarn, getOperation: noopWarn, getOperations: noopWarn, getOperationByType: noopWarn }; } const value = siblingOperationsCache.get(documents); if (value) return value; let fragmentsCache = null; const getFragments = () => { if (fragmentsCache === null) { const result = []; for (const source of documents) for (const definition of source.document?.definitions || []) definition.kind === import_graphql.Kind.FRAGMENT_DEFINITION && result.push({ filePath: source.location, document: definition }); fragmentsCache = result; } return fragmentsCache; }; let cachedOperations = null; const getOperations = () => { if (cachedOperations === null) { const result = []; for (const source of documents) for (const definition of source.document?.definitions || []) definition.kind === import_graphql.Kind.OPERATION_DEFINITION && result.push({ filePath: source.location, document: definition }); cachedOperations = result; } return cachedOperations; }, getFragment = (name) => getFragments().filter((f) => f.document.name.value === name), collectFragments = (selectable, recursive, collected = /* @__PURE__ */ new Map()) => ((0, import_graphql.visit)(selectable, { FragmentSpread(spread) { const fragmentName = spread.name.value, [fragment] = getFragment(fragmentName); if (!fragment) { import_utils2.logger.warn( `Unable to locate fragment named "${fragmentName}", please make sure it's loaded using "parserOptions.operations"` ); return; } collected.has(fragmentName) || (collected.set(fragmentName, fragment.document), recursive && collectFragments(fragment.document, recursive, collected)); } }), collected), siblingOperations = { available: !0, getFragment, getFragments, getFragmentByType: (typeName) => getFragments().filter((f) => f.document.typeCondition.name.value === typeName), getFragmentsInUse: (selectable, recursive = !0) => Array.from(collectFragments(selectable, recursive).values()), getOperation: (name) => getOperations().filter((o) => o.document.name?.value === name), getOperations, getOperationByType: (type) => getOperations().filter((o) => o.document.operation === type) }; return siblingOperationsCache.set(documents, siblingOperations), siblingOperations; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getSiblings });