UNPKG

5.01 kBJavaScriptView Raw
1import { invariant } from "../../utilities/globals/index.js";
2import { argumentsObjectFromField, DeepMerger, isNonEmptyArray, isNonNullObject, } from "../../utilities/index.js";
3import { hasOwn, isArray } from "./helpers.js";
4var specifierInfoCache = Object.create(null);
5function lookupSpecifierInfo(spec) {
6 var cacheKey = JSON.stringify(spec);
7 return specifierInfoCache[cacheKey] ||
8 (specifierInfoCache[cacheKey] = Object.create(null));
9}
10export function keyFieldsFnFromSpecifier(specifier) {
11 var info = lookupSpecifierInfo(specifier);
12 return info.keyFieldsFn || (info.keyFieldsFn = function (object, context) {
13 var extract = function (from, key) { return context.readField(key, from); };
14 var keyObject = context.keyObject = collectSpecifierPaths(specifier, function (schemaKeyPath) {
15 var extracted = extractKeyPath(context.storeObject, schemaKeyPath, extract);
16 if (extracted === void 0 &&
17 object !== context.storeObject &&
18 hasOwn.call(object, schemaKeyPath[0])) {
19 extracted = extractKeyPath(object, schemaKeyPath, extractKey);
20 }
21 __DEV__ ? invariant(extracted !== void 0, "Missing field '".concat(schemaKeyPath.join('.'), "' while extracting keyFields from ").concat(JSON.stringify(object))) : invariant(extracted !== void 0, 2);
22 return extracted;
23 });
24 return "".concat(context.typename, ":").concat(JSON.stringify(keyObject));
25 });
26}
27export function keyArgsFnFromSpecifier(specifier) {
28 var info = lookupSpecifierInfo(specifier);
29 return info.keyArgsFn || (info.keyArgsFn = function (args, _a) {
30 var field = _a.field, variables = _a.variables, fieldName = _a.fieldName;
31 var collected = collectSpecifierPaths(specifier, function (keyPath) {
32 var firstKey = keyPath[0];
33 var firstChar = firstKey.charAt(0);
34 if (firstChar === "@") {
35 if (field && isNonEmptyArray(field.directives)) {
36 var directiveName_1 = firstKey.slice(1);
37 var d = field.directives.find(function (d) { return d.name.value === directiveName_1; });
38 var directiveArgs = d && argumentsObjectFromField(d, variables);
39 return directiveArgs && extractKeyPath(directiveArgs, keyPath.slice(1));
40 }
41 return;
42 }
43 if (firstChar === "$") {
44 var variableName = firstKey.slice(1);
45 if (variables && hasOwn.call(variables, variableName)) {
46 var varKeyPath = keyPath.slice(0);
47 varKeyPath[0] = variableName;
48 return extractKeyPath(variables, varKeyPath);
49 }
50 return;
51 }
52 if (args) {
53 return extractKeyPath(args, keyPath);
54 }
55 });
56 var suffix = JSON.stringify(collected);
57 if (args || suffix !== "{}") {
58 fieldName += ":" + suffix;
59 }
60 return fieldName;
61 });
62}
63export function collectSpecifierPaths(specifier, extractor) {
64 var merger = new DeepMerger;
65 return getSpecifierPaths(specifier).reduce(function (collected, path) {
66 var _a;
67 var toMerge = extractor(path);
68 if (toMerge !== void 0) {
69 for (var i = path.length - 1; i >= 0; --i) {
70 toMerge = (_a = {}, _a[path[i]] = toMerge, _a);
71 }
72 collected = merger.merge(collected, toMerge);
73 }
74 return collected;
75 }, Object.create(null));
76}
77export function getSpecifierPaths(spec) {
78 var info = lookupSpecifierInfo(spec);
79 if (!info.paths) {
80 var paths_1 = info.paths = [];
81 var currentPath_1 = [];
82 spec.forEach(function (s, i) {
83 if (isArray(s)) {
84 getSpecifierPaths(s).forEach(function (p) { return paths_1.push(currentPath_1.concat(p)); });
85 currentPath_1.length = 0;
86 }
87 else {
88 currentPath_1.push(s);
89 if (!isArray(spec[i + 1])) {
90 paths_1.push(currentPath_1.slice(0));
91 currentPath_1.length = 0;
92 }
93 }
94 });
95 }
96 return info.paths;
97}
98function extractKey(object, key) {
99 return object[key];
100}
101export function extractKeyPath(object, path, extract) {
102 extract = extract || extractKey;
103 return normalize(path.reduce(function reducer(obj, key) {
104 return isArray(obj)
105 ? obj.map(function (child) { return reducer(child, key); })
106 : obj && extract(obj, key);
107 }, object));
108}
109function normalize(value) {
110 if (isNonNullObject(value)) {
111 if (isArray(value)) {
112 return value.map(normalize);
113 }
114 return collectSpecifierPaths(Object.keys(value).sort(), function (path) { return extractKeyPath(value, path); });
115 }
116 return value;
117}
118//# sourceMappingURL=key-extractor.js.map
\No newline at end of file