UNPKG

4.28 kBJavaScriptView Raw
1import { __assign, __spreadArray } from "tslib";
2import { visit, } from "graphql";
3import { wrap } from "optimism";
4import { getFragmentDefinitions } from "../../utilities/index.js";
5export function createFragmentRegistry() {
6 var fragments = [];
7 for (var _i = 0; _i < arguments.length; _i++) {
8 fragments[_i] = arguments[_i];
9 }
10 return new (FragmentRegistry.bind.apply(FragmentRegistry, __spreadArray([void 0], fragments, false)))();
11}
12var arrayLikeForEach = Array.prototype.forEach;
13var FragmentRegistry = (function () {
14 function FragmentRegistry() {
15 var fragments = [];
16 for (var _i = 0; _i < arguments.length; _i++) {
17 fragments[_i] = arguments[_i];
18 }
19 this.registry = Object.create(null);
20 this.resetCaches();
21 if (fragments.length) {
22 this.register.apply(this, fragments);
23 }
24 }
25 FragmentRegistry.prototype.register = function () {
26 var _this = this;
27 var definitions = new Map();
28 arrayLikeForEach.call(arguments, function (doc) {
29 getFragmentDefinitions(doc).forEach(function (node) {
30 definitions.set(node.name.value, node);
31 });
32 });
33 definitions.forEach(function (node, name) {
34 if (node !== _this.registry[name]) {
35 _this.registry[name] = node;
36 _this.invalidate(name);
37 }
38 });
39 return this;
40 };
41 FragmentRegistry.prototype.invalidate = function (name) { };
42 FragmentRegistry.prototype.resetCaches = function () {
43 this.invalidate = (this.lookup = this.cacheUnaryMethod("lookup")).dirty;
44 this.transform = this.cacheUnaryMethod("transform");
45 this.findFragmentSpreads = this.cacheUnaryMethod("findFragmentSpreads");
46 };
47 FragmentRegistry.prototype.cacheUnaryMethod = function (name) {
48 var registry = this;
49 var originalMethod = FragmentRegistry.prototype[name];
50 return wrap(function () {
51 return originalMethod.apply(registry, arguments);
52 }, {
53 makeCacheKey: function (arg) { return arg; },
54 });
55 };
56 FragmentRegistry.prototype.lookup = function (fragmentName) {
57 return this.registry[fragmentName] || null;
58 };
59 FragmentRegistry.prototype.transform = function (document) {
60 var _this = this;
61 var defined = new Map();
62 getFragmentDefinitions(document).forEach(function (def) {
63 defined.set(def.name.value, def);
64 });
65 var unbound = new Set();
66 var enqueue = function (spreadName) {
67 if (!defined.has(spreadName)) {
68 unbound.add(spreadName);
69 }
70 };
71 var enqueueChildSpreads = function (node) { return Object.keys(_this.findFragmentSpreads(node)).forEach(enqueue); };
72 enqueueChildSpreads(document);
73 var missing = [];
74 var map = Object.create(null);
75 unbound.forEach(function (fragmentName) {
76 var knownFragmentDef = defined.get(fragmentName);
77 if (knownFragmentDef) {
78 enqueueChildSpreads(map[fragmentName] = knownFragmentDef);
79 }
80 else {
81 missing.push(fragmentName);
82 var def = _this.lookup(fragmentName);
83 if (def) {
84 enqueueChildSpreads(map[fragmentName] = def);
85 }
86 }
87 });
88 if (missing.length) {
89 var defsToAppend_1 = [];
90 missing.forEach(function (name) {
91 var def = map[name];
92 if (def) {
93 defsToAppend_1.push(def);
94 }
95 });
96 if (defsToAppend_1.length) {
97 document = __assign(__assign({}, document), { definitions: document.definitions.concat(defsToAppend_1) });
98 }
99 }
100 return document;
101 };
102 FragmentRegistry.prototype.findFragmentSpreads = function (root) {
103 var spreads = Object.create(null);
104 visit(root, {
105 FragmentSpread: function (node) {
106 spreads[node.name.value] = node;
107 },
108 });
109 return spreads;
110 };
111 return FragmentRegistry;
112}());
113//# sourceMappingURL=fragmentRegistry.js.map
\No newline at end of file