UNPKG

3.25 kBJavaScriptView Raw
1import { isReference, isField, DeepMerger, resultKeyNameFromField, shouldInclude, isNonNullObject, compact, createFragmentMap, getFragmentDefinitions, isArray, } from "../../utilities/index.js";
2export var hasOwn = Object.prototype.hasOwnProperty;
3export function isNullish(value) {
4 return value === null || value === void 0;
5}
6export { isArray };
7export function defaultDataIdFromObject(_a, context) {
8 var __typename = _a.__typename, id = _a.id, _id = _a._id;
9 if (typeof __typename === "string") {
10 if (context) {
11 context.keyObject =
12 !isNullish(id) ? { id: id } :
13 !isNullish(_id) ? { _id: _id } :
14 void 0;
15 }
16 if (isNullish(id) && !isNullish(_id)) {
17 id = _id;
18 }
19 if (!isNullish(id)) {
20 return "".concat(__typename, ":").concat((typeof id === "number" ||
21 typeof id === "string") ? id : JSON.stringify(id));
22 }
23 }
24}
25var defaultConfig = {
26 dataIdFromObject: defaultDataIdFromObject,
27 addTypename: true,
28 resultCaching: true,
29 canonizeResults: false,
30};
31export function normalizeConfig(config) {
32 return compact(defaultConfig, config);
33}
34export function shouldCanonizeResults(config) {
35 var value = config.canonizeResults;
36 return value === void 0 ? defaultConfig.canonizeResults : value;
37}
38export function getTypenameFromStoreObject(store, objectOrReference) {
39 return isReference(objectOrReference)
40 ? store.get(objectOrReference.__ref, "__typename")
41 : objectOrReference && objectOrReference.__typename;
42}
43export var TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i;
44export function fieldNameFromStoreName(storeFieldName) {
45 var match = storeFieldName.match(TypeOrFieldNameRegExp);
46 return match ? match[0] : storeFieldName;
47}
48export function selectionSetMatchesResult(selectionSet, result, variables) {
49 if (isNonNullObject(result)) {
50 return isArray(result)
51 ? result.every(function (item) { return selectionSetMatchesResult(selectionSet, item, variables); })
52 : selectionSet.selections.every(function (field) {
53 if (isField(field) && shouldInclude(field, variables)) {
54 var key = resultKeyNameFromField(field);
55 return hasOwn.call(result, key) &&
56 (!field.selectionSet ||
57 selectionSetMatchesResult(field.selectionSet, result[key], variables));
58 }
59 return true;
60 });
61 }
62 return false;
63}
64export function storeValueIsStoreObject(value) {
65 return isNonNullObject(value) &&
66 !isReference(value) &&
67 !isArray(value);
68}
69export function makeProcessedFieldsMerger() {
70 return new DeepMerger;
71}
72export function extractFragmentContext(document, fragments) {
73 var fragmentMap = createFragmentMap(getFragmentDefinitions(document));
74 return {
75 fragmentMap: fragmentMap,
76 lookupFragment: function (name) {
77 var def = fragmentMap[name];
78 if (!def && fragments) {
79 def = fragments.lookup(name);
80 }
81 return def || null;
82 },
83 };
84}
85//# sourceMappingURL=helpers.js.map
\No newline at end of file