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G,EAAE;oBACP,cAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;iBACxB;YACH,CAAC,CAAC,CAAC;YAEH,IAAI,cAAY,CAAC,MAAM,EAAE;gBACvB,QAAQ,yBACH,QAAQ,KACX,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,cAAY,CAAC,GACvD,CAAC;aACH;SACF;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,AAhID,IAgIC","sourcesContent":["import {\n DocumentNode,\n ASTNode,\n FragmentDefinitionNode,\n FragmentSpreadNode,\n visit,\n} from \"graphql\";\n\nimport { wrap } from \"optimism\";\n\nimport { FragmentMap, getFragmentDefinitions } from \"../../utilities\";\n\nexport interface FragmentRegistryAPI {\n register(...fragments: DocumentNode[]): this;\n lookup(fragmentName: string): FragmentDefinitionNode | null;\n transform<D extends DocumentNode>(document: D): D;\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\nconst { forEach: arrayLikeForEach } = Array.prototype;\n\nclass FragmentRegistry implements FragmentRegistryAPI {\n private registry: FragmentMap = Object.create(null);\n\n // Call static method FragmentRegistry.from(...) 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.apply(this, fragments);\n }\n }\n\n public register(): this {\n const definitions = new Map<string, FragmentDefinitionNode>();\n arrayLikeForEach.call(arguments, (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 this.invalidate = (\n this.lookup = this.cacheUnaryMethod(\"lookup\")\n ).dirty; // This dirty function is bound to the wrapped lookup method.\n this.transform = this.cacheUnaryMethod(\"transform\");\n this.findFragmentSpreads = this.cacheUnaryMethod(\"findFragmentSpreads\");\n }\n\n private cacheUnaryMethod<TName extends keyof Pick<FragmentRegistry,\n | \"lookup\"\n | \"transform\"\n | \"findFragmentSpreads\"\n >>(name: TName) {\n const registry = this;\n const originalMethod = FragmentRegistry.prototype[name];\n return wrap(function () {\n return originalMethod.apply(registry, arguments);\n }, {\n makeCacheKey: arg => arg,\n });\n }\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) => Object.keys(\n this.findFragmentSpreads(node)\n ).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"]} |