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AAC,YAAY,CAAC,CAAC;gBAC3B,IAAM,GAAG,GAAG,KAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;gBACtC,IAAI,GAAG,EAAE,CAAC;oBACR,mBAAmB,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;gBACjD,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,IAAM,cAAY,GAA6B,EAAE,CAAC;YAClD,OAAO,CAAC,OAAO,CAAC,UAAC,IAAI;gBACnB,IAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;gBACtB,IAAI,GAAG,EAAE,CAAC;oBACR,cAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACzB,CAAC;YACH,CAAC,CAAC,CAAC;YAEH,IAAI,cAAY,CAAC,MAAM,EAAE,CAAC;gBACxB,QAAQ,yBACH,QAAQ,KACX,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,cAAY,CAAC,GACvD,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEM,8CAAmB,GAA1B,UAA2B,IAAa;QACtC,IAAM,OAAO,GAAsB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEvD,KAAK,CAAC,IAAI,EAAE;YACV,cAAc,YAAC,IAAI;gBACjB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;YAClC,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC;IACH,uBAAC;AAAD,CAAC,AApID,IAoIC","sourcesContent":["import type {\n DocumentNode,\n ASTNode,\n FragmentDefinitionNode,\n FragmentSpreadNode,\n} from \"graphql\";\nimport { visit } from \"graphql\";\n\nimport { wrap } from \"optimism\";\n\nimport type { FragmentMap } from \"../../utilities/index.js\";\nimport {\n cacheSizes,\n defaultCacheSizes,\n getFragmentDefinitions,\n} from \"../../utilities/index.js\";\nimport { WeakCache } from \"@wry/caches\";\n\nexport interface FragmentRegistryAPI {\n register(...fragments: DocumentNode[]): this;\n lookup(fragmentName: string): FragmentDefinitionNode | null;\n transform<D extends DocumentNode>(document: D): D;\n resetCaches(): void;\n}\n\n// As long as createFragmentRegistry is not imported or used, the\n// FragmentRegistry example implementation provided below should not be bundled\n// (by tree-shaking bundlers like Rollup), because the implementation of\n// InMemoryCache refers only to the TypeScript interface FragmentRegistryAPI,\n// never the concrete implementation FragmentRegistry (which is deliberately not\n// exported from this module).\nexport function createFragmentRegistry(\n ...fragments: DocumentNode[]\n): FragmentRegistryAPI {\n return new FragmentRegistry(...fragments);\n}\n\nclass FragmentRegistry implements FragmentRegistryAPI {\n private registry: FragmentMap = Object.create(null);\n\n // Call `createFragmentRegistry` instead of invoking the\n // FragmentRegistry constructor directly. This reserves the constructor for\n // future configuration of the FragmentRegistry.\n constructor(...fragments: DocumentNode[]) {\n this.resetCaches();\n if (fragments.length) {\n this.register(...fragments);\n }\n }\n\n public register(...fragments: DocumentNode[]): this {\n const definitions = new Map<string, FragmentDefinitionNode>();\n fragments.forEach((doc: DocumentNode) => {\n getFragmentDefinitions(doc).forEach((node) => {\n definitions.set(node.name.value, node);\n });\n });\n\n definitions.forEach((node, name) => {\n if (node !== this.registry[name]) {\n this.registry[name] = node;\n this.invalidate(name);\n }\n });\n\n return this;\n }\n\n // Overridden in the resetCaches method below.\n private invalidate(name: string) {}\n\n public resetCaches() {\n const proto = FragmentRegistry.prototype;\n this.invalidate = (this.lookup = wrap(proto.lookup.bind(this), {\n makeCacheKey: (arg) => arg,\n max:\n cacheSizes[\"fragmentRegistry.lookup\"] ||\n defaultCacheSizes[\"fragmentRegistry.lookup\"],\n })).dirty; // This dirty function is bound to the wrapped lookup method.\n this.transform = wrap(proto.transform.bind(this), {\n cache: WeakCache,\n max:\n cacheSizes[\"fragmentRegistry.transform\"] ||\n defaultCacheSizes[\"fragmentRegistry.transform\"],\n });\n this.findFragmentSpreads = wrap(proto.findFragmentSpreads.bind(this), {\n cache: WeakCache,\n max:\n cacheSizes[\"fragmentRegistry.findFragmentSpreads\"] ||\n defaultCacheSizes[\"fragmentRegistry.findFragmentSpreads\"],\n });\n }\n\n /*\n * Note:\n * This method is only memoized so it can serve as a dependency to `tranform`,\n * so calling `invalidate` will invalidate cache entries for `transform`.\n */\n public lookup(fragmentName: string): FragmentDefinitionNode | null {\n return this.registry[fragmentName] || null;\n }\n\n public transform<D extends DocumentNode>(document: D): D {\n const defined = new Map<string, FragmentDefinitionNode>();\n getFragmentDefinitions(document).forEach((def) => {\n defined.set(def.name.value, def);\n });\n\n const unbound = new Set<string>();\n const enqueue = (spreadName: string) => {\n if (!defined.has(spreadName)) {\n unbound.add(spreadName);\n }\n };\n\n const enqueueChildSpreads = (node: ASTNode) =>\n Object.keys(this.findFragmentSpreads(node)).forEach(enqueue);\n\n enqueueChildSpreads(document);\n\n const missing: string[] = [];\n const map: FragmentMap = Object.create(null);\n\n // This Set forEach loop can be extended during iteration by adding\n // additional strings to the unbound set.\n unbound.forEach((fragmentName) => {\n const knownFragmentDef = defined.get(fragmentName);\n if (knownFragmentDef) {\n enqueueChildSpreads((map[fragmentName] = knownFragmentDef));\n } else {\n missing.push(fragmentName);\n const def = this.lookup(fragmentName);\n if (def) {\n enqueueChildSpreads((map[fragmentName] = def));\n }\n }\n });\n\n if (missing.length) {\n const defsToAppend: FragmentDefinitionNode[] = [];\n missing.forEach((name) => {\n const def = map[name];\n if (def) {\n defsToAppend.push(def);\n }\n });\n\n if (defsToAppend.length) {\n document = {\n ...document,\n definitions: document.definitions.concat(defsToAppend),\n };\n }\n }\n\n return document;\n }\n\n public findFragmentSpreads(root: ASTNode): FragmentSpreadMap {\n const spreads: FragmentSpreadMap = Object.create(null);\n\n visit(root, {\n FragmentSpread(node) {\n spreads[node.name.value] = node;\n },\n });\n\n return spreads;\n }\n}\n\ninterface FragmentSpreadMap {\n [fragmentSpreadName: string]: FragmentSpreadNode;\n}\n"]} |