@apollo/client 3.5.10 → 3.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/apollo-client.cjs +509 -341
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs.native.js +2289 -0
- package/core/ApolloClient.d.ts +2 -3
- package/core/ApolloClient.d.ts.map +1 -1
- package/core/ApolloClient.js +4 -8
- package/core/ApolloClient.js.map +1 -1
- package/core/ObservableQuery.d.ts +10 -4
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +97 -45
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.d.ts.map +1 -1
- package/core/QueryInfo.js +4 -2
- package/core/QueryInfo.js.map +1 -1
- package/core/QueryManager.d.ts +5 -2
- package/core/QueryManager.d.ts.map +1 -1
- package/core/QueryManager.js +29 -23
- package/core/QueryManager.js.map +1 -1
- package/core/core.cjs +138 -85
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +2141 -0
- package/core/index.d.ts +1 -1
- package/core/index.d.ts.map +1 -1
- package/core/index.js +1 -1
- package/core/index.js.map +1 -1
- package/core/watchQueryOptions.d.ts +9 -1
- package/core/watchQueryOptions.d.ts.map +1 -1
- package/core/watchQueryOptions.js.map +1 -1
- package/errors/errors.cjs.native.js +48 -0
- package/invariantErrorCodes.js +1 -1
- package/link/batch/batch.cjs +47 -37
- package/link/batch/batch.cjs.map +1 -1
- package/link/batch/batch.cjs.native.js +161 -0
- package/link/batch/batching.d.ts +2 -6
- package/link/batch/batching.d.ts.map +1 -1
- package/link/batch/batching.js +47 -37
- package/link/batch/batching.js.map +1 -1
- package/link/batch-http/batch-http.cjs.native.js +127 -0
- package/link/context/context.cjs.native.js +38 -0
- package/link/core/core.cjs.native.js +121 -0
- package/link/error/error.cjs.native.js +90 -0
- package/link/http/http.cjs.native.js +320 -0
- package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
- package/link/http/selectHttpOptionsAndBody.js.map +1 -1
- package/link/persisted-queries/index.d.ts +2 -1
- package/link/persisted-queries/index.d.ts.map +1 -1
- package/link/persisted-queries/index.js +26 -13
- package/link/persisted-queries/index.js.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs +25 -12
- package/link/persisted-queries/persisted-queries.cjs.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
- package/link/retry/retry.cjs.native.js +170 -0
- package/link/schema/schema.cjs.native.js +56 -0
- package/link/subscriptions/subscriptions.cjs.native.js +45 -0
- package/link/utils/utils.cjs.native.js +115 -0
- package/link/ws/ws.cjs.native.js +28 -0
- package/main.cjs.native.js +16 -0
- package/package.json +28 -26
- package/react/components/components.cjs.native.js +79 -0
- package/react/context/ApolloConsumer.js +2 -2
- package/react/context/ApolloProvider.js +2 -2
- package/react/context/context.cjs +4 -4
- package/react/context/context.cjs.map +1 -1
- package/react/context/context.cjs.native.js +67 -0
- package/react/hoc/hoc.cjs.native.js +325 -0
- package/react/hooks/hooks.cjs +351 -247
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +604 -0
- package/react/hooks/index.d.ts +1 -1
- package/react/hooks/index.d.ts.map +1 -1
- package/react/hooks/index.js +1 -1
- package/react/hooks/index.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts +2 -2
- package/react/hooks/useLazyQuery.d.ts.map +1 -1
- package/react/hooks/useLazyQuery.js +22 -21
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts.map +1 -1
- package/react/hooks/useMutation.js +6 -3
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +36 -2
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +238 -204
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSubscription.d.ts.map +1 -1
- package/react/hooks/useSubscription.js +17 -7
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSyncExternalStore.d.ts +4 -0
- package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
- package/react/hooks/useSyncExternalStore.js +48 -0
- package/react/hooks/useSyncExternalStore.js.map +1 -0
- package/react/parser/index.d.ts.map +1 -1
- package/react/parser/index.js +24 -10
- package/react/parser/index.js.map +1 -1
- package/react/parser/parser.cjs +24 -10
- package/react/parser/parser.cjs.map +1 -1
- package/react/parser/parser.cjs.native.js +103 -0
- package/react/react.cjs.native.js +22 -0
- package/react/ssr/RenderPromises.d.ts +3 -2
- package/react/ssr/RenderPromises.d.ts.map +1 -1
- package/react/ssr/RenderPromises.js +25 -3
- package/react/ssr/RenderPromises.js.map +1 -1
- package/react/ssr/ssr.cjs +25 -3
- package/react/ssr/ssr.cjs.map +1 -1
- package/react/ssr/ssr.cjs.native.js +150 -0
- package/react/types/types.d.ts +10 -11
- package/react/types/types.d.ts.map +1 -1
- package/react/types/types.js.map +1 -1
- package/testing/core/core.cjs.native.js +288 -0
- package/testing/core/mocking/mockFetch.js +1 -1
- package/testing/core/mocking/mockFetch.js.map +1 -1
- package/testing/core/mocking/mockQueryManager.js +1 -1
- package/testing/core/mocking/mockWatchQuery.js +1 -1
- package/testing/core/wrap.js +1 -1
- package/testing/testing.cjs.native.js +58 -0
- package/utilities/common/canUse.d.ts +2 -0
- package/utilities/common/canUse.d.ts.map +1 -1
- package/utilities/common/canUse.js +6 -2
- package/utilities/common/canUse.js.map +1 -1
- package/utilities/common/mergeOptions.d.ts +5 -0
- package/utilities/common/mergeOptions.d.ts.map +1 -0
- package/utilities/common/mergeOptions.js +8 -0
- package/utilities/common/mergeOptions.js.map +1 -0
- package/utilities/globals/global.js +1 -2
- package/utilities/globals/globals.cjs.native.js +56 -0
- package/utilities/graphql/transform.d.ts +2 -2
- package/utilities/graphql/transform.d.ts.map +1 -1
- package/utilities/graphql/transform.js +1 -1
- package/utilities/graphql/transform.js.map +1 -1
- package/utilities/index.d.ts +1 -0
- package/utilities/index.d.ts.map +1 -1
- package/utilities/index.js +1 -0
- package/utilities/index.js.map +1 -1
- package/utilities/observables/Concast.d.ts.map +1 -1
- package/utilities/observables/Concast.js +5 -2
- package/utilities/observables/Concast.js.map +1 -1
- package/utilities/policies/pagination.d.ts.map +1 -1
- package/utilities/policies/pagination.js +9 -7
- package/utilities/policies/pagination.js.map +1 -1
- package/utilities/utilities.cjs +29 -12
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +1284 -0
- package/version.js +1 -1
|
@@ -0,0 +1,2289 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var globals = require('../utilities/globals');
|
|
6
|
+
var tslib = require('tslib');
|
|
7
|
+
var optimism = require('optimism');
|
|
8
|
+
var utilities = require('../utilities');
|
|
9
|
+
var equality = require('@wry/equality');
|
|
10
|
+
var trie = require('@wry/trie');
|
|
11
|
+
var context = require('@wry/context');
|
|
12
|
+
|
|
13
|
+
var ApolloCache = (function () {
|
|
14
|
+
function ApolloCache() {
|
|
15
|
+
this.getFragmentDoc = optimism.wrap(utilities.getFragmentQueryDocument);
|
|
16
|
+
}
|
|
17
|
+
ApolloCache.prototype.batch = function (options) {
|
|
18
|
+
var _this = this;
|
|
19
|
+
var optimisticId = typeof options.optimistic === "string" ? options.optimistic :
|
|
20
|
+
options.optimistic === false ? null : void 0;
|
|
21
|
+
var updateResult;
|
|
22
|
+
this.performTransaction(function () { return updateResult = options.update(_this); }, optimisticId);
|
|
23
|
+
return updateResult;
|
|
24
|
+
};
|
|
25
|
+
ApolloCache.prototype.recordOptimisticTransaction = function (transaction, optimisticId) {
|
|
26
|
+
this.performTransaction(transaction, optimisticId);
|
|
27
|
+
};
|
|
28
|
+
ApolloCache.prototype.transformDocument = function (document) {
|
|
29
|
+
return document;
|
|
30
|
+
};
|
|
31
|
+
ApolloCache.prototype.identify = function (object) {
|
|
32
|
+
return;
|
|
33
|
+
};
|
|
34
|
+
ApolloCache.prototype.gc = function () {
|
|
35
|
+
return [];
|
|
36
|
+
};
|
|
37
|
+
ApolloCache.prototype.modify = function (options) {
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
40
|
+
ApolloCache.prototype.transformForLink = function (document) {
|
|
41
|
+
return document;
|
|
42
|
+
};
|
|
43
|
+
ApolloCache.prototype.readQuery = function (options, optimistic) {
|
|
44
|
+
if (optimistic === void 0) { optimistic = !!options.optimistic; }
|
|
45
|
+
return this.read(tslib.__assign(tslib.__assign({}, options), { rootId: options.id || 'ROOT_QUERY', optimistic: optimistic }));
|
|
46
|
+
};
|
|
47
|
+
ApolloCache.prototype.readFragment = function (options, optimistic) {
|
|
48
|
+
if (optimistic === void 0) { optimistic = !!options.optimistic; }
|
|
49
|
+
return this.read(tslib.__assign(tslib.__assign({}, options), { query: this.getFragmentDoc(options.fragment, options.fragmentName), rootId: options.id, optimistic: optimistic }));
|
|
50
|
+
};
|
|
51
|
+
ApolloCache.prototype.writeQuery = function (_a) {
|
|
52
|
+
var id = _a.id, data = _a.data, options = tslib.__rest(_a, ["id", "data"]);
|
|
53
|
+
return this.write(Object.assign(options, {
|
|
54
|
+
dataId: id || 'ROOT_QUERY',
|
|
55
|
+
result: data,
|
|
56
|
+
}));
|
|
57
|
+
};
|
|
58
|
+
ApolloCache.prototype.writeFragment = function (_a) {
|
|
59
|
+
var id = _a.id, data = _a.data, fragment = _a.fragment, fragmentName = _a.fragmentName, options = tslib.__rest(_a, ["id", "data", "fragment", "fragmentName"]);
|
|
60
|
+
return this.write(Object.assign(options, {
|
|
61
|
+
query: this.getFragmentDoc(fragment, fragmentName),
|
|
62
|
+
dataId: id,
|
|
63
|
+
result: data,
|
|
64
|
+
}));
|
|
65
|
+
};
|
|
66
|
+
ApolloCache.prototype.updateQuery = function (options, update) {
|
|
67
|
+
return this.batch({
|
|
68
|
+
update: function (cache) {
|
|
69
|
+
var value = cache.readQuery(options);
|
|
70
|
+
var data = update(value);
|
|
71
|
+
if (data === void 0 || data === null)
|
|
72
|
+
return value;
|
|
73
|
+
cache.writeQuery(tslib.__assign(tslib.__assign({}, options), { data: data }));
|
|
74
|
+
return data;
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
ApolloCache.prototype.updateFragment = function (options, update) {
|
|
79
|
+
return this.batch({
|
|
80
|
+
update: function (cache) {
|
|
81
|
+
var value = cache.readFragment(options);
|
|
82
|
+
var data = update(value);
|
|
83
|
+
if (data === void 0 || data === null)
|
|
84
|
+
return value;
|
|
85
|
+
cache.writeFragment(tslib.__assign(tslib.__assign({}, options), { data: data }));
|
|
86
|
+
return data;
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
return ApolloCache;
|
|
91
|
+
}());
|
|
92
|
+
|
|
93
|
+
exports.Cache = void 0;
|
|
94
|
+
(function (Cache) {
|
|
95
|
+
})(exports.Cache || (exports.Cache = {}));
|
|
96
|
+
|
|
97
|
+
var MissingFieldError = (function () {
|
|
98
|
+
function MissingFieldError(message, path, query, variables) {
|
|
99
|
+
this.message = message;
|
|
100
|
+
this.path = path;
|
|
101
|
+
this.query = query;
|
|
102
|
+
this.variables = variables;
|
|
103
|
+
}
|
|
104
|
+
return MissingFieldError;
|
|
105
|
+
}());
|
|
106
|
+
|
|
107
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
108
|
+
function defaultDataIdFromObject(_a, context) {
|
|
109
|
+
var __typename = _a.__typename, id = _a.id, _id = _a._id;
|
|
110
|
+
if (typeof __typename === "string") {
|
|
111
|
+
if (context) {
|
|
112
|
+
context.keyObject =
|
|
113
|
+
id !== void 0 ? { id: id } :
|
|
114
|
+
_id !== void 0 ? { _id: _id } :
|
|
115
|
+
void 0;
|
|
116
|
+
}
|
|
117
|
+
if (id === void 0)
|
|
118
|
+
id = _id;
|
|
119
|
+
if (id !== void 0) {
|
|
120
|
+
return "".concat(__typename, ":").concat((typeof id === "number" ||
|
|
121
|
+
typeof id === "string") ? id : JSON.stringify(id));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
var defaultConfig = {
|
|
126
|
+
dataIdFromObject: defaultDataIdFromObject,
|
|
127
|
+
addTypename: true,
|
|
128
|
+
resultCaching: true,
|
|
129
|
+
canonizeResults: false,
|
|
130
|
+
};
|
|
131
|
+
function normalizeConfig(config) {
|
|
132
|
+
return utilities.compact(defaultConfig, config);
|
|
133
|
+
}
|
|
134
|
+
function shouldCanonizeResults(config) {
|
|
135
|
+
var value = config.canonizeResults;
|
|
136
|
+
return value === void 0 ? defaultConfig.canonizeResults : value;
|
|
137
|
+
}
|
|
138
|
+
function getTypenameFromStoreObject(store, objectOrReference) {
|
|
139
|
+
return utilities.isReference(objectOrReference)
|
|
140
|
+
? store.get(objectOrReference.__ref, "__typename")
|
|
141
|
+
: objectOrReference && objectOrReference.__typename;
|
|
142
|
+
}
|
|
143
|
+
var TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i;
|
|
144
|
+
function fieldNameFromStoreName(storeFieldName) {
|
|
145
|
+
var match = storeFieldName.match(TypeOrFieldNameRegExp);
|
|
146
|
+
return match ? match[0] : storeFieldName;
|
|
147
|
+
}
|
|
148
|
+
function selectionSetMatchesResult(selectionSet, result, variables) {
|
|
149
|
+
if (utilities.isNonNullObject(result)) {
|
|
150
|
+
return isArray(result)
|
|
151
|
+
? result.every(function (item) { return selectionSetMatchesResult(selectionSet, item, variables); })
|
|
152
|
+
: selectionSet.selections.every(function (field) {
|
|
153
|
+
if (utilities.isField(field) && utilities.shouldInclude(field, variables)) {
|
|
154
|
+
var key = utilities.resultKeyNameFromField(field);
|
|
155
|
+
return hasOwn.call(result, key) &&
|
|
156
|
+
(!field.selectionSet ||
|
|
157
|
+
selectionSetMatchesResult(field.selectionSet, result[key], variables));
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
function storeValueIsStoreObject(value) {
|
|
165
|
+
return utilities.isNonNullObject(value) &&
|
|
166
|
+
!utilities.isReference(value) &&
|
|
167
|
+
!isArray(value);
|
|
168
|
+
}
|
|
169
|
+
function makeProcessedFieldsMerger() {
|
|
170
|
+
return new utilities.DeepMerger;
|
|
171
|
+
}
|
|
172
|
+
var isArray = function (a) { return Array.isArray(a); };
|
|
173
|
+
|
|
174
|
+
var DELETE = Object.create(null);
|
|
175
|
+
var delModifier = function () { return DELETE; };
|
|
176
|
+
var INVALIDATE = Object.create(null);
|
|
177
|
+
exports.EntityStore = (function () {
|
|
178
|
+
function EntityStore(policies, group) {
|
|
179
|
+
var _this = this;
|
|
180
|
+
this.policies = policies;
|
|
181
|
+
this.group = group;
|
|
182
|
+
this.data = Object.create(null);
|
|
183
|
+
this.rootIds = Object.create(null);
|
|
184
|
+
this.refs = Object.create(null);
|
|
185
|
+
this.getFieldValue = function (objectOrReference, storeFieldName) { return utilities.maybeDeepFreeze(utilities.isReference(objectOrReference)
|
|
186
|
+
? _this.get(objectOrReference.__ref, storeFieldName)
|
|
187
|
+
: objectOrReference && objectOrReference[storeFieldName]); };
|
|
188
|
+
this.canRead = function (objOrRef) {
|
|
189
|
+
return utilities.isReference(objOrRef)
|
|
190
|
+
? _this.has(objOrRef.__ref)
|
|
191
|
+
: typeof objOrRef === "object";
|
|
192
|
+
};
|
|
193
|
+
this.toReference = function (objOrIdOrRef, mergeIntoStore) {
|
|
194
|
+
if (typeof objOrIdOrRef === "string") {
|
|
195
|
+
return utilities.makeReference(objOrIdOrRef);
|
|
196
|
+
}
|
|
197
|
+
if (utilities.isReference(objOrIdOrRef)) {
|
|
198
|
+
return objOrIdOrRef;
|
|
199
|
+
}
|
|
200
|
+
var id = _this.policies.identify(objOrIdOrRef)[0];
|
|
201
|
+
if (id) {
|
|
202
|
+
var ref = utilities.makeReference(id);
|
|
203
|
+
if (mergeIntoStore) {
|
|
204
|
+
_this.merge(id, objOrIdOrRef);
|
|
205
|
+
}
|
|
206
|
+
return ref;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
EntityStore.prototype.toObject = function () {
|
|
211
|
+
return tslib.__assign({}, this.data);
|
|
212
|
+
};
|
|
213
|
+
EntityStore.prototype.has = function (dataId) {
|
|
214
|
+
return this.lookup(dataId, true) !== void 0;
|
|
215
|
+
};
|
|
216
|
+
EntityStore.prototype.get = function (dataId, fieldName) {
|
|
217
|
+
this.group.depend(dataId, fieldName);
|
|
218
|
+
if (hasOwn.call(this.data, dataId)) {
|
|
219
|
+
var storeObject = this.data[dataId];
|
|
220
|
+
if (storeObject && hasOwn.call(storeObject, fieldName)) {
|
|
221
|
+
return storeObject[fieldName];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (fieldName === "__typename" &&
|
|
225
|
+
hasOwn.call(this.policies.rootTypenamesById, dataId)) {
|
|
226
|
+
return this.policies.rootTypenamesById[dataId];
|
|
227
|
+
}
|
|
228
|
+
if (this instanceof Layer) {
|
|
229
|
+
return this.parent.get(dataId, fieldName);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
EntityStore.prototype.lookup = function (dataId, dependOnExistence) {
|
|
233
|
+
if (dependOnExistence)
|
|
234
|
+
this.group.depend(dataId, "__exists");
|
|
235
|
+
if (hasOwn.call(this.data, dataId)) {
|
|
236
|
+
return this.data[dataId];
|
|
237
|
+
}
|
|
238
|
+
if (this instanceof Layer) {
|
|
239
|
+
return this.parent.lookup(dataId, dependOnExistence);
|
|
240
|
+
}
|
|
241
|
+
if (this.policies.rootTypenamesById[dataId]) {
|
|
242
|
+
return Object.create(null);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
EntityStore.prototype.merge = function (older, newer) {
|
|
246
|
+
var _this = this;
|
|
247
|
+
var dataId;
|
|
248
|
+
if (utilities.isReference(older))
|
|
249
|
+
older = older.__ref;
|
|
250
|
+
if (utilities.isReference(newer))
|
|
251
|
+
newer = newer.__ref;
|
|
252
|
+
var existing = typeof older === "string"
|
|
253
|
+
? this.lookup(dataId = older)
|
|
254
|
+
: older;
|
|
255
|
+
var incoming = typeof newer === "string"
|
|
256
|
+
? this.lookup(dataId = newer)
|
|
257
|
+
: newer;
|
|
258
|
+
if (!incoming)
|
|
259
|
+
return;
|
|
260
|
+
__DEV__ ? globals.invariant(typeof dataId === "string", "store.merge expects a string ID") : globals.invariant(typeof dataId === "string", 1);
|
|
261
|
+
var merged = new utilities.DeepMerger(storeObjectReconciler).merge(existing, incoming);
|
|
262
|
+
this.data[dataId] = merged;
|
|
263
|
+
if (merged !== existing) {
|
|
264
|
+
delete this.refs[dataId];
|
|
265
|
+
if (this.group.caching) {
|
|
266
|
+
var fieldsToDirty_1 = Object.create(null);
|
|
267
|
+
if (!existing)
|
|
268
|
+
fieldsToDirty_1.__exists = 1;
|
|
269
|
+
Object.keys(incoming).forEach(function (storeFieldName) {
|
|
270
|
+
if (!existing || existing[storeFieldName] !== merged[storeFieldName]) {
|
|
271
|
+
fieldsToDirty_1[storeFieldName] = 1;
|
|
272
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
273
|
+
if (fieldName !== storeFieldName &&
|
|
274
|
+
!_this.policies.hasKeyArgs(merged.__typename, fieldName)) {
|
|
275
|
+
fieldsToDirty_1[fieldName] = 1;
|
|
276
|
+
}
|
|
277
|
+
if (merged[storeFieldName] === void 0 && !(_this instanceof Layer)) {
|
|
278
|
+
delete merged[storeFieldName];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
if (fieldsToDirty_1.__typename &&
|
|
283
|
+
!(existing && existing.__typename) &&
|
|
284
|
+
this.policies.rootTypenamesById[dataId] === merged.__typename) {
|
|
285
|
+
delete fieldsToDirty_1.__typename;
|
|
286
|
+
}
|
|
287
|
+
Object.keys(fieldsToDirty_1).forEach(function (fieldName) { return _this.group.dirty(dataId, fieldName); });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
EntityStore.prototype.modify = function (dataId, fields) {
|
|
292
|
+
var _this = this;
|
|
293
|
+
var storeObject = this.lookup(dataId);
|
|
294
|
+
if (storeObject) {
|
|
295
|
+
var changedFields_1 = Object.create(null);
|
|
296
|
+
var needToMerge_1 = false;
|
|
297
|
+
var allDeleted_1 = true;
|
|
298
|
+
var sharedDetails_1 = {
|
|
299
|
+
DELETE: DELETE,
|
|
300
|
+
INVALIDATE: INVALIDATE,
|
|
301
|
+
isReference: utilities.isReference,
|
|
302
|
+
toReference: this.toReference,
|
|
303
|
+
canRead: this.canRead,
|
|
304
|
+
readField: function (fieldNameOrOptions, from) { return _this.policies.readField(typeof fieldNameOrOptions === "string" ? {
|
|
305
|
+
fieldName: fieldNameOrOptions,
|
|
306
|
+
from: from || utilities.makeReference(dataId),
|
|
307
|
+
} : fieldNameOrOptions, { store: _this }); },
|
|
308
|
+
};
|
|
309
|
+
Object.keys(storeObject).forEach(function (storeFieldName) {
|
|
310
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
311
|
+
var fieldValue = storeObject[storeFieldName];
|
|
312
|
+
if (fieldValue === void 0)
|
|
313
|
+
return;
|
|
314
|
+
var modify = typeof fields === "function"
|
|
315
|
+
? fields
|
|
316
|
+
: fields[storeFieldName] || fields[fieldName];
|
|
317
|
+
if (modify) {
|
|
318
|
+
var newValue = modify === delModifier ? DELETE :
|
|
319
|
+
modify(utilities.maybeDeepFreeze(fieldValue), tslib.__assign(tslib.__assign({}, sharedDetails_1), { fieldName: fieldName, storeFieldName: storeFieldName, storage: _this.getStorage(dataId, storeFieldName) }));
|
|
320
|
+
if (newValue === INVALIDATE) {
|
|
321
|
+
_this.group.dirty(dataId, storeFieldName);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
if (newValue === DELETE)
|
|
325
|
+
newValue = void 0;
|
|
326
|
+
if (newValue !== fieldValue) {
|
|
327
|
+
changedFields_1[storeFieldName] = newValue;
|
|
328
|
+
needToMerge_1 = true;
|
|
329
|
+
fieldValue = newValue;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (fieldValue !== void 0) {
|
|
334
|
+
allDeleted_1 = false;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
if (needToMerge_1) {
|
|
338
|
+
this.merge(dataId, changedFields_1);
|
|
339
|
+
if (allDeleted_1) {
|
|
340
|
+
if (this instanceof Layer) {
|
|
341
|
+
this.data[dataId] = void 0;
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
delete this.data[dataId];
|
|
345
|
+
}
|
|
346
|
+
this.group.dirty(dataId, "__exists");
|
|
347
|
+
}
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return false;
|
|
352
|
+
};
|
|
353
|
+
EntityStore.prototype.delete = function (dataId, fieldName, args) {
|
|
354
|
+
var _a;
|
|
355
|
+
var storeObject = this.lookup(dataId);
|
|
356
|
+
if (storeObject) {
|
|
357
|
+
var typename = this.getFieldValue(storeObject, "__typename");
|
|
358
|
+
var storeFieldName = fieldName && args
|
|
359
|
+
? this.policies.getStoreFieldName({ typename: typename, fieldName: fieldName, args: args })
|
|
360
|
+
: fieldName;
|
|
361
|
+
return this.modify(dataId, storeFieldName ? (_a = {},
|
|
362
|
+
_a[storeFieldName] = delModifier,
|
|
363
|
+
_a) : delModifier);
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
};
|
|
367
|
+
EntityStore.prototype.evict = function (options, limit) {
|
|
368
|
+
var evicted = false;
|
|
369
|
+
if (options.id) {
|
|
370
|
+
if (hasOwn.call(this.data, options.id)) {
|
|
371
|
+
evicted = this.delete(options.id, options.fieldName, options.args);
|
|
372
|
+
}
|
|
373
|
+
if (this instanceof Layer && this !== limit) {
|
|
374
|
+
evicted = this.parent.evict(options, limit) || evicted;
|
|
375
|
+
}
|
|
376
|
+
if (options.fieldName || evicted) {
|
|
377
|
+
this.group.dirty(options.id, options.fieldName || "__exists");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return evicted;
|
|
381
|
+
};
|
|
382
|
+
EntityStore.prototype.clear = function () {
|
|
383
|
+
this.replace(null);
|
|
384
|
+
};
|
|
385
|
+
EntityStore.prototype.extract = function () {
|
|
386
|
+
var _this = this;
|
|
387
|
+
var obj = this.toObject();
|
|
388
|
+
var extraRootIds = [];
|
|
389
|
+
this.getRootIdSet().forEach(function (id) {
|
|
390
|
+
if (!hasOwn.call(_this.policies.rootTypenamesById, id)) {
|
|
391
|
+
extraRootIds.push(id);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
if (extraRootIds.length) {
|
|
395
|
+
obj.__META = { extraRootIds: extraRootIds.sort() };
|
|
396
|
+
}
|
|
397
|
+
return obj;
|
|
398
|
+
};
|
|
399
|
+
EntityStore.prototype.replace = function (newData) {
|
|
400
|
+
var _this = this;
|
|
401
|
+
Object.keys(this.data).forEach(function (dataId) {
|
|
402
|
+
if (!(newData && hasOwn.call(newData, dataId))) {
|
|
403
|
+
_this.delete(dataId);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
if (newData) {
|
|
407
|
+
var __META = newData.__META, rest_1 = tslib.__rest(newData, ["__META"]);
|
|
408
|
+
Object.keys(rest_1).forEach(function (dataId) {
|
|
409
|
+
_this.merge(dataId, rest_1[dataId]);
|
|
410
|
+
});
|
|
411
|
+
if (__META) {
|
|
412
|
+
__META.extraRootIds.forEach(this.retain, this);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
EntityStore.prototype.retain = function (rootId) {
|
|
417
|
+
return this.rootIds[rootId] = (this.rootIds[rootId] || 0) + 1;
|
|
418
|
+
};
|
|
419
|
+
EntityStore.prototype.release = function (rootId) {
|
|
420
|
+
if (this.rootIds[rootId] > 0) {
|
|
421
|
+
var count = --this.rootIds[rootId];
|
|
422
|
+
if (!count)
|
|
423
|
+
delete this.rootIds[rootId];
|
|
424
|
+
return count;
|
|
425
|
+
}
|
|
426
|
+
return 0;
|
|
427
|
+
};
|
|
428
|
+
EntityStore.prototype.getRootIdSet = function (ids) {
|
|
429
|
+
if (ids === void 0) { ids = new Set(); }
|
|
430
|
+
Object.keys(this.rootIds).forEach(ids.add, ids);
|
|
431
|
+
if (this instanceof Layer) {
|
|
432
|
+
this.parent.getRootIdSet(ids);
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
Object.keys(this.policies.rootTypenamesById).forEach(ids.add, ids);
|
|
436
|
+
}
|
|
437
|
+
return ids;
|
|
438
|
+
};
|
|
439
|
+
EntityStore.prototype.gc = function () {
|
|
440
|
+
var _this = this;
|
|
441
|
+
var ids = this.getRootIdSet();
|
|
442
|
+
var snapshot = this.toObject();
|
|
443
|
+
ids.forEach(function (id) {
|
|
444
|
+
if (hasOwn.call(snapshot, id)) {
|
|
445
|
+
Object.keys(_this.findChildRefIds(id)).forEach(ids.add, ids);
|
|
446
|
+
delete snapshot[id];
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
var idsToRemove = Object.keys(snapshot);
|
|
450
|
+
if (idsToRemove.length) {
|
|
451
|
+
var root_1 = this;
|
|
452
|
+
while (root_1 instanceof Layer)
|
|
453
|
+
root_1 = root_1.parent;
|
|
454
|
+
idsToRemove.forEach(function (id) { return root_1.delete(id); });
|
|
455
|
+
}
|
|
456
|
+
return idsToRemove;
|
|
457
|
+
};
|
|
458
|
+
EntityStore.prototype.findChildRefIds = function (dataId) {
|
|
459
|
+
if (!hasOwn.call(this.refs, dataId)) {
|
|
460
|
+
var found_1 = this.refs[dataId] = Object.create(null);
|
|
461
|
+
var root = this.data[dataId];
|
|
462
|
+
if (!root)
|
|
463
|
+
return found_1;
|
|
464
|
+
var workSet_1 = new Set([root]);
|
|
465
|
+
workSet_1.forEach(function (obj) {
|
|
466
|
+
if (utilities.isReference(obj)) {
|
|
467
|
+
found_1[obj.__ref] = true;
|
|
468
|
+
}
|
|
469
|
+
if (utilities.isNonNullObject(obj)) {
|
|
470
|
+
Object.keys(obj).forEach(function (key) {
|
|
471
|
+
var child = obj[key];
|
|
472
|
+
if (utilities.isNonNullObject(child)) {
|
|
473
|
+
workSet_1.add(child);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
return this.refs[dataId];
|
|
480
|
+
};
|
|
481
|
+
EntityStore.prototype.makeCacheKey = function () {
|
|
482
|
+
return this.group.keyMaker.lookupArray(arguments);
|
|
483
|
+
};
|
|
484
|
+
return EntityStore;
|
|
485
|
+
}());
|
|
486
|
+
var CacheGroup = (function () {
|
|
487
|
+
function CacheGroup(caching, parent) {
|
|
488
|
+
if (parent === void 0) { parent = null; }
|
|
489
|
+
this.caching = caching;
|
|
490
|
+
this.parent = parent;
|
|
491
|
+
this.d = null;
|
|
492
|
+
this.resetCaching();
|
|
493
|
+
}
|
|
494
|
+
CacheGroup.prototype.resetCaching = function () {
|
|
495
|
+
this.d = this.caching ? optimism.dep() : null;
|
|
496
|
+
this.keyMaker = new trie.Trie(utilities.canUseWeakMap);
|
|
497
|
+
};
|
|
498
|
+
CacheGroup.prototype.depend = function (dataId, storeFieldName) {
|
|
499
|
+
if (this.d) {
|
|
500
|
+
this.d(makeDepKey(dataId, storeFieldName));
|
|
501
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
502
|
+
if (fieldName !== storeFieldName) {
|
|
503
|
+
this.d(makeDepKey(dataId, fieldName));
|
|
504
|
+
}
|
|
505
|
+
if (this.parent) {
|
|
506
|
+
this.parent.depend(dataId, storeFieldName);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
CacheGroup.prototype.dirty = function (dataId, storeFieldName) {
|
|
511
|
+
if (this.d) {
|
|
512
|
+
this.d.dirty(makeDepKey(dataId, storeFieldName), storeFieldName === "__exists" ? "forget" : "setDirty");
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
return CacheGroup;
|
|
516
|
+
}());
|
|
517
|
+
function makeDepKey(dataId, storeFieldName) {
|
|
518
|
+
return storeFieldName + '#' + dataId;
|
|
519
|
+
}
|
|
520
|
+
function maybeDependOnExistenceOfEntity(store, entityId) {
|
|
521
|
+
if (supportsResultCaching(store)) {
|
|
522
|
+
store.group.depend(entityId, "__exists");
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
(function (EntityStore) {
|
|
526
|
+
var Root = (function (_super) {
|
|
527
|
+
tslib.__extends(Root, _super);
|
|
528
|
+
function Root(_a) {
|
|
529
|
+
var policies = _a.policies, _b = _a.resultCaching, resultCaching = _b === void 0 ? true : _b, seed = _a.seed;
|
|
530
|
+
var _this = _super.call(this, policies, new CacheGroup(resultCaching)) || this;
|
|
531
|
+
_this.stump = new Stump(_this);
|
|
532
|
+
_this.storageTrie = new trie.Trie(utilities.canUseWeakMap);
|
|
533
|
+
if (seed)
|
|
534
|
+
_this.replace(seed);
|
|
535
|
+
return _this;
|
|
536
|
+
}
|
|
537
|
+
Root.prototype.addLayer = function (layerId, replay) {
|
|
538
|
+
return this.stump.addLayer(layerId, replay);
|
|
539
|
+
};
|
|
540
|
+
Root.prototype.removeLayer = function () {
|
|
541
|
+
return this;
|
|
542
|
+
};
|
|
543
|
+
Root.prototype.getStorage = function () {
|
|
544
|
+
return this.storageTrie.lookupArray(arguments);
|
|
545
|
+
};
|
|
546
|
+
return Root;
|
|
547
|
+
}(EntityStore));
|
|
548
|
+
EntityStore.Root = Root;
|
|
549
|
+
})(exports.EntityStore || (exports.EntityStore = {}));
|
|
550
|
+
var Layer = (function (_super) {
|
|
551
|
+
tslib.__extends(Layer, _super);
|
|
552
|
+
function Layer(id, parent, replay, group) {
|
|
553
|
+
var _this = _super.call(this, parent.policies, group) || this;
|
|
554
|
+
_this.id = id;
|
|
555
|
+
_this.parent = parent;
|
|
556
|
+
_this.replay = replay;
|
|
557
|
+
_this.group = group;
|
|
558
|
+
replay(_this);
|
|
559
|
+
return _this;
|
|
560
|
+
}
|
|
561
|
+
Layer.prototype.addLayer = function (layerId, replay) {
|
|
562
|
+
return new Layer(layerId, this, replay, this.group);
|
|
563
|
+
};
|
|
564
|
+
Layer.prototype.removeLayer = function (layerId) {
|
|
565
|
+
var _this = this;
|
|
566
|
+
var parent = this.parent.removeLayer(layerId);
|
|
567
|
+
if (layerId === this.id) {
|
|
568
|
+
if (this.group.caching) {
|
|
569
|
+
Object.keys(this.data).forEach(function (dataId) {
|
|
570
|
+
var ownStoreObject = _this.data[dataId];
|
|
571
|
+
var parentStoreObject = parent["lookup"](dataId);
|
|
572
|
+
if (!parentStoreObject) {
|
|
573
|
+
_this.delete(dataId);
|
|
574
|
+
}
|
|
575
|
+
else if (!ownStoreObject) {
|
|
576
|
+
_this.group.dirty(dataId, "__exists");
|
|
577
|
+
Object.keys(parentStoreObject).forEach(function (storeFieldName) {
|
|
578
|
+
_this.group.dirty(dataId, storeFieldName);
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
else if (ownStoreObject !== parentStoreObject) {
|
|
582
|
+
Object.keys(ownStoreObject).forEach(function (storeFieldName) {
|
|
583
|
+
if (!equality.equal(ownStoreObject[storeFieldName], parentStoreObject[storeFieldName])) {
|
|
584
|
+
_this.group.dirty(dataId, storeFieldName);
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
return parent;
|
|
591
|
+
}
|
|
592
|
+
if (parent === this.parent)
|
|
593
|
+
return this;
|
|
594
|
+
return parent.addLayer(this.id, this.replay);
|
|
595
|
+
};
|
|
596
|
+
Layer.prototype.toObject = function () {
|
|
597
|
+
return tslib.__assign(tslib.__assign({}, this.parent.toObject()), this.data);
|
|
598
|
+
};
|
|
599
|
+
Layer.prototype.findChildRefIds = function (dataId) {
|
|
600
|
+
var fromParent = this.parent.findChildRefIds(dataId);
|
|
601
|
+
return hasOwn.call(this.data, dataId) ? tslib.__assign(tslib.__assign({}, fromParent), _super.prototype.findChildRefIds.call(this, dataId)) : fromParent;
|
|
602
|
+
};
|
|
603
|
+
Layer.prototype.getStorage = function () {
|
|
604
|
+
var p = this.parent;
|
|
605
|
+
while (p.parent)
|
|
606
|
+
p = p.parent;
|
|
607
|
+
return p.getStorage.apply(p, arguments);
|
|
608
|
+
};
|
|
609
|
+
return Layer;
|
|
610
|
+
}(exports.EntityStore));
|
|
611
|
+
var Stump = (function (_super) {
|
|
612
|
+
tslib.__extends(Stump, _super);
|
|
613
|
+
function Stump(root) {
|
|
614
|
+
return _super.call(this, "EntityStore.Stump", root, function () { }, new CacheGroup(root.group.caching, root.group)) || this;
|
|
615
|
+
}
|
|
616
|
+
Stump.prototype.removeLayer = function () {
|
|
617
|
+
return this;
|
|
618
|
+
};
|
|
619
|
+
Stump.prototype.merge = function () {
|
|
620
|
+
return this.parent.merge.apply(this.parent, arguments);
|
|
621
|
+
};
|
|
622
|
+
return Stump;
|
|
623
|
+
}(Layer));
|
|
624
|
+
function storeObjectReconciler(existingObject, incomingObject, property) {
|
|
625
|
+
var existingValue = existingObject[property];
|
|
626
|
+
var incomingValue = incomingObject[property];
|
|
627
|
+
return equality.equal(existingValue, incomingValue) ? existingValue : incomingValue;
|
|
628
|
+
}
|
|
629
|
+
function supportsResultCaching(store) {
|
|
630
|
+
return !!(store instanceof exports.EntityStore && store.group.caching);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function shallowCopy(value) {
|
|
634
|
+
if (utilities.isNonNullObject(value)) {
|
|
635
|
+
return isArray(value)
|
|
636
|
+
? value.slice(0)
|
|
637
|
+
: tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
|
|
638
|
+
}
|
|
639
|
+
return value;
|
|
640
|
+
}
|
|
641
|
+
var ObjectCanon = (function () {
|
|
642
|
+
function ObjectCanon() {
|
|
643
|
+
this.known = new (utilities.canUseWeakSet ? WeakSet : Set)();
|
|
644
|
+
this.pool = new trie.Trie(utilities.canUseWeakMap);
|
|
645
|
+
this.passes = new WeakMap();
|
|
646
|
+
this.keysByJSON = new Map();
|
|
647
|
+
this.empty = this.admit({});
|
|
648
|
+
}
|
|
649
|
+
ObjectCanon.prototype.isKnown = function (value) {
|
|
650
|
+
return utilities.isNonNullObject(value) && this.known.has(value);
|
|
651
|
+
};
|
|
652
|
+
ObjectCanon.prototype.pass = function (value) {
|
|
653
|
+
if (utilities.isNonNullObject(value)) {
|
|
654
|
+
var copy = shallowCopy(value);
|
|
655
|
+
this.passes.set(copy, value);
|
|
656
|
+
return copy;
|
|
657
|
+
}
|
|
658
|
+
return value;
|
|
659
|
+
};
|
|
660
|
+
ObjectCanon.prototype.admit = function (value) {
|
|
661
|
+
var _this = this;
|
|
662
|
+
if (utilities.isNonNullObject(value)) {
|
|
663
|
+
var original = this.passes.get(value);
|
|
664
|
+
if (original)
|
|
665
|
+
return original;
|
|
666
|
+
var proto = Object.getPrototypeOf(value);
|
|
667
|
+
switch (proto) {
|
|
668
|
+
case Array.prototype: {
|
|
669
|
+
if (this.known.has(value))
|
|
670
|
+
return value;
|
|
671
|
+
var array = value.map(this.admit, this);
|
|
672
|
+
var node = this.pool.lookupArray(array);
|
|
673
|
+
if (!node.array) {
|
|
674
|
+
this.known.add(node.array = array);
|
|
675
|
+
if (__DEV__) {
|
|
676
|
+
Object.freeze(array);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return node.array;
|
|
680
|
+
}
|
|
681
|
+
case null:
|
|
682
|
+
case Object.prototype: {
|
|
683
|
+
if (this.known.has(value))
|
|
684
|
+
return value;
|
|
685
|
+
var proto_1 = Object.getPrototypeOf(value);
|
|
686
|
+
var array_1 = [proto_1];
|
|
687
|
+
var keys = this.sortedKeys(value);
|
|
688
|
+
array_1.push(keys.json);
|
|
689
|
+
var firstValueIndex_1 = array_1.length;
|
|
690
|
+
keys.sorted.forEach(function (key) {
|
|
691
|
+
array_1.push(_this.admit(value[key]));
|
|
692
|
+
});
|
|
693
|
+
var node = this.pool.lookupArray(array_1);
|
|
694
|
+
if (!node.object) {
|
|
695
|
+
var obj_1 = node.object = Object.create(proto_1);
|
|
696
|
+
this.known.add(obj_1);
|
|
697
|
+
keys.sorted.forEach(function (key, i) {
|
|
698
|
+
obj_1[key] = array_1[firstValueIndex_1 + i];
|
|
699
|
+
});
|
|
700
|
+
if (__DEV__) {
|
|
701
|
+
Object.freeze(obj_1);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return node.object;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return value;
|
|
709
|
+
};
|
|
710
|
+
ObjectCanon.prototype.sortedKeys = function (obj) {
|
|
711
|
+
var keys = Object.keys(obj);
|
|
712
|
+
var node = this.pool.lookupArray(keys);
|
|
713
|
+
if (!node.keys) {
|
|
714
|
+
keys.sort();
|
|
715
|
+
var json = JSON.stringify(keys);
|
|
716
|
+
if (!(node.keys = this.keysByJSON.get(json))) {
|
|
717
|
+
this.keysByJSON.set(json, node.keys = { sorted: keys, json: json });
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
return node.keys;
|
|
721
|
+
};
|
|
722
|
+
return ObjectCanon;
|
|
723
|
+
}());
|
|
724
|
+
var canonicalStringify = Object.assign(function (value) {
|
|
725
|
+
if (utilities.isNonNullObject(value)) {
|
|
726
|
+
if (stringifyCanon === void 0) {
|
|
727
|
+
resetCanonicalStringify();
|
|
728
|
+
}
|
|
729
|
+
var canonical = stringifyCanon.admit(value);
|
|
730
|
+
var json = stringifyCache.get(canonical);
|
|
731
|
+
if (json === void 0) {
|
|
732
|
+
stringifyCache.set(canonical, json = JSON.stringify(canonical));
|
|
733
|
+
}
|
|
734
|
+
return json;
|
|
735
|
+
}
|
|
736
|
+
return JSON.stringify(value);
|
|
737
|
+
}, {
|
|
738
|
+
reset: resetCanonicalStringify,
|
|
739
|
+
});
|
|
740
|
+
var stringifyCanon;
|
|
741
|
+
var stringifyCache;
|
|
742
|
+
function resetCanonicalStringify() {
|
|
743
|
+
stringifyCanon = new ObjectCanon;
|
|
744
|
+
stringifyCache = new (utilities.canUseWeakMap ? WeakMap : Map)();
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function execSelectionSetKeyArgs(options) {
|
|
748
|
+
return [
|
|
749
|
+
options.selectionSet,
|
|
750
|
+
options.objectOrReference,
|
|
751
|
+
options.context,
|
|
752
|
+
options.context.canonizeResults,
|
|
753
|
+
];
|
|
754
|
+
}
|
|
755
|
+
var StoreReader = (function () {
|
|
756
|
+
function StoreReader(config) {
|
|
757
|
+
var _this = this;
|
|
758
|
+
this.knownResults = new (utilities.canUseWeakMap ? WeakMap : Map)();
|
|
759
|
+
this.config = utilities.compact(config, {
|
|
760
|
+
addTypename: config.addTypename !== false,
|
|
761
|
+
canonizeResults: shouldCanonizeResults(config),
|
|
762
|
+
});
|
|
763
|
+
this.canon = config.canon || new ObjectCanon;
|
|
764
|
+
this.executeSelectionSet = optimism.wrap(function (options) {
|
|
765
|
+
var _a;
|
|
766
|
+
var canonizeResults = options.context.canonizeResults;
|
|
767
|
+
var peekArgs = execSelectionSetKeyArgs(options);
|
|
768
|
+
peekArgs[3] = !canonizeResults;
|
|
769
|
+
var other = (_a = _this.executeSelectionSet).peek.apply(_a, peekArgs);
|
|
770
|
+
if (other) {
|
|
771
|
+
if (canonizeResults) {
|
|
772
|
+
return tslib.__assign(tslib.__assign({}, other), { result: _this.canon.admit(other.result) });
|
|
773
|
+
}
|
|
774
|
+
return other;
|
|
775
|
+
}
|
|
776
|
+
maybeDependOnExistenceOfEntity(options.context.store, options.enclosingRef.__ref);
|
|
777
|
+
return _this.execSelectionSetImpl(options);
|
|
778
|
+
}, {
|
|
779
|
+
max: this.config.resultCacheMaxSize,
|
|
780
|
+
keyArgs: execSelectionSetKeyArgs,
|
|
781
|
+
makeCacheKey: function (selectionSet, parent, context, canonizeResults) {
|
|
782
|
+
if (supportsResultCaching(context.store)) {
|
|
783
|
+
return context.store.makeCacheKey(selectionSet, utilities.isReference(parent) ? parent.__ref : parent, context.varString, canonizeResults);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
});
|
|
787
|
+
this.executeSubSelectedArray = optimism.wrap(function (options) {
|
|
788
|
+
maybeDependOnExistenceOfEntity(options.context.store, options.enclosingRef.__ref);
|
|
789
|
+
return _this.execSubSelectedArrayImpl(options);
|
|
790
|
+
}, {
|
|
791
|
+
max: this.config.resultCacheMaxSize,
|
|
792
|
+
makeCacheKey: function (_a) {
|
|
793
|
+
var field = _a.field, array = _a.array, context = _a.context;
|
|
794
|
+
if (supportsResultCaching(context.store)) {
|
|
795
|
+
return context.store.makeCacheKey(field, array, context.varString);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
StoreReader.prototype.resetCanon = function () {
|
|
801
|
+
this.canon = new ObjectCanon;
|
|
802
|
+
};
|
|
803
|
+
StoreReader.prototype.diffQueryAgainstStore = function (_a) {
|
|
804
|
+
var store = _a.store, query = _a.query, _b = _a.rootId, rootId = _b === void 0 ? 'ROOT_QUERY' : _b, variables = _a.variables, _c = _a.returnPartialData, returnPartialData = _c === void 0 ? true : _c, _d = _a.canonizeResults, canonizeResults = _d === void 0 ? this.config.canonizeResults : _d;
|
|
805
|
+
var policies = this.config.cache.policies;
|
|
806
|
+
variables = tslib.__assign(tslib.__assign({}, utilities.getDefaultValues(utilities.getQueryDefinition(query))), variables);
|
|
807
|
+
var rootRef = utilities.makeReference(rootId);
|
|
808
|
+
var merger = new utilities.DeepMerger;
|
|
809
|
+
var execResult = this.executeSelectionSet({
|
|
810
|
+
selectionSet: utilities.getMainDefinition(query).selectionSet,
|
|
811
|
+
objectOrReference: rootRef,
|
|
812
|
+
enclosingRef: rootRef,
|
|
813
|
+
context: {
|
|
814
|
+
store: store,
|
|
815
|
+
query: query,
|
|
816
|
+
policies: policies,
|
|
817
|
+
variables: variables,
|
|
818
|
+
varString: canonicalStringify(variables),
|
|
819
|
+
canonizeResults: canonizeResults,
|
|
820
|
+
fragmentMap: utilities.createFragmentMap(utilities.getFragmentDefinitions(query)),
|
|
821
|
+
merge: function (a, b) {
|
|
822
|
+
return merger.merge(a, b);
|
|
823
|
+
},
|
|
824
|
+
},
|
|
825
|
+
});
|
|
826
|
+
var missing;
|
|
827
|
+
if (execResult.missing) {
|
|
828
|
+
missing = [new MissingFieldError(firstMissing(execResult.missing), execResult.missing, query, variables)];
|
|
829
|
+
if (!returnPartialData) {
|
|
830
|
+
throw missing[0];
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
return {
|
|
834
|
+
result: execResult.result,
|
|
835
|
+
complete: !missing,
|
|
836
|
+
missing: missing,
|
|
837
|
+
};
|
|
838
|
+
};
|
|
839
|
+
StoreReader.prototype.isFresh = function (result, parent, selectionSet, context) {
|
|
840
|
+
if (supportsResultCaching(context.store) &&
|
|
841
|
+
this.knownResults.get(result) === selectionSet) {
|
|
842
|
+
var latest = this.executeSelectionSet.peek(selectionSet, parent, context, this.canon.isKnown(result));
|
|
843
|
+
if (latest && result === latest.result) {
|
|
844
|
+
return true;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
return false;
|
|
848
|
+
};
|
|
849
|
+
StoreReader.prototype.execSelectionSetImpl = function (_a) {
|
|
850
|
+
var _this = this;
|
|
851
|
+
var selectionSet = _a.selectionSet, objectOrReference = _a.objectOrReference, enclosingRef = _a.enclosingRef, context = _a.context;
|
|
852
|
+
if (utilities.isReference(objectOrReference) &&
|
|
853
|
+
!context.policies.rootTypenamesById[objectOrReference.__ref] &&
|
|
854
|
+
!context.store.has(objectOrReference.__ref)) {
|
|
855
|
+
return {
|
|
856
|
+
result: this.canon.empty,
|
|
857
|
+
missing: "Dangling reference to missing ".concat(objectOrReference.__ref, " object"),
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
var variables = context.variables, policies = context.policies, store = context.store;
|
|
861
|
+
var typename = store.getFieldValue(objectOrReference, "__typename");
|
|
862
|
+
var result = {};
|
|
863
|
+
var missing;
|
|
864
|
+
if (this.config.addTypename &&
|
|
865
|
+
typeof typename === "string" &&
|
|
866
|
+
!policies.rootIdsByTypename[typename]) {
|
|
867
|
+
result = { __typename: typename };
|
|
868
|
+
}
|
|
869
|
+
function handleMissing(result, resultName) {
|
|
870
|
+
var _a;
|
|
871
|
+
if (result.missing) {
|
|
872
|
+
missing = context.merge(missing, (_a = {}, _a[resultName] = result.missing, _a));
|
|
873
|
+
}
|
|
874
|
+
return result.result;
|
|
875
|
+
}
|
|
876
|
+
var workSet = new Set(selectionSet.selections);
|
|
877
|
+
workSet.forEach(function (selection) {
|
|
878
|
+
var _a, _b;
|
|
879
|
+
if (!utilities.shouldInclude(selection, variables))
|
|
880
|
+
return;
|
|
881
|
+
if (utilities.isField(selection)) {
|
|
882
|
+
var fieldValue = policies.readField({
|
|
883
|
+
fieldName: selection.name.value,
|
|
884
|
+
field: selection,
|
|
885
|
+
variables: context.variables,
|
|
886
|
+
from: objectOrReference,
|
|
887
|
+
}, context);
|
|
888
|
+
var resultName = utilities.resultKeyNameFromField(selection);
|
|
889
|
+
if (fieldValue === void 0) {
|
|
890
|
+
if (!utilities.addTypenameToDocument.added(selection)) {
|
|
891
|
+
missing = context.merge(missing, (_a = {},
|
|
892
|
+
_a[resultName] = "Can't find field '".concat(selection.name.value, "' on ").concat(utilities.isReference(objectOrReference)
|
|
893
|
+
? objectOrReference.__ref + " object"
|
|
894
|
+
: "object " + JSON.stringify(objectOrReference, null, 2)),
|
|
895
|
+
_a));
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
else if (isArray(fieldValue)) {
|
|
899
|
+
fieldValue = handleMissing(_this.executeSubSelectedArray({
|
|
900
|
+
field: selection,
|
|
901
|
+
array: fieldValue,
|
|
902
|
+
enclosingRef: enclosingRef,
|
|
903
|
+
context: context,
|
|
904
|
+
}), resultName);
|
|
905
|
+
}
|
|
906
|
+
else if (!selection.selectionSet) {
|
|
907
|
+
if (context.canonizeResults) {
|
|
908
|
+
fieldValue = _this.canon.pass(fieldValue);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
else if (fieldValue != null) {
|
|
912
|
+
fieldValue = handleMissing(_this.executeSelectionSet({
|
|
913
|
+
selectionSet: selection.selectionSet,
|
|
914
|
+
objectOrReference: fieldValue,
|
|
915
|
+
enclosingRef: utilities.isReference(fieldValue) ? fieldValue : enclosingRef,
|
|
916
|
+
context: context,
|
|
917
|
+
}), resultName);
|
|
918
|
+
}
|
|
919
|
+
if (fieldValue !== void 0) {
|
|
920
|
+
result = context.merge(result, (_b = {}, _b[resultName] = fieldValue, _b));
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
else {
|
|
924
|
+
var fragment = utilities.getFragmentFromSelection(selection, context.fragmentMap);
|
|
925
|
+
if (fragment && policies.fragmentMatches(fragment, typename)) {
|
|
926
|
+
fragment.selectionSet.selections.forEach(workSet.add, workSet);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
});
|
|
930
|
+
var finalResult = { result: result, missing: missing };
|
|
931
|
+
var frozen = context.canonizeResults
|
|
932
|
+
? this.canon.admit(finalResult)
|
|
933
|
+
: utilities.maybeDeepFreeze(finalResult);
|
|
934
|
+
if (frozen.result) {
|
|
935
|
+
this.knownResults.set(frozen.result, selectionSet);
|
|
936
|
+
}
|
|
937
|
+
return frozen;
|
|
938
|
+
};
|
|
939
|
+
StoreReader.prototype.execSubSelectedArrayImpl = function (_a) {
|
|
940
|
+
var _this = this;
|
|
941
|
+
var field = _a.field, array = _a.array, enclosingRef = _a.enclosingRef, context = _a.context;
|
|
942
|
+
var missing;
|
|
943
|
+
function handleMissing(childResult, i) {
|
|
944
|
+
var _a;
|
|
945
|
+
if (childResult.missing) {
|
|
946
|
+
missing = context.merge(missing, (_a = {}, _a[i] = childResult.missing, _a));
|
|
947
|
+
}
|
|
948
|
+
return childResult.result;
|
|
949
|
+
}
|
|
950
|
+
if (field.selectionSet) {
|
|
951
|
+
array = array.filter(context.store.canRead);
|
|
952
|
+
}
|
|
953
|
+
array = array.map(function (item, i) {
|
|
954
|
+
if (item === null) {
|
|
955
|
+
return null;
|
|
956
|
+
}
|
|
957
|
+
if (isArray(item)) {
|
|
958
|
+
return handleMissing(_this.executeSubSelectedArray({
|
|
959
|
+
field: field,
|
|
960
|
+
array: item,
|
|
961
|
+
enclosingRef: enclosingRef,
|
|
962
|
+
context: context,
|
|
963
|
+
}), i);
|
|
964
|
+
}
|
|
965
|
+
if (field.selectionSet) {
|
|
966
|
+
return handleMissing(_this.executeSelectionSet({
|
|
967
|
+
selectionSet: field.selectionSet,
|
|
968
|
+
objectOrReference: item,
|
|
969
|
+
enclosingRef: utilities.isReference(item) ? item : enclosingRef,
|
|
970
|
+
context: context,
|
|
971
|
+
}), i);
|
|
972
|
+
}
|
|
973
|
+
if (__DEV__) {
|
|
974
|
+
assertSelectionSetForIdValue(context.store, field, item);
|
|
975
|
+
}
|
|
976
|
+
return item;
|
|
977
|
+
});
|
|
978
|
+
return {
|
|
979
|
+
result: context.canonizeResults ? this.canon.admit(array) : array,
|
|
980
|
+
missing: missing,
|
|
981
|
+
};
|
|
982
|
+
};
|
|
983
|
+
return StoreReader;
|
|
984
|
+
}());
|
|
985
|
+
function firstMissing(tree) {
|
|
986
|
+
try {
|
|
987
|
+
JSON.stringify(tree, function (_, value) {
|
|
988
|
+
if (typeof value === "string")
|
|
989
|
+
throw value;
|
|
990
|
+
return value;
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
catch (result) {
|
|
994
|
+
return result;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
function assertSelectionSetForIdValue(store, field, fieldValue) {
|
|
998
|
+
if (!field.selectionSet) {
|
|
999
|
+
var workSet_1 = new Set([fieldValue]);
|
|
1000
|
+
workSet_1.forEach(function (value) {
|
|
1001
|
+
if (utilities.isNonNullObject(value)) {
|
|
1002
|
+
__DEV__ ? globals.invariant(!utilities.isReference(value), "Missing selection set for object of type ".concat(getTypenameFromStoreObject(store, value), " returned for query field ").concat(field.name.value)) : globals.invariant(!utilities.isReference(value), 5);
|
|
1003
|
+
Object.values(value).forEach(workSet_1.add, workSet_1);
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
var cacheSlot = new context.Slot();
|
|
1010
|
+
var cacheInfoMap = new WeakMap();
|
|
1011
|
+
function getCacheInfo(cache) {
|
|
1012
|
+
var info = cacheInfoMap.get(cache);
|
|
1013
|
+
if (!info) {
|
|
1014
|
+
cacheInfoMap.set(cache, info = {
|
|
1015
|
+
vars: new Set,
|
|
1016
|
+
dep: optimism.dep(),
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
return info;
|
|
1020
|
+
}
|
|
1021
|
+
function forgetCache(cache) {
|
|
1022
|
+
getCacheInfo(cache).vars.forEach(function (rv) { return rv.forgetCache(cache); });
|
|
1023
|
+
}
|
|
1024
|
+
function recallCache(cache) {
|
|
1025
|
+
getCacheInfo(cache).vars.forEach(function (rv) { return rv.attachCache(cache); });
|
|
1026
|
+
}
|
|
1027
|
+
function makeVar(value) {
|
|
1028
|
+
var caches = new Set();
|
|
1029
|
+
var listeners = new Set();
|
|
1030
|
+
var rv = function (newValue) {
|
|
1031
|
+
if (arguments.length > 0) {
|
|
1032
|
+
if (value !== newValue) {
|
|
1033
|
+
value = newValue;
|
|
1034
|
+
caches.forEach(function (cache) {
|
|
1035
|
+
getCacheInfo(cache).dep.dirty(rv);
|
|
1036
|
+
broadcast(cache);
|
|
1037
|
+
});
|
|
1038
|
+
var oldListeners = Array.from(listeners);
|
|
1039
|
+
listeners.clear();
|
|
1040
|
+
oldListeners.forEach(function (listener) { return listener(value); });
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
else {
|
|
1044
|
+
var cache = cacheSlot.getValue();
|
|
1045
|
+
if (cache) {
|
|
1046
|
+
attach(cache);
|
|
1047
|
+
getCacheInfo(cache).dep(rv);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return value;
|
|
1051
|
+
};
|
|
1052
|
+
rv.onNextChange = function (listener) {
|
|
1053
|
+
listeners.add(listener);
|
|
1054
|
+
return function () {
|
|
1055
|
+
listeners.delete(listener);
|
|
1056
|
+
};
|
|
1057
|
+
};
|
|
1058
|
+
var attach = rv.attachCache = function (cache) {
|
|
1059
|
+
caches.add(cache);
|
|
1060
|
+
getCacheInfo(cache).vars.add(rv);
|
|
1061
|
+
return rv;
|
|
1062
|
+
};
|
|
1063
|
+
rv.forgetCache = function (cache) { return caches.delete(cache); };
|
|
1064
|
+
return rv;
|
|
1065
|
+
}
|
|
1066
|
+
function broadcast(cache) {
|
|
1067
|
+
if (cache.broadcastWatches) {
|
|
1068
|
+
cache.broadcastWatches();
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
var specifierInfoCache = Object.create(null);
|
|
1073
|
+
function lookupSpecifierInfo(spec) {
|
|
1074
|
+
var cacheKey = JSON.stringify(spec);
|
|
1075
|
+
return specifierInfoCache[cacheKey] ||
|
|
1076
|
+
(specifierInfoCache[cacheKey] = Object.create(null));
|
|
1077
|
+
}
|
|
1078
|
+
function keyFieldsFnFromSpecifier(specifier) {
|
|
1079
|
+
var info = lookupSpecifierInfo(specifier);
|
|
1080
|
+
return info.keyFieldsFn || (info.keyFieldsFn = function (object, context) {
|
|
1081
|
+
var extract = function (from, key) { return context.readField(key, from); };
|
|
1082
|
+
var keyObject = context.keyObject = collectSpecifierPaths(specifier, function (schemaKeyPath) {
|
|
1083
|
+
var extracted = extractKeyPath(context.storeObject, schemaKeyPath, extract);
|
|
1084
|
+
if (extracted === void 0 &&
|
|
1085
|
+
object !== context.storeObject &&
|
|
1086
|
+
hasOwn.call(object, schemaKeyPath[0])) {
|
|
1087
|
+
extracted = extractKeyPath(object, schemaKeyPath, extractKey);
|
|
1088
|
+
}
|
|
1089
|
+
__DEV__ ? globals.invariant(extracted !== void 0, "Missing field '".concat(schemaKeyPath.join('.'), "' while extracting keyFields from ").concat(JSON.stringify(object))) : globals.invariant(extracted !== void 0, 2);
|
|
1090
|
+
return extracted;
|
|
1091
|
+
});
|
|
1092
|
+
return "".concat(context.typename, ":").concat(JSON.stringify(keyObject));
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
function keyArgsFnFromSpecifier(specifier) {
|
|
1096
|
+
var info = lookupSpecifierInfo(specifier);
|
|
1097
|
+
return info.keyArgsFn || (info.keyArgsFn = function (args, _a) {
|
|
1098
|
+
var field = _a.field, variables = _a.variables, fieldName = _a.fieldName;
|
|
1099
|
+
var collected = collectSpecifierPaths(specifier, function (keyPath) {
|
|
1100
|
+
var firstKey = keyPath[0];
|
|
1101
|
+
var firstChar = firstKey.charAt(0);
|
|
1102
|
+
if (firstChar === "@") {
|
|
1103
|
+
if (field && utilities.isNonEmptyArray(field.directives)) {
|
|
1104
|
+
var directiveName_1 = firstKey.slice(1);
|
|
1105
|
+
var d = field.directives.find(function (d) { return d.name.value === directiveName_1; });
|
|
1106
|
+
var directiveArgs = d && utilities.argumentsObjectFromField(d, variables);
|
|
1107
|
+
return directiveArgs && extractKeyPath(directiveArgs, keyPath.slice(1));
|
|
1108
|
+
}
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
if (firstChar === "$") {
|
|
1112
|
+
var variableName = firstKey.slice(1);
|
|
1113
|
+
if (variables && hasOwn.call(variables, variableName)) {
|
|
1114
|
+
var varKeyPath = keyPath.slice(0);
|
|
1115
|
+
varKeyPath[0] = variableName;
|
|
1116
|
+
return extractKeyPath(variables, varKeyPath);
|
|
1117
|
+
}
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
if (args) {
|
|
1121
|
+
return extractKeyPath(args, keyPath);
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
var suffix = JSON.stringify(collected);
|
|
1125
|
+
if (args || suffix !== "{}") {
|
|
1126
|
+
fieldName += ":" + suffix;
|
|
1127
|
+
}
|
|
1128
|
+
return fieldName;
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
function collectSpecifierPaths(specifier, extractor) {
|
|
1132
|
+
var merger = new utilities.DeepMerger;
|
|
1133
|
+
return getSpecifierPaths(specifier).reduce(function (collected, path) {
|
|
1134
|
+
var _a;
|
|
1135
|
+
var toMerge = extractor(path);
|
|
1136
|
+
if (toMerge !== void 0) {
|
|
1137
|
+
for (var i = path.length - 1; i >= 0; --i) {
|
|
1138
|
+
toMerge = (_a = {}, _a[path[i]] = toMerge, _a);
|
|
1139
|
+
}
|
|
1140
|
+
collected = merger.merge(collected, toMerge);
|
|
1141
|
+
}
|
|
1142
|
+
return collected;
|
|
1143
|
+
}, Object.create(null));
|
|
1144
|
+
}
|
|
1145
|
+
function getSpecifierPaths(spec) {
|
|
1146
|
+
var info = lookupSpecifierInfo(spec);
|
|
1147
|
+
if (!info.paths) {
|
|
1148
|
+
var paths_1 = info.paths = [];
|
|
1149
|
+
var currentPath_1 = [];
|
|
1150
|
+
spec.forEach(function (s, i) {
|
|
1151
|
+
if (isArray(s)) {
|
|
1152
|
+
getSpecifierPaths(s).forEach(function (p) { return paths_1.push(currentPath_1.concat(p)); });
|
|
1153
|
+
currentPath_1.length = 0;
|
|
1154
|
+
}
|
|
1155
|
+
else {
|
|
1156
|
+
currentPath_1.push(s);
|
|
1157
|
+
if (!isArray(spec[i + 1])) {
|
|
1158
|
+
paths_1.push(currentPath_1.slice(0));
|
|
1159
|
+
currentPath_1.length = 0;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
return info.paths;
|
|
1165
|
+
}
|
|
1166
|
+
function extractKey(object, key) {
|
|
1167
|
+
return object[key];
|
|
1168
|
+
}
|
|
1169
|
+
function extractKeyPath(object, path, extract) {
|
|
1170
|
+
extract = extract || extractKey;
|
|
1171
|
+
return normalize(path.reduce(function reducer(obj, key) {
|
|
1172
|
+
return isArray(obj)
|
|
1173
|
+
? obj.map(function (child) { return reducer(child, key); })
|
|
1174
|
+
: obj && extract(obj, key);
|
|
1175
|
+
}, object));
|
|
1176
|
+
}
|
|
1177
|
+
function normalize(value) {
|
|
1178
|
+
if (utilities.isNonNullObject(value)) {
|
|
1179
|
+
if (isArray(value)) {
|
|
1180
|
+
return value.map(normalize);
|
|
1181
|
+
}
|
|
1182
|
+
return collectSpecifierPaths(Object.keys(value).sort(), function (path) { return extractKeyPath(value, path); });
|
|
1183
|
+
}
|
|
1184
|
+
return value;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
utilities.getStoreKeyName.setStringify(canonicalStringify);
|
|
1188
|
+
function argsFromFieldSpecifier(spec) {
|
|
1189
|
+
return spec.args !== void 0 ? spec.args :
|
|
1190
|
+
spec.field ? utilities.argumentsObjectFromField(spec.field, spec.variables) : null;
|
|
1191
|
+
}
|
|
1192
|
+
var nullKeyFieldsFn = function () { return void 0; };
|
|
1193
|
+
var simpleKeyArgsFn = function (_args, context) { return context.fieldName; };
|
|
1194
|
+
var mergeTrueFn = function (existing, incoming, _a) {
|
|
1195
|
+
var mergeObjects = _a.mergeObjects;
|
|
1196
|
+
return mergeObjects(existing, incoming);
|
|
1197
|
+
};
|
|
1198
|
+
var mergeFalseFn = function (_, incoming) { return incoming; };
|
|
1199
|
+
var Policies = (function () {
|
|
1200
|
+
function Policies(config) {
|
|
1201
|
+
this.config = config;
|
|
1202
|
+
this.typePolicies = Object.create(null);
|
|
1203
|
+
this.toBeAdded = Object.create(null);
|
|
1204
|
+
this.supertypeMap = new Map();
|
|
1205
|
+
this.fuzzySubtypes = new Map();
|
|
1206
|
+
this.rootIdsByTypename = Object.create(null);
|
|
1207
|
+
this.rootTypenamesById = Object.create(null);
|
|
1208
|
+
this.usingPossibleTypes = false;
|
|
1209
|
+
this.config = tslib.__assign({ dataIdFromObject: defaultDataIdFromObject }, config);
|
|
1210
|
+
this.cache = this.config.cache;
|
|
1211
|
+
this.setRootTypename("Query");
|
|
1212
|
+
this.setRootTypename("Mutation");
|
|
1213
|
+
this.setRootTypename("Subscription");
|
|
1214
|
+
if (config.possibleTypes) {
|
|
1215
|
+
this.addPossibleTypes(config.possibleTypes);
|
|
1216
|
+
}
|
|
1217
|
+
if (config.typePolicies) {
|
|
1218
|
+
this.addTypePolicies(config.typePolicies);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
Policies.prototype.identify = function (object, partialContext) {
|
|
1222
|
+
var _a;
|
|
1223
|
+
var policies = this;
|
|
1224
|
+
var typename = partialContext && (partialContext.typename ||
|
|
1225
|
+
((_a = partialContext.storeObject) === null || _a === void 0 ? void 0 : _a.__typename)) || object.__typename;
|
|
1226
|
+
if (typename === this.rootTypenamesById.ROOT_QUERY) {
|
|
1227
|
+
return ["ROOT_QUERY"];
|
|
1228
|
+
}
|
|
1229
|
+
var storeObject = partialContext && partialContext.storeObject || object;
|
|
1230
|
+
var context = tslib.__assign(tslib.__assign({}, partialContext), { typename: typename, storeObject: storeObject, readField: partialContext && partialContext.readField || function () {
|
|
1231
|
+
var options = normalizeReadFieldOptions(arguments, storeObject);
|
|
1232
|
+
return policies.readField(options, {
|
|
1233
|
+
store: policies.cache["data"],
|
|
1234
|
+
variables: options.variables,
|
|
1235
|
+
});
|
|
1236
|
+
} });
|
|
1237
|
+
var id;
|
|
1238
|
+
var policy = typename && this.getTypePolicy(typename);
|
|
1239
|
+
var keyFn = policy && policy.keyFn || this.config.dataIdFromObject;
|
|
1240
|
+
while (keyFn) {
|
|
1241
|
+
var specifierOrId = keyFn(object, context);
|
|
1242
|
+
if (isArray(specifierOrId)) {
|
|
1243
|
+
keyFn = keyFieldsFnFromSpecifier(specifierOrId);
|
|
1244
|
+
}
|
|
1245
|
+
else {
|
|
1246
|
+
id = specifierOrId;
|
|
1247
|
+
break;
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
id = id ? String(id) : void 0;
|
|
1251
|
+
return context.keyObject ? [id, context.keyObject] : [id];
|
|
1252
|
+
};
|
|
1253
|
+
Policies.prototype.addTypePolicies = function (typePolicies) {
|
|
1254
|
+
var _this = this;
|
|
1255
|
+
Object.keys(typePolicies).forEach(function (typename) {
|
|
1256
|
+
var _a = typePolicies[typename], queryType = _a.queryType, mutationType = _a.mutationType, subscriptionType = _a.subscriptionType, incoming = tslib.__rest(_a, ["queryType", "mutationType", "subscriptionType"]);
|
|
1257
|
+
if (queryType)
|
|
1258
|
+
_this.setRootTypename("Query", typename);
|
|
1259
|
+
if (mutationType)
|
|
1260
|
+
_this.setRootTypename("Mutation", typename);
|
|
1261
|
+
if (subscriptionType)
|
|
1262
|
+
_this.setRootTypename("Subscription", typename);
|
|
1263
|
+
if (hasOwn.call(_this.toBeAdded, typename)) {
|
|
1264
|
+
_this.toBeAdded[typename].push(incoming);
|
|
1265
|
+
}
|
|
1266
|
+
else {
|
|
1267
|
+
_this.toBeAdded[typename] = [incoming];
|
|
1268
|
+
}
|
|
1269
|
+
});
|
|
1270
|
+
};
|
|
1271
|
+
Policies.prototype.updateTypePolicy = function (typename, incoming) {
|
|
1272
|
+
var _this = this;
|
|
1273
|
+
var existing = this.getTypePolicy(typename);
|
|
1274
|
+
var keyFields = incoming.keyFields, fields = incoming.fields;
|
|
1275
|
+
function setMerge(existing, merge) {
|
|
1276
|
+
existing.merge =
|
|
1277
|
+
typeof merge === "function" ? merge :
|
|
1278
|
+
merge === true ? mergeTrueFn :
|
|
1279
|
+
merge === false ? mergeFalseFn :
|
|
1280
|
+
existing.merge;
|
|
1281
|
+
}
|
|
1282
|
+
setMerge(existing, incoming.merge);
|
|
1283
|
+
existing.keyFn =
|
|
1284
|
+
keyFields === false ? nullKeyFieldsFn :
|
|
1285
|
+
isArray(keyFields) ? keyFieldsFnFromSpecifier(keyFields) :
|
|
1286
|
+
typeof keyFields === "function" ? keyFields :
|
|
1287
|
+
existing.keyFn;
|
|
1288
|
+
if (fields) {
|
|
1289
|
+
Object.keys(fields).forEach(function (fieldName) {
|
|
1290
|
+
var existing = _this.getFieldPolicy(typename, fieldName, true);
|
|
1291
|
+
var incoming = fields[fieldName];
|
|
1292
|
+
if (typeof incoming === "function") {
|
|
1293
|
+
existing.read = incoming;
|
|
1294
|
+
}
|
|
1295
|
+
else {
|
|
1296
|
+
var keyArgs = incoming.keyArgs, read = incoming.read, merge = incoming.merge;
|
|
1297
|
+
existing.keyFn =
|
|
1298
|
+
keyArgs === false ? simpleKeyArgsFn :
|
|
1299
|
+
isArray(keyArgs) ? keyArgsFnFromSpecifier(keyArgs) :
|
|
1300
|
+
typeof keyArgs === "function" ? keyArgs :
|
|
1301
|
+
existing.keyFn;
|
|
1302
|
+
if (typeof read === "function") {
|
|
1303
|
+
existing.read = read;
|
|
1304
|
+
}
|
|
1305
|
+
setMerge(existing, merge);
|
|
1306
|
+
}
|
|
1307
|
+
if (existing.read && existing.merge) {
|
|
1308
|
+
existing.keyFn = existing.keyFn || simpleKeyArgsFn;
|
|
1309
|
+
}
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
Policies.prototype.setRootTypename = function (which, typename) {
|
|
1314
|
+
if (typename === void 0) { typename = which; }
|
|
1315
|
+
var rootId = "ROOT_" + which.toUpperCase();
|
|
1316
|
+
var old = this.rootTypenamesById[rootId];
|
|
1317
|
+
if (typename !== old) {
|
|
1318
|
+
__DEV__ ? globals.invariant(!old || old === which, "Cannot change root ".concat(which, " __typename more than once")) : globals.invariant(!old || old === which, 3);
|
|
1319
|
+
if (old)
|
|
1320
|
+
delete this.rootIdsByTypename[old];
|
|
1321
|
+
this.rootIdsByTypename[typename] = rootId;
|
|
1322
|
+
this.rootTypenamesById[rootId] = typename;
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
Policies.prototype.addPossibleTypes = function (possibleTypes) {
|
|
1326
|
+
var _this = this;
|
|
1327
|
+
this.usingPossibleTypes = true;
|
|
1328
|
+
Object.keys(possibleTypes).forEach(function (supertype) {
|
|
1329
|
+
_this.getSupertypeSet(supertype, true);
|
|
1330
|
+
possibleTypes[supertype].forEach(function (subtype) {
|
|
1331
|
+
_this.getSupertypeSet(subtype, true).add(supertype);
|
|
1332
|
+
var match = subtype.match(TypeOrFieldNameRegExp);
|
|
1333
|
+
if (!match || match[0] !== subtype) {
|
|
1334
|
+
_this.fuzzySubtypes.set(subtype, new RegExp(subtype));
|
|
1335
|
+
}
|
|
1336
|
+
});
|
|
1337
|
+
});
|
|
1338
|
+
};
|
|
1339
|
+
Policies.prototype.getTypePolicy = function (typename) {
|
|
1340
|
+
var _this = this;
|
|
1341
|
+
if (!hasOwn.call(this.typePolicies, typename)) {
|
|
1342
|
+
var policy_1 = this.typePolicies[typename] = Object.create(null);
|
|
1343
|
+
policy_1.fields = Object.create(null);
|
|
1344
|
+
var supertypes = this.supertypeMap.get(typename);
|
|
1345
|
+
if (supertypes && supertypes.size) {
|
|
1346
|
+
supertypes.forEach(function (supertype) {
|
|
1347
|
+
var _a = _this.getTypePolicy(supertype), fields = _a.fields, rest = tslib.__rest(_a, ["fields"]);
|
|
1348
|
+
Object.assign(policy_1, rest);
|
|
1349
|
+
Object.assign(policy_1.fields, fields);
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
var inbox = this.toBeAdded[typename];
|
|
1354
|
+
if (inbox && inbox.length) {
|
|
1355
|
+
inbox.splice(0).forEach(function (policy) {
|
|
1356
|
+
_this.updateTypePolicy(typename, policy);
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
return this.typePolicies[typename];
|
|
1360
|
+
};
|
|
1361
|
+
Policies.prototype.getFieldPolicy = function (typename, fieldName, createIfMissing) {
|
|
1362
|
+
if (typename) {
|
|
1363
|
+
var fieldPolicies = this.getTypePolicy(typename).fields;
|
|
1364
|
+
return fieldPolicies[fieldName] || (createIfMissing && (fieldPolicies[fieldName] = Object.create(null)));
|
|
1365
|
+
}
|
|
1366
|
+
};
|
|
1367
|
+
Policies.prototype.getSupertypeSet = function (subtype, createIfMissing) {
|
|
1368
|
+
var supertypeSet = this.supertypeMap.get(subtype);
|
|
1369
|
+
if (!supertypeSet && createIfMissing) {
|
|
1370
|
+
this.supertypeMap.set(subtype, supertypeSet = new Set());
|
|
1371
|
+
}
|
|
1372
|
+
return supertypeSet;
|
|
1373
|
+
};
|
|
1374
|
+
Policies.prototype.fragmentMatches = function (fragment, typename, result, variables) {
|
|
1375
|
+
var _this = this;
|
|
1376
|
+
if (!fragment.typeCondition)
|
|
1377
|
+
return true;
|
|
1378
|
+
if (!typename)
|
|
1379
|
+
return false;
|
|
1380
|
+
var supertype = fragment.typeCondition.name.value;
|
|
1381
|
+
if (typename === supertype)
|
|
1382
|
+
return true;
|
|
1383
|
+
if (this.usingPossibleTypes &&
|
|
1384
|
+
this.supertypeMap.has(supertype)) {
|
|
1385
|
+
var typenameSupertypeSet = this.getSupertypeSet(typename, true);
|
|
1386
|
+
var workQueue_1 = [typenameSupertypeSet];
|
|
1387
|
+
var maybeEnqueue_1 = function (subtype) {
|
|
1388
|
+
var supertypeSet = _this.getSupertypeSet(subtype, false);
|
|
1389
|
+
if (supertypeSet &&
|
|
1390
|
+
supertypeSet.size &&
|
|
1391
|
+
workQueue_1.indexOf(supertypeSet) < 0) {
|
|
1392
|
+
workQueue_1.push(supertypeSet);
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
var needToCheckFuzzySubtypes = !!(result && this.fuzzySubtypes.size);
|
|
1396
|
+
var checkingFuzzySubtypes = false;
|
|
1397
|
+
for (var i = 0; i < workQueue_1.length; ++i) {
|
|
1398
|
+
var supertypeSet = workQueue_1[i];
|
|
1399
|
+
if (supertypeSet.has(supertype)) {
|
|
1400
|
+
if (!typenameSupertypeSet.has(supertype)) {
|
|
1401
|
+
if (checkingFuzzySubtypes) {
|
|
1402
|
+
__DEV__ && globals.invariant.warn("Inferring subtype ".concat(typename, " of supertype ").concat(supertype));
|
|
1403
|
+
}
|
|
1404
|
+
typenameSupertypeSet.add(supertype);
|
|
1405
|
+
}
|
|
1406
|
+
return true;
|
|
1407
|
+
}
|
|
1408
|
+
supertypeSet.forEach(maybeEnqueue_1);
|
|
1409
|
+
if (needToCheckFuzzySubtypes &&
|
|
1410
|
+
i === workQueue_1.length - 1 &&
|
|
1411
|
+
selectionSetMatchesResult(fragment.selectionSet, result, variables)) {
|
|
1412
|
+
needToCheckFuzzySubtypes = false;
|
|
1413
|
+
checkingFuzzySubtypes = true;
|
|
1414
|
+
this.fuzzySubtypes.forEach(function (regExp, fuzzyString) {
|
|
1415
|
+
var match = typename.match(regExp);
|
|
1416
|
+
if (match && match[0] === typename) {
|
|
1417
|
+
maybeEnqueue_1(fuzzyString);
|
|
1418
|
+
}
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
return false;
|
|
1424
|
+
};
|
|
1425
|
+
Policies.prototype.hasKeyArgs = function (typename, fieldName) {
|
|
1426
|
+
var policy = this.getFieldPolicy(typename, fieldName, false);
|
|
1427
|
+
return !!(policy && policy.keyFn);
|
|
1428
|
+
};
|
|
1429
|
+
Policies.prototype.getStoreFieldName = function (fieldSpec) {
|
|
1430
|
+
var typename = fieldSpec.typename, fieldName = fieldSpec.fieldName;
|
|
1431
|
+
var policy = this.getFieldPolicy(typename, fieldName, false);
|
|
1432
|
+
var storeFieldName;
|
|
1433
|
+
var keyFn = policy && policy.keyFn;
|
|
1434
|
+
if (keyFn && typename) {
|
|
1435
|
+
var context = {
|
|
1436
|
+
typename: typename,
|
|
1437
|
+
fieldName: fieldName,
|
|
1438
|
+
field: fieldSpec.field || null,
|
|
1439
|
+
variables: fieldSpec.variables,
|
|
1440
|
+
};
|
|
1441
|
+
var args = argsFromFieldSpecifier(fieldSpec);
|
|
1442
|
+
while (keyFn) {
|
|
1443
|
+
var specifierOrString = keyFn(args, context);
|
|
1444
|
+
if (isArray(specifierOrString)) {
|
|
1445
|
+
keyFn = keyArgsFnFromSpecifier(specifierOrString);
|
|
1446
|
+
}
|
|
1447
|
+
else {
|
|
1448
|
+
storeFieldName = specifierOrString || fieldName;
|
|
1449
|
+
break;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
if (storeFieldName === void 0) {
|
|
1454
|
+
storeFieldName = fieldSpec.field
|
|
1455
|
+
? utilities.storeKeyNameFromField(fieldSpec.field, fieldSpec.variables)
|
|
1456
|
+
: utilities.getStoreKeyName(fieldName, argsFromFieldSpecifier(fieldSpec));
|
|
1457
|
+
}
|
|
1458
|
+
if (storeFieldName === false) {
|
|
1459
|
+
return fieldName;
|
|
1460
|
+
}
|
|
1461
|
+
return fieldName === fieldNameFromStoreName(storeFieldName)
|
|
1462
|
+
? storeFieldName
|
|
1463
|
+
: fieldName + ":" + storeFieldName;
|
|
1464
|
+
};
|
|
1465
|
+
Policies.prototype.readField = function (options, context) {
|
|
1466
|
+
var objectOrReference = options.from;
|
|
1467
|
+
if (!objectOrReference)
|
|
1468
|
+
return;
|
|
1469
|
+
var nameOrField = options.field || options.fieldName;
|
|
1470
|
+
if (!nameOrField)
|
|
1471
|
+
return;
|
|
1472
|
+
if (options.typename === void 0) {
|
|
1473
|
+
var typename = context.store.getFieldValue(objectOrReference, "__typename");
|
|
1474
|
+
if (typename)
|
|
1475
|
+
options.typename = typename;
|
|
1476
|
+
}
|
|
1477
|
+
var storeFieldName = this.getStoreFieldName(options);
|
|
1478
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
1479
|
+
var existing = context.store.getFieldValue(objectOrReference, storeFieldName);
|
|
1480
|
+
var policy = this.getFieldPolicy(options.typename, fieldName, false);
|
|
1481
|
+
var read = policy && policy.read;
|
|
1482
|
+
if (read) {
|
|
1483
|
+
var readOptions = makeFieldFunctionOptions(this, objectOrReference, options, context, context.store.getStorage(utilities.isReference(objectOrReference)
|
|
1484
|
+
? objectOrReference.__ref
|
|
1485
|
+
: objectOrReference, storeFieldName));
|
|
1486
|
+
return cacheSlot.withValue(this.cache, read, [existing, readOptions]);
|
|
1487
|
+
}
|
|
1488
|
+
return existing;
|
|
1489
|
+
};
|
|
1490
|
+
Policies.prototype.getReadFunction = function (typename, fieldName) {
|
|
1491
|
+
var policy = this.getFieldPolicy(typename, fieldName, false);
|
|
1492
|
+
return policy && policy.read;
|
|
1493
|
+
};
|
|
1494
|
+
Policies.prototype.getMergeFunction = function (parentTypename, fieldName, childTypename) {
|
|
1495
|
+
var policy = this.getFieldPolicy(parentTypename, fieldName, false);
|
|
1496
|
+
var merge = policy && policy.merge;
|
|
1497
|
+
if (!merge && childTypename) {
|
|
1498
|
+
policy = this.getTypePolicy(childTypename);
|
|
1499
|
+
merge = policy && policy.merge;
|
|
1500
|
+
}
|
|
1501
|
+
return merge;
|
|
1502
|
+
};
|
|
1503
|
+
Policies.prototype.runMergeFunction = function (existing, incoming, _a, context, storage) {
|
|
1504
|
+
var field = _a.field, typename = _a.typename, merge = _a.merge;
|
|
1505
|
+
if (merge === mergeTrueFn) {
|
|
1506
|
+
return makeMergeObjectsFunction(context.store)(existing, incoming);
|
|
1507
|
+
}
|
|
1508
|
+
if (merge === mergeFalseFn) {
|
|
1509
|
+
return incoming;
|
|
1510
|
+
}
|
|
1511
|
+
if (context.overwrite) {
|
|
1512
|
+
existing = void 0;
|
|
1513
|
+
}
|
|
1514
|
+
return merge(existing, incoming, makeFieldFunctionOptions(this, void 0, { typename: typename, fieldName: field.name.value, field: field, variables: context.variables }, context, storage || Object.create(null)));
|
|
1515
|
+
};
|
|
1516
|
+
return Policies;
|
|
1517
|
+
}());
|
|
1518
|
+
function makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, context, storage) {
|
|
1519
|
+
var storeFieldName = policies.getStoreFieldName(fieldSpec);
|
|
1520
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
1521
|
+
var variables = fieldSpec.variables || context.variables;
|
|
1522
|
+
var _a = context.store, toReference = _a.toReference, canRead = _a.canRead;
|
|
1523
|
+
return {
|
|
1524
|
+
args: argsFromFieldSpecifier(fieldSpec),
|
|
1525
|
+
field: fieldSpec.field || null,
|
|
1526
|
+
fieldName: fieldName,
|
|
1527
|
+
storeFieldName: storeFieldName,
|
|
1528
|
+
variables: variables,
|
|
1529
|
+
isReference: utilities.isReference,
|
|
1530
|
+
toReference: toReference,
|
|
1531
|
+
storage: storage,
|
|
1532
|
+
cache: policies.cache,
|
|
1533
|
+
canRead: canRead,
|
|
1534
|
+
readField: function () {
|
|
1535
|
+
return policies.readField(normalizeReadFieldOptions(arguments, objectOrReference, context), context);
|
|
1536
|
+
},
|
|
1537
|
+
mergeObjects: makeMergeObjectsFunction(context.store),
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
function normalizeReadFieldOptions(readFieldArgs, objectOrReference, variables) {
|
|
1541
|
+
var fieldNameOrOptions = readFieldArgs[0], from = readFieldArgs[1], argc = readFieldArgs.length;
|
|
1542
|
+
var options;
|
|
1543
|
+
if (typeof fieldNameOrOptions === "string") {
|
|
1544
|
+
options = {
|
|
1545
|
+
fieldName: fieldNameOrOptions,
|
|
1546
|
+
from: argc > 1 ? from : objectOrReference,
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
else {
|
|
1550
|
+
options = tslib.__assign({}, fieldNameOrOptions);
|
|
1551
|
+
if (!hasOwn.call(options, "from")) {
|
|
1552
|
+
options.from = objectOrReference;
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
if (__DEV__ && options.from === void 0) {
|
|
1556
|
+
__DEV__ && globals.invariant.warn("Undefined 'from' passed to readField with arguments ".concat(utilities.stringifyForDisplay(Array.from(readFieldArgs))));
|
|
1557
|
+
}
|
|
1558
|
+
if (void 0 === options.variables) {
|
|
1559
|
+
options.variables = variables;
|
|
1560
|
+
}
|
|
1561
|
+
return options;
|
|
1562
|
+
}
|
|
1563
|
+
function makeMergeObjectsFunction(store) {
|
|
1564
|
+
return function mergeObjects(existing, incoming) {
|
|
1565
|
+
if (isArray(existing) || isArray(incoming)) {
|
|
1566
|
+
throw __DEV__ ? new globals.InvariantError("Cannot automatically merge arrays") : new globals.InvariantError(4);
|
|
1567
|
+
}
|
|
1568
|
+
if (utilities.isNonNullObject(existing) &&
|
|
1569
|
+
utilities.isNonNullObject(incoming)) {
|
|
1570
|
+
var eType = store.getFieldValue(existing, "__typename");
|
|
1571
|
+
var iType = store.getFieldValue(incoming, "__typename");
|
|
1572
|
+
var typesDiffer = eType && iType && eType !== iType;
|
|
1573
|
+
if (typesDiffer) {
|
|
1574
|
+
return incoming;
|
|
1575
|
+
}
|
|
1576
|
+
if (utilities.isReference(existing) &&
|
|
1577
|
+
storeValueIsStoreObject(incoming)) {
|
|
1578
|
+
store.merge(existing.__ref, incoming);
|
|
1579
|
+
return existing;
|
|
1580
|
+
}
|
|
1581
|
+
if (storeValueIsStoreObject(existing) &&
|
|
1582
|
+
utilities.isReference(incoming)) {
|
|
1583
|
+
store.merge(existing, incoming.__ref);
|
|
1584
|
+
return incoming;
|
|
1585
|
+
}
|
|
1586
|
+
if (storeValueIsStoreObject(existing) &&
|
|
1587
|
+
storeValueIsStoreObject(incoming)) {
|
|
1588
|
+
return tslib.__assign(tslib.__assign({}, existing), incoming);
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
return incoming;
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
function getContextFlavor(context, clientOnly, deferred) {
|
|
1596
|
+
var key = "".concat(clientOnly).concat(deferred);
|
|
1597
|
+
var flavored = context.flavors.get(key);
|
|
1598
|
+
if (!flavored) {
|
|
1599
|
+
context.flavors.set(key, flavored = (context.clientOnly === clientOnly &&
|
|
1600
|
+
context.deferred === deferred) ? context : tslib.__assign(tslib.__assign({}, context), { clientOnly: clientOnly, deferred: deferred }));
|
|
1601
|
+
}
|
|
1602
|
+
return flavored;
|
|
1603
|
+
}
|
|
1604
|
+
var StoreWriter = (function () {
|
|
1605
|
+
function StoreWriter(cache, reader) {
|
|
1606
|
+
this.cache = cache;
|
|
1607
|
+
this.reader = reader;
|
|
1608
|
+
}
|
|
1609
|
+
StoreWriter.prototype.writeToStore = function (store, _a) {
|
|
1610
|
+
var _this = this;
|
|
1611
|
+
var query = _a.query, result = _a.result, dataId = _a.dataId, variables = _a.variables, overwrite = _a.overwrite;
|
|
1612
|
+
var operationDefinition = utilities.getOperationDefinition(query);
|
|
1613
|
+
var merger = makeProcessedFieldsMerger();
|
|
1614
|
+
variables = tslib.__assign(tslib.__assign({}, utilities.getDefaultValues(operationDefinition)), variables);
|
|
1615
|
+
var context = {
|
|
1616
|
+
store: store,
|
|
1617
|
+
written: Object.create(null),
|
|
1618
|
+
merge: function (existing, incoming) {
|
|
1619
|
+
return merger.merge(existing, incoming);
|
|
1620
|
+
},
|
|
1621
|
+
variables: variables,
|
|
1622
|
+
varString: canonicalStringify(variables),
|
|
1623
|
+
fragmentMap: utilities.createFragmentMap(utilities.getFragmentDefinitions(query)),
|
|
1624
|
+
overwrite: !!overwrite,
|
|
1625
|
+
incomingById: new Map,
|
|
1626
|
+
clientOnly: false,
|
|
1627
|
+
deferred: false,
|
|
1628
|
+
flavors: new Map,
|
|
1629
|
+
};
|
|
1630
|
+
var ref = this.processSelectionSet({
|
|
1631
|
+
result: result || Object.create(null),
|
|
1632
|
+
dataId: dataId,
|
|
1633
|
+
selectionSet: operationDefinition.selectionSet,
|
|
1634
|
+
mergeTree: { map: new Map },
|
|
1635
|
+
context: context,
|
|
1636
|
+
});
|
|
1637
|
+
if (!utilities.isReference(ref)) {
|
|
1638
|
+
throw __DEV__ ? new globals.InvariantError("Could not identify object ".concat(JSON.stringify(result))) : new globals.InvariantError(6);
|
|
1639
|
+
}
|
|
1640
|
+
context.incomingById.forEach(function (_a, dataId) {
|
|
1641
|
+
var storeObject = _a.storeObject, mergeTree = _a.mergeTree, fieldNodeSet = _a.fieldNodeSet;
|
|
1642
|
+
var entityRef = utilities.makeReference(dataId);
|
|
1643
|
+
if (mergeTree && mergeTree.map.size) {
|
|
1644
|
+
var applied = _this.applyMerges(mergeTree, entityRef, storeObject, context);
|
|
1645
|
+
if (utilities.isReference(applied)) {
|
|
1646
|
+
return;
|
|
1647
|
+
}
|
|
1648
|
+
storeObject = applied;
|
|
1649
|
+
}
|
|
1650
|
+
if (__DEV__ && !context.overwrite) {
|
|
1651
|
+
var fieldsWithSelectionSets_1 = Object.create(null);
|
|
1652
|
+
fieldNodeSet.forEach(function (field) {
|
|
1653
|
+
if (field.selectionSet) {
|
|
1654
|
+
fieldsWithSelectionSets_1[field.name.value] = true;
|
|
1655
|
+
}
|
|
1656
|
+
});
|
|
1657
|
+
var hasSelectionSet_1 = function (storeFieldName) {
|
|
1658
|
+
return fieldsWithSelectionSets_1[fieldNameFromStoreName(storeFieldName)] === true;
|
|
1659
|
+
};
|
|
1660
|
+
var hasMergeFunction_1 = function (storeFieldName) {
|
|
1661
|
+
var childTree = mergeTree && mergeTree.map.get(storeFieldName);
|
|
1662
|
+
return Boolean(childTree && childTree.info && childTree.info.merge);
|
|
1663
|
+
};
|
|
1664
|
+
Object.keys(storeObject).forEach(function (storeFieldName) {
|
|
1665
|
+
if (hasSelectionSet_1(storeFieldName) &&
|
|
1666
|
+
!hasMergeFunction_1(storeFieldName)) {
|
|
1667
|
+
warnAboutDataLoss(entityRef, storeObject, storeFieldName, context.store);
|
|
1668
|
+
}
|
|
1669
|
+
});
|
|
1670
|
+
}
|
|
1671
|
+
store.merge(dataId, storeObject);
|
|
1672
|
+
});
|
|
1673
|
+
store.retain(ref.__ref);
|
|
1674
|
+
return ref;
|
|
1675
|
+
};
|
|
1676
|
+
StoreWriter.prototype.processSelectionSet = function (_a) {
|
|
1677
|
+
var _this = this;
|
|
1678
|
+
var dataId = _a.dataId, result = _a.result, selectionSet = _a.selectionSet, context = _a.context, mergeTree = _a.mergeTree;
|
|
1679
|
+
var policies = this.cache.policies;
|
|
1680
|
+
var incoming = Object.create(null);
|
|
1681
|
+
var typename = (dataId && policies.rootTypenamesById[dataId]) ||
|
|
1682
|
+
utilities.getTypenameFromResult(result, selectionSet, context.fragmentMap) ||
|
|
1683
|
+
(dataId && context.store.get(dataId, "__typename"));
|
|
1684
|
+
if ("string" === typeof typename) {
|
|
1685
|
+
incoming.__typename = typename;
|
|
1686
|
+
}
|
|
1687
|
+
var readField = function () {
|
|
1688
|
+
var options = normalizeReadFieldOptions(arguments, incoming, context.variables);
|
|
1689
|
+
if (utilities.isReference(options.from)) {
|
|
1690
|
+
var info = context.incomingById.get(options.from.__ref);
|
|
1691
|
+
if (info) {
|
|
1692
|
+
var result_1 = policies.readField(tslib.__assign(tslib.__assign({}, options), { from: info.storeObject }), context);
|
|
1693
|
+
if (result_1 !== void 0) {
|
|
1694
|
+
return result_1;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
return policies.readField(options, context);
|
|
1699
|
+
};
|
|
1700
|
+
var fieldNodeSet = new Set();
|
|
1701
|
+
this.flattenFields(selectionSet, result, context, typename).forEach(function (context, field) {
|
|
1702
|
+
var _a;
|
|
1703
|
+
var resultFieldKey = utilities.resultKeyNameFromField(field);
|
|
1704
|
+
var value = result[resultFieldKey];
|
|
1705
|
+
fieldNodeSet.add(field);
|
|
1706
|
+
if (value !== void 0) {
|
|
1707
|
+
var storeFieldName = policies.getStoreFieldName({
|
|
1708
|
+
typename: typename,
|
|
1709
|
+
fieldName: field.name.value,
|
|
1710
|
+
field: field,
|
|
1711
|
+
variables: context.variables,
|
|
1712
|
+
});
|
|
1713
|
+
var childTree = getChildMergeTree(mergeTree, storeFieldName);
|
|
1714
|
+
var incomingValue = _this.processFieldValue(value, field, field.selectionSet
|
|
1715
|
+
? getContextFlavor(context, false, false)
|
|
1716
|
+
: context, childTree);
|
|
1717
|
+
var childTypename = void 0;
|
|
1718
|
+
if (field.selectionSet &&
|
|
1719
|
+
(utilities.isReference(incomingValue) ||
|
|
1720
|
+
storeValueIsStoreObject(incomingValue))) {
|
|
1721
|
+
childTypename = readField("__typename", incomingValue);
|
|
1722
|
+
}
|
|
1723
|
+
var merge = policies.getMergeFunction(typename, field.name.value, childTypename);
|
|
1724
|
+
if (merge) {
|
|
1725
|
+
childTree.info = {
|
|
1726
|
+
field: field,
|
|
1727
|
+
typename: typename,
|
|
1728
|
+
merge: merge,
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
else {
|
|
1732
|
+
maybeRecycleChildMergeTree(mergeTree, storeFieldName);
|
|
1733
|
+
}
|
|
1734
|
+
incoming = context.merge(incoming, (_a = {},
|
|
1735
|
+
_a[storeFieldName] = incomingValue,
|
|
1736
|
+
_a));
|
|
1737
|
+
}
|
|
1738
|
+
else if (__DEV__ &&
|
|
1739
|
+
!context.clientOnly &&
|
|
1740
|
+
!context.deferred &&
|
|
1741
|
+
!utilities.addTypenameToDocument.added(field) &&
|
|
1742
|
+
!policies.getReadFunction(typename, field.name.value)) {
|
|
1743
|
+
__DEV__ && globals.invariant.error("Missing field '".concat(utilities.resultKeyNameFromField(field), "' while writing result ").concat(JSON.stringify(result, null, 2)).substring(0, 1000));
|
|
1744
|
+
}
|
|
1745
|
+
});
|
|
1746
|
+
try {
|
|
1747
|
+
var _b = policies.identify(result, {
|
|
1748
|
+
typename: typename,
|
|
1749
|
+
selectionSet: selectionSet,
|
|
1750
|
+
fragmentMap: context.fragmentMap,
|
|
1751
|
+
storeObject: incoming,
|
|
1752
|
+
readField: readField,
|
|
1753
|
+
}), id = _b[0], keyObject = _b[1];
|
|
1754
|
+
dataId = dataId || id;
|
|
1755
|
+
if (keyObject) {
|
|
1756
|
+
incoming = context.merge(incoming, keyObject);
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
catch (e) {
|
|
1760
|
+
if (!dataId)
|
|
1761
|
+
throw e;
|
|
1762
|
+
}
|
|
1763
|
+
if ("string" === typeof dataId) {
|
|
1764
|
+
var dataRef = utilities.makeReference(dataId);
|
|
1765
|
+
var sets = context.written[dataId] || (context.written[dataId] = []);
|
|
1766
|
+
if (sets.indexOf(selectionSet) >= 0)
|
|
1767
|
+
return dataRef;
|
|
1768
|
+
sets.push(selectionSet);
|
|
1769
|
+
if (this.reader && this.reader.isFresh(result, dataRef, selectionSet, context)) {
|
|
1770
|
+
return dataRef;
|
|
1771
|
+
}
|
|
1772
|
+
var previous_1 = context.incomingById.get(dataId);
|
|
1773
|
+
if (previous_1) {
|
|
1774
|
+
previous_1.storeObject = context.merge(previous_1.storeObject, incoming);
|
|
1775
|
+
previous_1.mergeTree = mergeMergeTrees(previous_1.mergeTree, mergeTree);
|
|
1776
|
+
fieldNodeSet.forEach(function (field) { return previous_1.fieldNodeSet.add(field); });
|
|
1777
|
+
}
|
|
1778
|
+
else {
|
|
1779
|
+
context.incomingById.set(dataId, {
|
|
1780
|
+
storeObject: incoming,
|
|
1781
|
+
mergeTree: mergeTreeIsEmpty(mergeTree) ? void 0 : mergeTree,
|
|
1782
|
+
fieldNodeSet: fieldNodeSet,
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
return dataRef;
|
|
1786
|
+
}
|
|
1787
|
+
return incoming;
|
|
1788
|
+
};
|
|
1789
|
+
StoreWriter.prototype.processFieldValue = function (value, field, context, mergeTree) {
|
|
1790
|
+
var _this = this;
|
|
1791
|
+
if (!field.selectionSet || value === null) {
|
|
1792
|
+
return __DEV__ ? utilities.cloneDeep(value) : value;
|
|
1793
|
+
}
|
|
1794
|
+
if (isArray(value)) {
|
|
1795
|
+
return value.map(function (item, i) {
|
|
1796
|
+
var value = _this.processFieldValue(item, field, context, getChildMergeTree(mergeTree, i));
|
|
1797
|
+
maybeRecycleChildMergeTree(mergeTree, i);
|
|
1798
|
+
return value;
|
|
1799
|
+
});
|
|
1800
|
+
}
|
|
1801
|
+
return this.processSelectionSet({
|
|
1802
|
+
result: value,
|
|
1803
|
+
selectionSet: field.selectionSet,
|
|
1804
|
+
context: context,
|
|
1805
|
+
mergeTree: mergeTree,
|
|
1806
|
+
});
|
|
1807
|
+
};
|
|
1808
|
+
StoreWriter.prototype.flattenFields = function (selectionSet, result, context, typename) {
|
|
1809
|
+
if (typename === void 0) { typename = utilities.getTypenameFromResult(result, selectionSet, context.fragmentMap); }
|
|
1810
|
+
var fieldMap = new Map();
|
|
1811
|
+
var policies = this.cache.policies;
|
|
1812
|
+
var limitingTrie = new trie.Trie(false);
|
|
1813
|
+
(function flatten(selectionSet, inheritedContext) {
|
|
1814
|
+
var visitedNode = limitingTrie.lookup(selectionSet, inheritedContext.clientOnly, inheritedContext.deferred);
|
|
1815
|
+
if (visitedNode.visited)
|
|
1816
|
+
return;
|
|
1817
|
+
visitedNode.visited = true;
|
|
1818
|
+
selectionSet.selections.forEach(function (selection) {
|
|
1819
|
+
if (!utilities.shouldInclude(selection, context.variables))
|
|
1820
|
+
return;
|
|
1821
|
+
var clientOnly = inheritedContext.clientOnly, deferred = inheritedContext.deferred;
|
|
1822
|
+
if (!(clientOnly && deferred) &&
|
|
1823
|
+
utilities.isNonEmptyArray(selection.directives)) {
|
|
1824
|
+
selection.directives.forEach(function (dir) {
|
|
1825
|
+
var name = dir.name.value;
|
|
1826
|
+
if (name === "client")
|
|
1827
|
+
clientOnly = true;
|
|
1828
|
+
if (name === "defer") {
|
|
1829
|
+
var args = utilities.argumentsObjectFromField(dir, context.variables);
|
|
1830
|
+
if (!args || args.if !== false) {
|
|
1831
|
+
deferred = true;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
if (utilities.isField(selection)) {
|
|
1837
|
+
var existing = fieldMap.get(selection);
|
|
1838
|
+
if (existing) {
|
|
1839
|
+
clientOnly = clientOnly && existing.clientOnly;
|
|
1840
|
+
deferred = deferred && existing.deferred;
|
|
1841
|
+
}
|
|
1842
|
+
fieldMap.set(selection, getContextFlavor(context, clientOnly, deferred));
|
|
1843
|
+
}
|
|
1844
|
+
else {
|
|
1845
|
+
var fragment = utilities.getFragmentFromSelection(selection, context.fragmentMap);
|
|
1846
|
+
if (fragment &&
|
|
1847
|
+
policies.fragmentMatches(fragment, typename, result, context.variables)) {
|
|
1848
|
+
flatten(fragment.selectionSet, getContextFlavor(context, clientOnly, deferred));
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
});
|
|
1852
|
+
})(selectionSet, context);
|
|
1853
|
+
return fieldMap;
|
|
1854
|
+
};
|
|
1855
|
+
StoreWriter.prototype.applyMerges = function (mergeTree, existing, incoming, context, getStorageArgs) {
|
|
1856
|
+
var _a;
|
|
1857
|
+
var _this = this;
|
|
1858
|
+
if (mergeTree.map.size && !utilities.isReference(incoming)) {
|
|
1859
|
+
var e_1 = (!isArray(incoming) &&
|
|
1860
|
+
(utilities.isReference(existing) || storeValueIsStoreObject(existing))) ? existing : void 0;
|
|
1861
|
+
var i_1 = incoming;
|
|
1862
|
+
if (e_1 && !getStorageArgs) {
|
|
1863
|
+
getStorageArgs = [utilities.isReference(e_1) ? e_1.__ref : e_1];
|
|
1864
|
+
}
|
|
1865
|
+
var changedFields_1;
|
|
1866
|
+
var getValue_1 = function (from, name) {
|
|
1867
|
+
return isArray(from)
|
|
1868
|
+
? (typeof name === "number" ? from[name] : void 0)
|
|
1869
|
+
: context.store.getFieldValue(from, String(name));
|
|
1870
|
+
};
|
|
1871
|
+
mergeTree.map.forEach(function (childTree, storeFieldName) {
|
|
1872
|
+
var eVal = getValue_1(e_1, storeFieldName);
|
|
1873
|
+
var iVal = getValue_1(i_1, storeFieldName);
|
|
1874
|
+
if (void 0 === iVal)
|
|
1875
|
+
return;
|
|
1876
|
+
if (getStorageArgs) {
|
|
1877
|
+
getStorageArgs.push(storeFieldName);
|
|
1878
|
+
}
|
|
1879
|
+
var aVal = _this.applyMerges(childTree, eVal, iVal, context, getStorageArgs);
|
|
1880
|
+
if (aVal !== iVal) {
|
|
1881
|
+
changedFields_1 = changedFields_1 || new Map;
|
|
1882
|
+
changedFields_1.set(storeFieldName, aVal);
|
|
1883
|
+
}
|
|
1884
|
+
if (getStorageArgs) {
|
|
1885
|
+
globals.invariant(getStorageArgs.pop() === storeFieldName);
|
|
1886
|
+
}
|
|
1887
|
+
});
|
|
1888
|
+
if (changedFields_1) {
|
|
1889
|
+
incoming = (isArray(i_1) ? i_1.slice(0) : tslib.__assign({}, i_1));
|
|
1890
|
+
changedFields_1.forEach(function (value, name) {
|
|
1891
|
+
incoming[name] = value;
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
if (mergeTree.info) {
|
|
1896
|
+
return this.cache.policies.runMergeFunction(existing, incoming, mergeTree.info, context, getStorageArgs && (_a = context.store).getStorage.apply(_a, getStorageArgs));
|
|
1897
|
+
}
|
|
1898
|
+
return incoming;
|
|
1899
|
+
};
|
|
1900
|
+
return StoreWriter;
|
|
1901
|
+
}());
|
|
1902
|
+
var emptyMergeTreePool = [];
|
|
1903
|
+
function getChildMergeTree(_a, name) {
|
|
1904
|
+
var map = _a.map;
|
|
1905
|
+
if (!map.has(name)) {
|
|
1906
|
+
map.set(name, emptyMergeTreePool.pop() || { map: new Map });
|
|
1907
|
+
}
|
|
1908
|
+
return map.get(name);
|
|
1909
|
+
}
|
|
1910
|
+
function mergeMergeTrees(left, right) {
|
|
1911
|
+
if (left === right || !right || mergeTreeIsEmpty(right))
|
|
1912
|
+
return left;
|
|
1913
|
+
if (!left || mergeTreeIsEmpty(left))
|
|
1914
|
+
return right;
|
|
1915
|
+
var info = left.info && right.info ? tslib.__assign(tslib.__assign({}, left.info), right.info) : left.info || right.info;
|
|
1916
|
+
var needToMergeMaps = left.map.size && right.map.size;
|
|
1917
|
+
var map = needToMergeMaps ? new Map :
|
|
1918
|
+
left.map.size ? left.map : right.map;
|
|
1919
|
+
var merged = { info: info, map: map };
|
|
1920
|
+
if (needToMergeMaps) {
|
|
1921
|
+
var remainingRightKeys_1 = new Set(right.map.keys());
|
|
1922
|
+
left.map.forEach(function (leftTree, key) {
|
|
1923
|
+
merged.map.set(key, mergeMergeTrees(leftTree, right.map.get(key)));
|
|
1924
|
+
remainingRightKeys_1.delete(key);
|
|
1925
|
+
});
|
|
1926
|
+
remainingRightKeys_1.forEach(function (key) {
|
|
1927
|
+
merged.map.set(key, mergeMergeTrees(right.map.get(key), left.map.get(key)));
|
|
1928
|
+
});
|
|
1929
|
+
}
|
|
1930
|
+
return merged;
|
|
1931
|
+
}
|
|
1932
|
+
function mergeTreeIsEmpty(tree) {
|
|
1933
|
+
return !tree || !(tree.info || tree.map.size);
|
|
1934
|
+
}
|
|
1935
|
+
function maybeRecycleChildMergeTree(_a, name) {
|
|
1936
|
+
var map = _a.map;
|
|
1937
|
+
var childTree = map.get(name);
|
|
1938
|
+
if (childTree && mergeTreeIsEmpty(childTree)) {
|
|
1939
|
+
emptyMergeTreePool.push(childTree);
|
|
1940
|
+
map.delete(name);
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
var warnings = new Set();
|
|
1944
|
+
function warnAboutDataLoss(existingRef, incomingObj, storeFieldName, store) {
|
|
1945
|
+
var getChild = function (objOrRef) {
|
|
1946
|
+
var child = store.getFieldValue(objOrRef, storeFieldName);
|
|
1947
|
+
return typeof child === "object" && child;
|
|
1948
|
+
};
|
|
1949
|
+
var existing = getChild(existingRef);
|
|
1950
|
+
if (!existing)
|
|
1951
|
+
return;
|
|
1952
|
+
var incoming = getChild(incomingObj);
|
|
1953
|
+
if (!incoming)
|
|
1954
|
+
return;
|
|
1955
|
+
if (utilities.isReference(existing))
|
|
1956
|
+
return;
|
|
1957
|
+
if (equality.equal(existing, incoming))
|
|
1958
|
+
return;
|
|
1959
|
+
if (Object.keys(existing).every(function (key) { return store.getFieldValue(incoming, key) !== void 0; })) {
|
|
1960
|
+
return;
|
|
1961
|
+
}
|
|
1962
|
+
var parentType = store.getFieldValue(existingRef, "__typename") ||
|
|
1963
|
+
store.getFieldValue(incomingObj, "__typename");
|
|
1964
|
+
var fieldName = fieldNameFromStoreName(storeFieldName);
|
|
1965
|
+
var typeDotName = "".concat(parentType, ".").concat(fieldName);
|
|
1966
|
+
if (warnings.has(typeDotName))
|
|
1967
|
+
return;
|
|
1968
|
+
warnings.add(typeDotName);
|
|
1969
|
+
var childTypenames = [];
|
|
1970
|
+
if (!isArray(existing) &&
|
|
1971
|
+
!isArray(incoming)) {
|
|
1972
|
+
[existing, incoming].forEach(function (child) {
|
|
1973
|
+
var typename = store.getFieldValue(child, "__typename");
|
|
1974
|
+
if (typeof typename === "string" &&
|
|
1975
|
+
!childTypenames.includes(typename)) {
|
|
1976
|
+
childTypenames.push(typename);
|
|
1977
|
+
}
|
|
1978
|
+
});
|
|
1979
|
+
}
|
|
1980
|
+
__DEV__ && globals.invariant.warn("Cache data may be lost when replacing the ".concat(fieldName, " field of a ").concat(parentType, " object.\n\nTo address this problem (which is not a bug in Apollo Client), ").concat(childTypenames.length
|
|
1981
|
+
? "either ensure all objects of type " +
|
|
1982
|
+
childTypenames.join(" and ") + " have an ID or a custom merge function, or "
|
|
1983
|
+
: "", "define a custom merge function for the ").concat(typeDotName, " field, so InMemoryCache can safely merge these objects:\n\n existing: ").concat(JSON.stringify(existing).slice(0, 1000), "\n incoming: ").concat(JSON.stringify(incoming).slice(0, 1000), "\n\nFor more information about these options, please refer to the documentation:\n\n * Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers\n * Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects\n"));
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
var InMemoryCache = (function (_super) {
|
|
1987
|
+
tslib.__extends(InMemoryCache, _super);
|
|
1988
|
+
function InMemoryCache(config) {
|
|
1989
|
+
if (config === void 0) { config = {}; }
|
|
1990
|
+
var _this = _super.call(this) || this;
|
|
1991
|
+
_this.watches = new Set();
|
|
1992
|
+
_this.typenameDocumentCache = new Map();
|
|
1993
|
+
_this.makeVar = makeVar;
|
|
1994
|
+
_this.txCount = 0;
|
|
1995
|
+
_this.config = normalizeConfig(config);
|
|
1996
|
+
_this.addTypename = !!_this.config.addTypename;
|
|
1997
|
+
_this.policies = new Policies({
|
|
1998
|
+
cache: _this,
|
|
1999
|
+
dataIdFromObject: _this.config.dataIdFromObject,
|
|
2000
|
+
possibleTypes: _this.config.possibleTypes,
|
|
2001
|
+
typePolicies: _this.config.typePolicies,
|
|
2002
|
+
});
|
|
2003
|
+
_this.init();
|
|
2004
|
+
return _this;
|
|
2005
|
+
}
|
|
2006
|
+
InMemoryCache.prototype.init = function () {
|
|
2007
|
+
var rootStore = this.data = new exports.EntityStore.Root({
|
|
2008
|
+
policies: this.policies,
|
|
2009
|
+
resultCaching: this.config.resultCaching,
|
|
2010
|
+
});
|
|
2011
|
+
this.optimisticData = rootStore.stump;
|
|
2012
|
+
this.resetResultCache();
|
|
2013
|
+
};
|
|
2014
|
+
InMemoryCache.prototype.resetResultCache = function (resetResultIdentities) {
|
|
2015
|
+
var _this = this;
|
|
2016
|
+
var previousReader = this.storeReader;
|
|
2017
|
+
this.storeWriter = new StoreWriter(this, this.storeReader = new StoreReader({
|
|
2018
|
+
cache: this,
|
|
2019
|
+
addTypename: this.addTypename,
|
|
2020
|
+
resultCacheMaxSize: this.config.resultCacheMaxSize,
|
|
2021
|
+
canonizeResults: shouldCanonizeResults(this.config),
|
|
2022
|
+
canon: resetResultIdentities
|
|
2023
|
+
? void 0
|
|
2024
|
+
: previousReader && previousReader.canon,
|
|
2025
|
+
}));
|
|
2026
|
+
this.maybeBroadcastWatch = optimism.wrap(function (c, options) {
|
|
2027
|
+
return _this.broadcastWatch(c, options);
|
|
2028
|
+
}, {
|
|
2029
|
+
max: this.config.resultCacheMaxSize,
|
|
2030
|
+
makeCacheKey: function (c) {
|
|
2031
|
+
var store = c.optimistic ? _this.optimisticData : _this.data;
|
|
2032
|
+
if (supportsResultCaching(store)) {
|
|
2033
|
+
var optimistic = c.optimistic, rootId = c.rootId, variables = c.variables;
|
|
2034
|
+
return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, rootId: rootId, variables: variables }));
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
});
|
|
2038
|
+
new Set([
|
|
2039
|
+
this.data.group,
|
|
2040
|
+
this.optimisticData.group,
|
|
2041
|
+
]).forEach(function (group) { return group.resetCaching(); });
|
|
2042
|
+
};
|
|
2043
|
+
InMemoryCache.prototype.restore = function (data) {
|
|
2044
|
+
this.init();
|
|
2045
|
+
if (data)
|
|
2046
|
+
this.data.replace(data);
|
|
2047
|
+
return this;
|
|
2048
|
+
};
|
|
2049
|
+
InMemoryCache.prototype.extract = function (optimistic) {
|
|
2050
|
+
if (optimistic === void 0) { optimistic = false; }
|
|
2051
|
+
return (optimistic ? this.optimisticData : this.data).extract();
|
|
2052
|
+
};
|
|
2053
|
+
InMemoryCache.prototype.read = function (options) {
|
|
2054
|
+
var _a = options.returnPartialData, returnPartialData = _a === void 0 ? false : _a;
|
|
2055
|
+
try {
|
|
2056
|
+
return this.storeReader.diffQueryAgainstStore(tslib.__assign(tslib.__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, config: this.config, returnPartialData: returnPartialData })).result || null;
|
|
2057
|
+
}
|
|
2058
|
+
catch (e) {
|
|
2059
|
+
if (e instanceof MissingFieldError) {
|
|
2060
|
+
return null;
|
|
2061
|
+
}
|
|
2062
|
+
throw e;
|
|
2063
|
+
}
|
|
2064
|
+
};
|
|
2065
|
+
InMemoryCache.prototype.write = function (options) {
|
|
2066
|
+
try {
|
|
2067
|
+
++this.txCount;
|
|
2068
|
+
return this.storeWriter.writeToStore(this.data, options);
|
|
2069
|
+
}
|
|
2070
|
+
finally {
|
|
2071
|
+
if (!--this.txCount && options.broadcast !== false) {
|
|
2072
|
+
this.broadcastWatches();
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
};
|
|
2076
|
+
InMemoryCache.prototype.modify = function (options) {
|
|
2077
|
+
if (hasOwn.call(options, "id") && !options.id) {
|
|
2078
|
+
return false;
|
|
2079
|
+
}
|
|
2080
|
+
var store = options.optimistic
|
|
2081
|
+
? this.optimisticData
|
|
2082
|
+
: this.data;
|
|
2083
|
+
try {
|
|
2084
|
+
++this.txCount;
|
|
2085
|
+
return store.modify(options.id || "ROOT_QUERY", options.fields);
|
|
2086
|
+
}
|
|
2087
|
+
finally {
|
|
2088
|
+
if (!--this.txCount && options.broadcast !== false) {
|
|
2089
|
+
this.broadcastWatches();
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
};
|
|
2093
|
+
InMemoryCache.prototype.diff = function (options) {
|
|
2094
|
+
return this.storeReader.diffQueryAgainstStore(tslib.__assign(tslib.__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, rootId: options.id || "ROOT_QUERY", config: this.config }));
|
|
2095
|
+
};
|
|
2096
|
+
InMemoryCache.prototype.watch = function (watch) {
|
|
2097
|
+
var _this = this;
|
|
2098
|
+
if (!this.watches.size) {
|
|
2099
|
+
recallCache(this);
|
|
2100
|
+
}
|
|
2101
|
+
this.watches.add(watch);
|
|
2102
|
+
if (watch.immediate) {
|
|
2103
|
+
this.maybeBroadcastWatch(watch);
|
|
2104
|
+
}
|
|
2105
|
+
return function () {
|
|
2106
|
+
if (_this.watches.delete(watch) && !_this.watches.size) {
|
|
2107
|
+
forgetCache(_this);
|
|
2108
|
+
}
|
|
2109
|
+
_this.maybeBroadcastWatch.forget(watch);
|
|
2110
|
+
};
|
|
2111
|
+
};
|
|
2112
|
+
InMemoryCache.prototype.gc = function (options) {
|
|
2113
|
+
canonicalStringify.reset();
|
|
2114
|
+
var ids = this.optimisticData.gc();
|
|
2115
|
+
if (options && !this.txCount) {
|
|
2116
|
+
if (options.resetResultCache) {
|
|
2117
|
+
this.resetResultCache(options.resetResultIdentities);
|
|
2118
|
+
}
|
|
2119
|
+
else if (options.resetResultIdentities) {
|
|
2120
|
+
this.storeReader.resetCanon();
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
return ids;
|
|
2124
|
+
};
|
|
2125
|
+
InMemoryCache.prototype.retain = function (rootId, optimistic) {
|
|
2126
|
+
return (optimistic ? this.optimisticData : this.data).retain(rootId);
|
|
2127
|
+
};
|
|
2128
|
+
InMemoryCache.prototype.release = function (rootId, optimistic) {
|
|
2129
|
+
return (optimistic ? this.optimisticData : this.data).release(rootId);
|
|
2130
|
+
};
|
|
2131
|
+
InMemoryCache.prototype.identify = function (object) {
|
|
2132
|
+
if (utilities.isReference(object))
|
|
2133
|
+
return object.__ref;
|
|
2134
|
+
try {
|
|
2135
|
+
return this.policies.identify(object)[0];
|
|
2136
|
+
}
|
|
2137
|
+
catch (e) {
|
|
2138
|
+
__DEV__ && globals.invariant.warn(e);
|
|
2139
|
+
}
|
|
2140
|
+
};
|
|
2141
|
+
InMemoryCache.prototype.evict = function (options) {
|
|
2142
|
+
if (!options.id) {
|
|
2143
|
+
if (hasOwn.call(options, "id")) {
|
|
2144
|
+
return false;
|
|
2145
|
+
}
|
|
2146
|
+
options = tslib.__assign(tslib.__assign({}, options), { id: "ROOT_QUERY" });
|
|
2147
|
+
}
|
|
2148
|
+
try {
|
|
2149
|
+
++this.txCount;
|
|
2150
|
+
return this.optimisticData.evict(options, this.data);
|
|
2151
|
+
}
|
|
2152
|
+
finally {
|
|
2153
|
+
if (!--this.txCount && options.broadcast !== false) {
|
|
2154
|
+
this.broadcastWatches();
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
};
|
|
2158
|
+
InMemoryCache.prototype.reset = function (options) {
|
|
2159
|
+
var _this = this;
|
|
2160
|
+
this.init();
|
|
2161
|
+
canonicalStringify.reset();
|
|
2162
|
+
if (options && options.discardWatches) {
|
|
2163
|
+
this.watches.forEach(function (watch) { return _this.maybeBroadcastWatch.forget(watch); });
|
|
2164
|
+
this.watches.clear();
|
|
2165
|
+
forgetCache(this);
|
|
2166
|
+
}
|
|
2167
|
+
else {
|
|
2168
|
+
this.broadcastWatches();
|
|
2169
|
+
}
|
|
2170
|
+
return Promise.resolve();
|
|
2171
|
+
};
|
|
2172
|
+
InMemoryCache.prototype.removeOptimistic = function (idToRemove) {
|
|
2173
|
+
var newOptimisticData = this.optimisticData.removeLayer(idToRemove);
|
|
2174
|
+
if (newOptimisticData !== this.optimisticData) {
|
|
2175
|
+
this.optimisticData = newOptimisticData;
|
|
2176
|
+
this.broadcastWatches();
|
|
2177
|
+
}
|
|
2178
|
+
};
|
|
2179
|
+
InMemoryCache.prototype.batch = function (options) {
|
|
2180
|
+
var _this = this;
|
|
2181
|
+
var update = options.update, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, removeOptimistic = options.removeOptimistic, onWatchUpdated = options.onWatchUpdated;
|
|
2182
|
+
var updateResult;
|
|
2183
|
+
var perform = function (layer) {
|
|
2184
|
+
var _a = _this, data = _a.data, optimisticData = _a.optimisticData;
|
|
2185
|
+
++_this.txCount;
|
|
2186
|
+
if (layer) {
|
|
2187
|
+
_this.data = _this.optimisticData = layer;
|
|
2188
|
+
}
|
|
2189
|
+
try {
|
|
2190
|
+
return updateResult = update(_this);
|
|
2191
|
+
}
|
|
2192
|
+
finally {
|
|
2193
|
+
--_this.txCount;
|
|
2194
|
+
_this.data = data;
|
|
2195
|
+
_this.optimisticData = optimisticData;
|
|
2196
|
+
}
|
|
2197
|
+
};
|
|
2198
|
+
var alreadyDirty = new Set();
|
|
2199
|
+
if (onWatchUpdated && !this.txCount) {
|
|
2200
|
+
this.broadcastWatches(tslib.__assign(tslib.__assign({}, options), { onWatchUpdated: function (watch) {
|
|
2201
|
+
alreadyDirty.add(watch);
|
|
2202
|
+
return false;
|
|
2203
|
+
} }));
|
|
2204
|
+
}
|
|
2205
|
+
if (typeof optimistic === 'string') {
|
|
2206
|
+
this.optimisticData = this.optimisticData.addLayer(optimistic, perform);
|
|
2207
|
+
}
|
|
2208
|
+
else if (optimistic === false) {
|
|
2209
|
+
perform(this.data);
|
|
2210
|
+
}
|
|
2211
|
+
else {
|
|
2212
|
+
perform();
|
|
2213
|
+
}
|
|
2214
|
+
if (typeof removeOptimistic === "string") {
|
|
2215
|
+
this.optimisticData = this.optimisticData.removeLayer(removeOptimistic);
|
|
2216
|
+
}
|
|
2217
|
+
if (onWatchUpdated && alreadyDirty.size) {
|
|
2218
|
+
this.broadcastWatches(tslib.__assign(tslib.__assign({}, options), { onWatchUpdated: function (watch, diff) {
|
|
2219
|
+
var result = onWatchUpdated.call(this, watch, diff);
|
|
2220
|
+
if (result !== false) {
|
|
2221
|
+
alreadyDirty.delete(watch);
|
|
2222
|
+
}
|
|
2223
|
+
return result;
|
|
2224
|
+
} }));
|
|
2225
|
+
if (alreadyDirty.size) {
|
|
2226
|
+
alreadyDirty.forEach(function (watch) { return _this.maybeBroadcastWatch.dirty(watch); });
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
else {
|
|
2230
|
+
this.broadcastWatches(options);
|
|
2231
|
+
}
|
|
2232
|
+
return updateResult;
|
|
2233
|
+
};
|
|
2234
|
+
InMemoryCache.prototype.performTransaction = function (update, optimisticId) {
|
|
2235
|
+
return this.batch({
|
|
2236
|
+
update: update,
|
|
2237
|
+
optimistic: optimisticId || (optimisticId !== null),
|
|
2238
|
+
});
|
|
2239
|
+
};
|
|
2240
|
+
InMemoryCache.prototype.transformDocument = function (document) {
|
|
2241
|
+
if (this.addTypename) {
|
|
2242
|
+
var result = this.typenameDocumentCache.get(document);
|
|
2243
|
+
if (!result) {
|
|
2244
|
+
result = utilities.addTypenameToDocument(document);
|
|
2245
|
+
this.typenameDocumentCache.set(document, result);
|
|
2246
|
+
this.typenameDocumentCache.set(result, result);
|
|
2247
|
+
}
|
|
2248
|
+
return result;
|
|
2249
|
+
}
|
|
2250
|
+
return document;
|
|
2251
|
+
};
|
|
2252
|
+
InMemoryCache.prototype.broadcastWatches = function (options) {
|
|
2253
|
+
var _this = this;
|
|
2254
|
+
if (!this.txCount) {
|
|
2255
|
+
this.watches.forEach(function (c) { return _this.maybeBroadcastWatch(c, options); });
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
InMemoryCache.prototype.broadcastWatch = function (c, options) {
|
|
2259
|
+
var lastDiff = c.lastDiff;
|
|
2260
|
+
var diff = this.diff(c);
|
|
2261
|
+
if (options) {
|
|
2262
|
+
if (c.optimistic &&
|
|
2263
|
+
typeof options.optimistic === "string") {
|
|
2264
|
+
diff.fromOptimisticTransaction = true;
|
|
2265
|
+
}
|
|
2266
|
+
if (options.onWatchUpdated &&
|
|
2267
|
+
options.onWatchUpdated.call(this, c, diff, lastDiff) === false) {
|
|
2268
|
+
return;
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
if (!lastDiff || !equality.equal(lastDiff.result, diff.result)) {
|
|
2272
|
+
c.callback(c.lastDiff = diff, lastDiff);
|
|
2273
|
+
}
|
|
2274
|
+
};
|
|
2275
|
+
return InMemoryCache;
|
|
2276
|
+
}(ApolloCache));
|
|
2277
|
+
|
|
2278
|
+
exports.isReference = utilities.isReference;
|
|
2279
|
+
exports.makeReference = utilities.makeReference;
|
|
2280
|
+
exports.ApolloCache = ApolloCache;
|
|
2281
|
+
exports.InMemoryCache = InMemoryCache;
|
|
2282
|
+
exports.MissingFieldError = MissingFieldError;
|
|
2283
|
+
exports.Policies = Policies;
|
|
2284
|
+
exports.cacheSlot = cacheSlot;
|
|
2285
|
+
exports.canonicalStringify = canonicalStringify;
|
|
2286
|
+
exports.defaultDataIdFromObject = defaultDataIdFromObject;
|
|
2287
|
+
exports.fieldNameFromStoreName = fieldNameFromStoreName;
|
|
2288
|
+
exports.makeVar = makeVar;
|
|
2289
|
+
//# sourceMappingURL=cache.cjs.map
|