UNPKG

4.04 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 there is no object.id, fall back to object._id.
17 if (isNullish(id) && !isNullish(_id)) {
18 id = _id;
19 }
20 if (!isNullish(id)) {
21 return "".concat(__typename, ":").concat(typeof id === "number" || typeof id === "string" ?
22 id
23 : JSON.stringify(id));
24 }
25 }
26}
27var defaultConfig = {
28 dataIdFromObject: defaultDataIdFromObject,
29 addTypename: true,
30 resultCaching: true,
31 // Thanks to the shouldCanonizeResults helper, this should be the only line
32 // you have to change to reenable canonization by default in the future.
33 canonizeResults: false,
34};
35export function normalizeConfig(config) {
36 return compact(defaultConfig, config);
37}
38export function shouldCanonizeResults(config) {
39 var value = config.canonizeResults;
40 return value === void 0 ? defaultConfig.canonizeResults : value;
41}
42export function getTypenameFromStoreObject(store, objectOrReference) {
43 return isReference(objectOrReference) ?
44 store.get(objectOrReference.__ref, "__typename")
45 : objectOrReference && objectOrReference.__typename;
46}
47export var TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i;
48export function fieldNameFromStoreName(storeFieldName) {
49 var match = storeFieldName.match(TypeOrFieldNameRegExp);
50 return match ? match[0] : storeFieldName;
51}
52export function selectionSetMatchesResult(selectionSet, result, variables) {
53 if (isNonNullObject(result)) {
54 return isArray(result) ?
55 result.every(function (item) {
56 return selectionSetMatchesResult(selectionSet, item, variables);
57 })
58 : selectionSet.selections.every(function (field) {
59 if (isField(field) && shouldInclude(field, variables)) {
60 var key = resultKeyNameFromField(field);
61 return (hasOwn.call(result, key) &&
62 (!field.selectionSet ||
63 selectionSetMatchesResult(field.selectionSet, result[key], variables)));
64 }
65 // If the selection has been skipped with @skip(true) or
66 // @include(false), it should not count against the matching. If
67 // the selection is not a field, it must be a fragment (inline or
68 // named). We will determine if selectionSetMatchesResult for that
69 // fragment when we get to it, so for now we return true.
70 return true;
71 });
72 }
73 return false;
74}
75export function storeValueIsStoreObject(value) {
76 return isNonNullObject(value) && !isReference(value) && !isArray(value);
77}
78export function makeProcessedFieldsMerger() {
79 return new DeepMerger();
80}
81export function extractFragmentContext(document, fragments) {
82 // FragmentMap consisting only of fragments defined directly in document, not
83 // including other fragments registered in the FragmentRegistry.
84 var fragmentMap = createFragmentMap(getFragmentDefinitions(document));
85 return {
86 fragmentMap: fragmentMap,
87 lookupFragment: function (name) {
88 var def = fragmentMap[name];
89 if (!def && fragments) {
90 def = fragments.lookup(name);
91 }
92 return def || null;
93 },
94 };
95}
96//# sourceMappingURL=helpers.js.map
\No newline at end of file