@apollo/client 4.0.0-alpha.20 → 4.0.0-alpha.21
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/CHANGELOG.md +10 -0
- package/__cjs/cache/core/cache.cjs +1 -1
- package/__cjs/cache/inmemory/entityStore.cjs +3 -3
- package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
- package/__cjs/cache/inmemory/policies.cjs +4 -4
- package/__cjs/cache/inmemory/policies.cjs.map +1 -1
- package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
- package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
- package/__cjs/core/ApolloClient.cjs +10 -10
- package/__cjs/core/ObservableQuery.cjs +15 -11
- package/__cjs/core/ObservableQuery.cjs.map +1 -1
- package/__cjs/core/ObservableQuery.d.cts +1 -2
- package/__cjs/core/QueryInfo.cjs +223 -34
- package/__cjs/core/QueryInfo.cjs.map +1 -1
- package/__cjs/core/QueryInfo.d.cts +34 -24
- package/__cjs/core/QueryManager.cjs +75 -256
- package/__cjs/core/QueryManager.cjs.map +1 -1
- package/__cjs/core/QueryManager.d.cts +4 -36
- package/__cjs/core/networkStatus.cjs +7 -1
- package/__cjs/core/networkStatus.cjs.map +1 -1
- package/__cjs/core/networkStatus.d.cts +7 -1
- package/__cjs/invariantErrorCodes.cjs +64 -69
- package/__cjs/link/core/ApolloLink.cjs +2 -2
- package/__cjs/link/http/checkFetcher.cjs +1 -1
- package/__cjs/link/http/createHttpLink.cjs +1 -1
- package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
- package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
- package/__cjs/link/persisted-queries/index.cjs +2 -2
- package/__cjs/link/utils/validateOperation.cjs +1 -1
- package/__cjs/local-state/LocalState.cjs +10 -10
- package/__cjs/masking/maskDefinition.cjs.map +1 -1
- package/__cjs/masking/maskFragment.cjs +0 -8
- package/__cjs/masking/maskFragment.cjs.map +1 -1
- package/__cjs/masking/maskOperation.cjs +0 -8
- package/__cjs/masking/maskOperation.cjs.map +1 -1
- package/__cjs/masking/utils.cjs +3 -11
- package/__cjs/masking/utils.cjs.map +1 -1
- package/__cjs/masking/utils.d.cts +0 -1
- package/__cjs/react/hooks/useLazyQuery.cjs +1 -2
- package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLazyQuery.d.cts +0 -8
- package/__cjs/utilities/index.cjs +1 -16
- package/__cjs/utilities/index.cjs.map +1 -1
- package/__cjs/utilities/index.d.cts +0 -13
- package/__cjs/version.cjs +1 -1
- package/cache/core/cache.js +1 -1
- package/cache/inmemory/entityStore.js +3 -3
- package/cache/inmemory/key-extractor.js +1 -1
- package/cache/inmemory/policies.js +4 -4
- package/cache/inmemory/policies.js.map +1 -1
- package/cache/inmemory/readFromStore.js +2 -2
- package/cache/inmemory/writeToStore.js +4 -4
- package/core/ApolloClient.js +10 -10
- package/core/ObservableQuery.d.ts +1 -2
- package/core/ObservableQuery.js +15 -11
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.d.ts +34 -24
- package/core/QueryInfo.js +221 -34
- package/core/QueryInfo.js.map +1 -1
- package/core/QueryManager.d.ts +4 -36
- package/core/QueryManager.js +77 -258
- package/core/QueryManager.js.map +1 -1
- package/core/networkStatus.d.ts +7 -1
- package/core/networkStatus.js +7 -1
- package/core/networkStatus.js.map +1 -1
- package/invariantErrorCodes.js +64 -69
- package/link/core/ApolloLink.js +2 -2
- package/link/http/checkFetcher.js +1 -1
- package/link/http/createHttpLink.js +1 -1
- package/link/http/parseAndCheckHttpResponse.js +1 -1
- package/link/http/serializeFetchParameter.js +1 -1
- package/link/persisted-queries/index.js +2 -2
- package/link/utils/validateOperation.js +1 -1
- package/local-state/LocalState.js +10 -10
- package/masking/maskDefinition.js.map +1 -1
- package/masking/maskFragment.js +0 -8
- package/masking/maskFragment.js.map +1 -1
- package/masking/maskOperation.js +0 -8
- package/masking/maskOperation.js.map +1 -1
- package/masking/utils.d.ts +0 -1
- package/masking/utils.js +3 -10
- package/masking/utils.js.map +1 -1
- package/package.json +1 -1
- package/react/hooks/useLazyQuery.d.ts +0 -8
- package/react/hooks/useLazyQuery.js +1 -2
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/utilities/index.d.ts +0 -13
- package/utilities/index.js +0 -12
- package/utilities/index.js.map +1 -1
- package/version.js +1 -1
package/core/QueryInfo.d.ts
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
1
|
import type { DocumentNode } from "graphql";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ApolloCache } from "@apollo/client/cache";
|
|
3
3
|
import type { FetchResult } from "@apollo/client/link";
|
|
4
4
|
import type { ObservableQuery } from "./ObservableQuery.js";
|
|
5
5
|
import type { QueryManager } from "./QueryManager.js";
|
|
6
|
-
import type { OperationVariables } from "./types.js";
|
|
7
|
-
import type { ErrorPolicy, WatchQueryOptions } from "./watchQueryOptions.js";
|
|
6
|
+
import type { DefaultContext, InternalRefetchQueriesInclude, MutationUpdaterFunction, OnQueryUpdated, OperationVariables, TypedDocumentNode } from "./types.js";
|
|
7
|
+
import type { ErrorPolicy, MutationOptions, WatchQueryOptions } from "./watchQueryOptions.js";
|
|
8
|
+
type UpdateQueries<TData> = MutationOptions<TData, any, any>["updateQueries"];
|
|
8
9
|
export declare const enum CacheWriteBehavior {
|
|
9
10
|
FORBID = 0,
|
|
10
11
|
OVERWRITE = 1,
|
|
11
12
|
MERGE = 2
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
interface LastWrite {
|
|
14
15
|
result: FetchResult<any>;
|
|
15
16
|
variables: WatchQueryOptions["variables"];
|
|
16
17
|
dmCount: number | undefined;
|
|
17
18
|
}
|
|
19
|
+
interface OperationInfo<TData, TVariables extends OperationVariables, AllowedCacheWriteBehavior = CacheWriteBehavior> {
|
|
20
|
+
document: DocumentNode | TypedDocumentNode<TData, TVariables>;
|
|
21
|
+
variables: TVariables;
|
|
22
|
+
errorPolicy: ErrorPolicy;
|
|
23
|
+
cacheWriteBehavior: AllowedCacheWriteBehavior;
|
|
24
|
+
}
|
|
18
25
|
export declare class QueryInfo {
|
|
19
|
-
document: DocumentNode | null;
|
|
20
26
|
lastRequestId: number;
|
|
21
|
-
variables?: Record<string, any>;
|
|
22
27
|
private cache;
|
|
23
|
-
|
|
24
|
-
readonly
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
queryId?: string;
|
|
28
|
-
observableQuery?: ObservableQuery<any, any>;
|
|
29
|
-
});
|
|
30
|
-
init(query: {
|
|
31
|
-
document: DocumentNode;
|
|
32
|
-
variables: Record<string, any> | undefined;
|
|
33
|
-
}): this;
|
|
34
|
-
getDiffOptions(variables?: Record<string, any> | undefined): Cache.DiffOptions;
|
|
28
|
+
private queryManager;
|
|
29
|
+
readonly id: string;
|
|
30
|
+
private readonly observableQuery?;
|
|
31
|
+
constructor(queryManager: QueryManager, observableQuery?: ObservableQuery<any, any>);
|
|
35
32
|
/**
|
|
36
33
|
* @internal
|
|
37
34
|
* For feud-preventing behaviour, `lastWrite` should be shared by all `QueryInfo` instances of an `ObservableQuery`.
|
|
@@ -44,12 +41,25 @@ export declare class QueryInfo {
|
|
|
44
41
|
private set lastWrite(value);
|
|
45
42
|
resetLastWrite(): void;
|
|
46
43
|
private shouldWrite;
|
|
47
|
-
resetDiff(): void;
|
|
48
44
|
private lastDiff?;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
markQueryResult<TData, TVariables extends OperationVariables>(result: FetchResult<TData>, { document: query, variables, errorPolicy, cacheWriteBehavior, }: OperationInfo<TData, TVariables>): void;
|
|
46
|
+
markMutationResult<TData, TVariables extends OperationVariables, TCache extends ApolloCache>(result: FetchResult<TData>, mutation: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE> & {
|
|
47
|
+
context?: DefaultContext;
|
|
48
|
+
updateQueries: UpdateQueries<TData>;
|
|
49
|
+
update?: MutationUpdaterFunction<TData, TVariables, TCache>;
|
|
50
|
+
awaitRefetchQueries?: boolean;
|
|
51
|
+
refetchQueries?: InternalRefetchQueriesInclude;
|
|
52
|
+
removeOptimistic?: string;
|
|
53
|
+
onQueryUpdated?: OnQueryUpdated<any>;
|
|
54
|
+
keepRootFields?: boolean;
|
|
55
|
+
}, cache?: ApolloCache): Promise<FetchResult<TData>>;
|
|
56
|
+
markMutationOptimistic<TData, TVariables extends OperationVariables, TCache extends ApolloCache>(optimisticResponse: any, mutation: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE> & {
|
|
57
|
+
context?: DefaultContext;
|
|
58
|
+
updateQueries: UpdateQueries<TData>;
|
|
59
|
+
update?: MutationUpdaterFunction<TData, TVariables, TCache>;
|
|
60
|
+
keepRootFields?: boolean;
|
|
61
|
+
}): boolean;
|
|
62
|
+
markSubscriptionResult<TData, TVariables extends OperationVariables>(result: FetchResult<TData>, { document, variables, errorPolicy, cacheWriteBehavior, }: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE>): void;
|
|
53
63
|
}
|
|
54
|
-
export
|
|
64
|
+
export {};
|
|
55
65
|
//# sourceMappingURL=QueryInfo.d.ts.map
|
package/core/QueryInfo.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
1
2
|
import { equal } from "@wry/equality";
|
|
2
|
-
import { DeepMerger, graphQLResultHasError, isNonEmptyArray, mergeIncrementalData, } from "@apollo/client/utilities/internal";
|
|
3
|
+
import { DeepMerger, getOperationName, graphQLResultHasError, isExecutionPatchIncrementalResult, isExecutionPatchResult, isNonEmptyArray, mergeIncrementalData, } from "@apollo/client/utilities/internal";
|
|
4
|
+
import { invariant } from "@apollo/client/utilities/invariant";
|
|
5
|
+
const IGNORE = {};
|
|
3
6
|
const destructiveMethodCounts = new WeakMap();
|
|
4
7
|
function wrapDestructiveCacheMethod(cache, methodName) {
|
|
5
8
|
const original = cache[methodName];
|
|
@@ -17,26 +20,28 @@ function wrapDestructiveCacheMethod(cache, methodName) {
|
|
|
17
20
|
};
|
|
18
21
|
}
|
|
19
22
|
}
|
|
23
|
+
const queryInfoIds = new WeakMap();
|
|
20
24
|
// A QueryInfo object represents a single network request, either initiated
|
|
21
25
|
// from the QueryManager or from an ObservableQuery.
|
|
22
26
|
// It will only ever be used for a single network call.
|
|
23
27
|
// It is responsible for reporting results to the cache, merging and in a no-cache
|
|
24
28
|
// scenario accumulating the response.
|
|
25
29
|
export class QueryInfo {
|
|
26
|
-
document = null;
|
|
27
30
|
// TODO remove soon - this should be able to be handled by cancelling old operations before starting new ones
|
|
28
31
|
lastRequestId = 1;
|
|
29
|
-
variables;
|
|
30
32
|
cache;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
queryManager;
|
|
34
|
+
id;
|
|
33
35
|
observableQuery;
|
|
34
|
-
constructor(
|
|
36
|
+
constructor(queryManager, observableQuery) {
|
|
35
37
|
const cache = (this.cache = queryManager.cache);
|
|
36
|
-
|
|
38
|
+
const id = (queryInfoIds.get(queryManager) || 0) + 1;
|
|
39
|
+
queryInfoIds.set(queryManager, id);
|
|
40
|
+
this.id = id + "";
|
|
37
41
|
this.observableQuery = observableQuery;
|
|
42
|
+
this.queryManager = queryManager;
|
|
38
43
|
// Track how often cache.evict is called, since we want eviction to
|
|
39
|
-
// override the feud-stopping logic in the
|
|
44
|
+
// override the feud-stopping logic in the markQueryResult method, by
|
|
40
45
|
// causing shouldWrite to return true. Wrapping the cache.evict method
|
|
41
46
|
// is a bit of a hack, but it saves us from having to make eviction
|
|
42
47
|
// counting an official part of the ApolloCache API.
|
|
@@ -47,19 +52,6 @@ export class QueryInfo {
|
|
|
47
52
|
wrapDestructiveCacheMethod(cache, "reset");
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
|
-
init(query) {
|
|
51
|
-
this.document = query.document;
|
|
52
|
-
this.variables = query.variables;
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
getDiffOptions(variables = this.variables) {
|
|
56
|
-
return {
|
|
57
|
-
query: this.document,
|
|
58
|
-
variables,
|
|
59
|
-
returnPartialData: true,
|
|
60
|
-
optimistic: true,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
55
|
/**
|
|
64
56
|
* @internal
|
|
65
57
|
* For feud-preventing behaviour, `lastWrite` should be shared by all `QueryInfo` instances of an `ObservableQuery`.
|
|
@@ -87,16 +79,18 @@ export class QueryInfo {
|
|
|
87
79
|
equal(variables, lastWrite.variables) &&
|
|
88
80
|
equal(result.data, lastWrite.result.data));
|
|
89
81
|
}
|
|
90
|
-
resetDiff() {
|
|
91
|
-
this.lastDiff = void 0;
|
|
92
|
-
}
|
|
93
82
|
lastDiff;
|
|
94
|
-
|
|
83
|
+
markQueryResult(result, { document: query, variables, errorPolicy, cacheWriteBehavior, }) {
|
|
84
|
+
const diffOptions = {
|
|
85
|
+
query,
|
|
86
|
+
variables,
|
|
87
|
+
returnPartialData: true,
|
|
88
|
+
optimistic: true,
|
|
89
|
+
};
|
|
95
90
|
// Cancel the pending notify timeout (if it exists) to prevent extraneous network
|
|
96
91
|
// requests. To allow future notify timeouts, diff and dirty are reset as well.
|
|
97
92
|
this.observableQuery?.["resetNotifications"]();
|
|
98
93
|
if (cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */) {
|
|
99
|
-
const diffOptions = this.getDiffOptions(options.variables);
|
|
100
94
|
const lastDiff = this.lastDiff && equal(diffOptions, this.lastDiff.options) ?
|
|
101
95
|
this.lastDiff.diff
|
|
102
96
|
: { result: null, complete: false };
|
|
@@ -107,9 +101,9 @@ export class QueryInfo {
|
|
|
107
101
|
};
|
|
108
102
|
}
|
|
109
103
|
else {
|
|
110
|
-
const lastDiff = this.cache.diff(
|
|
104
|
+
const lastDiff = this.cache.diff(diffOptions);
|
|
111
105
|
handleIncrementalResult(result, lastDiff);
|
|
112
|
-
if (shouldWriteResult(result,
|
|
106
|
+
if (shouldWriteResult(result, errorPolicy)) {
|
|
113
107
|
// Using a transaction here so we have a chance to read the result
|
|
114
108
|
// back from the cache before the watch callback fires as a result
|
|
115
109
|
// of writeQuery, so we can store the new diff quietly and ignore
|
|
@@ -125,16 +119,16 @@ export class QueryInfo {
|
|
|
125
119
|
}
|
|
126
120
|
},
|
|
127
121
|
update: (cache) => {
|
|
128
|
-
if (this.shouldWrite(result,
|
|
122
|
+
if (this.shouldWrite(result, variables)) {
|
|
129
123
|
cache.writeQuery({
|
|
130
|
-
query
|
|
124
|
+
query,
|
|
131
125
|
data: result.data,
|
|
132
|
-
variables
|
|
126
|
+
variables,
|
|
133
127
|
overwrite: cacheWriteBehavior === 1 /* CacheWriteBehavior.OVERWRITE */,
|
|
134
128
|
});
|
|
135
129
|
this.lastWrite = {
|
|
136
130
|
result,
|
|
137
|
-
variables
|
|
131
|
+
variables,
|
|
138
132
|
dmCount: destructiveMethodCounts.get(this.cache),
|
|
139
133
|
};
|
|
140
134
|
}
|
|
@@ -180,7 +174,6 @@ export class QueryInfo {
|
|
|
180
174
|
// If the previous this.diff was incomplete, fall through to
|
|
181
175
|
// re-reading the latest data with cache.diff, below.
|
|
182
176
|
}
|
|
183
|
-
const diffOptions = this.getDiffOptions(options.variables);
|
|
184
177
|
const diff = cache.diff(diffOptions);
|
|
185
178
|
// If we're allowed to write to the cache, and we can read a
|
|
186
179
|
// complete result from the cache, update result.data to be the
|
|
@@ -198,6 +191,200 @@ export class QueryInfo {
|
|
|
198
191
|
}
|
|
199
192
|
}
|
|
200
193
|
}
|
|
194
|
+
markMutationResult(result, mutation, cache = this.cache) {
|
|
195
|
+
const cacheWrites = [];
|
|
196
|
+
const skipCache = mutation.cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
|
|
197
|
+
if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
|
|
198
|
+
if (!isExecutionPatchIncrementalResult(result)) {
|
|
199
|
+
cacheWrites.push({
|
|
200
|
+
result: result.data,
|
|
201
|
+
dataId: "ROOT_MUTATION",
|
|
202
|
+
query: mutation.document,
|
|
203
|
+
variables: mutation.variables,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
if (isExecutionPatchIncrementalResult(result) &&
|
|
207
|
+
isNonEmptyArray(result.incremental)) {
|
|
208
|
+
const diff = cache.diff({
|
|
209
|
+
id: "ROOT_MUTATION",
|
|
210
|
+
// The cache complains if passed a mutation where it expects a
|
|
211
|
+
// query, so we transform mutations and subscriptions to queries
|
|
212
|
+
// (only once, thanks to this.transformCache).
|
|
213
|
+
query: this.queryManager.getDocumentInfo(mutation.document).asQuery,
|
|
214
|
+
variables: mutation.variables,
|
|
215
|
+
optimistic: false,
|
|
216
|
+
returnPartialData: true,
|
|
217
|
+
});
|
|
218
|
+
let mergedData;
|
|
219
|
+
if (diff.result) {
|
|
220
|
+
mergedData = mergeIncrementalData(diff.result, result);
|
|
221
|
+
}
|
|
222
|
+
if (typeof mergedData !== "undefined") {
|
|
223
|
+
// cast the ExecutionPatchResult to FetchResult here since
|
|
224
|
+
// ExecutionPatchResult never has `data` when returned from the server
|
|
225
|
+
result.data = mergedData;
|
|
226
|
+
cacheWrites.push({
|
|
227
|
+
result: mergedData,
|
|
228
|
+
dataId: "ROOT_MUTATION",
|
|
229
|
+
query: mutation.document,
|
|
230
|
+
variables: mutation.variables,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const { updateQueries } = mutation;
|
|
235
|
+
if (updateQueries) {
|
|
236
|
+
this.queryManager
|
|
237
|
+
.getObservableQueries("all")
|
|
238
|
+
.forEach((observableQuery) => {
|
|
239
|
+
const queryName = observableQuery && observableQuery.queryName;
|
|
240
|
+
if (!queryName ||
|
|
241
|
+
!Object.hasOwnProperty.call(updateQueries, queryName)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const updater = updateQueries[queryName];
|
|
245
|
+
const { query: document, variables } = observableQuery;
|
|
246
|
+
// Read the current query result from the store.
|
|
247
|
+
const { result: currentQueryResult, complete } = observableQuery.getCacheDiff({ optimistic: false });
|
|
248
|
+
if (complete && currentQueryResult) {
|
|
249
|
+
// Run our reducer using the current query result and the mutation result.
|
|
250
|
+
const nextQueryResult = updater(currentQueryResult, {
|
|
251
|
+
mutationResult: result,
|
|
252
|
+
queryName: (document && getOperationName(document)) || void 0,
|
|
253
|
+
queryVariables: variables,
|
|
254
|
+
});
|
|
255
|
+
// Write the modified result back into the store if we got a new result.
|
|
256
|
+
if (nextQueryResult) {
|
|
257
|
+
cacheWrites.push({
|
|
258
|
+
result: nextQueryResult,
|
|
259
|
+
dataId: "ROOT_QUERY",
|
|
260
|
+
query: document,
|
|
261
|
+
variables,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (cacheWrites.length > 0 ||
|
|
269
|
+
(mutation.refetchQueries || "").length > 0 ||
|
|
270
|
+
mutation.update ||
|
|
271
|
+
mutation.onQueryUpdated ||
|
|
272
|
+
mutation.removeOptimistic) {
|
|
273
|
+
const results = [];
|
|
274
|
+
this.queryManager
|
|
275
|
+
.refetchQueries({
|
|
276
|
+
updateCache: (cache) => {
|
|
277
|
+
if (!skipCache) {
|
|
278
|
+
cacheWrites.forEach((write) => cache.write(write));
|
|
279
|
+
}
|
|
280
|
+
// If the mutation has some writes associated with it then we need to
|
|
281
|
+
// apply those writes to the store by running this reducer again with
|
|
282
|
+
// a write action.
|
|
283
|
+
const { update } = mutation;
|
|
284
|
+
// Determine whether result is a SingleExecutionResult,
|
|
285
|
+
// or the final ExecutionPatchResult.
|
|
286
|
+
const isFinalResult = !isExecutionPatchResult(result) ||
|
|
287
|
+
(isExecutionPatchIncrementalResult(result) && !result.hasNext);
|
|
288
|
+
if (update) {
|
|
289
|
+
if (!skipCache) {
|
|
290
|
+
// Re-read the ROOT_MUTATION data we just wrote into the cache
|
|
291
|
+
// (the first cache.write call in the cacheWrites.forEach loop
|
|
292
|
+
// above), so field read functions have a chance to run for
|
|
293
|
+
// fields within mutation result objects.
|
|
294
|
+
const diff = cache.diff({
|
|
295
|
+
id: "ROOT_MUTATION",
|
|
296
|
+
// The cache complains if passed a mutation where it expects a
|
|
297
|
+
// query, so we transform mutations and subscriptions to queries
|
|
298
|
+
// (only once, thanks to this.transformCache).
|
|
299
|
+
query: this.queryManager.getDocumentInfo(mutation.document)
|
|
300
|
+
.asQuery,
|
|
301
|
+
variables: mutation.variables,
|
|
302
|
+
optimistic: false,
|
|
303
|
+
returnPartialData: true,
|
|
304
|
+
});
|
|
305
|
+
if (diff.complete) {
|
|
306
|
+
result = { ...result, data: diff.result };
|
|
307
|
+
if ("incremental" in result) {
|
|
308
|
+
delete result.incremental;
|
|
309
|
+
}
|
|
310
|
+
if ("hasNext" in result) {
|
|
311
|
+
delete result.hasNext;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// If we've received the whole response,
|
|
316
|
+
// either a SingleExecutionResult or the final ExecutionPatchResult,
|
|
317
|
+
// call the update function.
|
|
318
|
+
if (isFinalResult) {
|
|
319
|
+
update(cache, result, {
|
|
320
|
+
context: mutation.context,
|
|
321
|
+
variables: mutation.variables,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// TODO Do this with cache.evict({ id: 'ROOT_MUTATION' }) but make it
|
|
326
|
+
// shallow to allow rolling back optimistic evictions.
|
|
327
|
+
if (!skipCache && !mutation.keepRootFields && isFinalResult) {
|
|
328
|
+
cache.modify({
|
|
329
|
+
id: "ROOT_MUTATION",
|
|
330
|
+
fields(value, { fieldName, DELETE }) {
|
|
331
|
+
return fieldName === "__typename" ? value : DELETE;
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
include: mutation.refetchQueries,
|
|
337
|
+
// Write the final mutation.result to the root layer of the cache.
|
|
338
|
+
optimistic: false,
|
|
339
|
+
// Remove the corresponding optimistic layer at the same time as we
|
|
340
|
+
// write the final non-optimistic result.
|
|
341
|
+
removeOptimistic: mutation.removeOptimistic,
|
|
342
|
+
// Let the caller of client.mutate optionally determine the refetching
|
|
343
|
+
// behavior for watched queries after the mutation.update function runs.
|
|
344
|
+
// If no onQueryUpdated function was provided for this mutation, pass
|
|
345
|
+
// null instead of undefined to disable the default refetching behavior.
|
|
346
|
+
onQueryUpdated: mutation.onQueryUpdated || null,
|
|
347
|
+
})
|
|
348
|
+
.forEach((result) => results.push(result));
|
|
349
|
+
if (mutation.awaitRefetchQueries || mutation.onQueryUpdated) {
|
|
350
|
+
// Returning a promise here makes the mutation await that promise, so we
|
|
351
|
+
// include results in that promise's work if awaitRefetchQueries or an
|
|
352
|
+
// onQueryUpdated function was specified.
|
|
353
|
+
return Promise.all(results).then(() => result);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return Promise.resolve(result);
|
|
357
|
+
}
|
|
358
|
+
markMutationOptimistic(optimisticResponse, mutation) {
|
|
359
|
+
const data = typeof optimisticResponse === "function" ?
|
|
360
|
+
optimisticResponse(mutation.variables, { IGNORE })
|
|
361
|
+
: optimisticResponse;
|
|
362
|
+
if (data === IGNORE) {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
this.cache.recordOptimisticTransaction((cache) => {
|
|
366
|
+
try {
|
|
367
|
+
this.markMutationResult({ data }, mutation, cache);
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
__DEV__ && invariant.error(error);
|
|
371
|
+
}
|
|
372
|
+
}, this.id);
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
markSubscriptionResult(result, { document, variables, errorPolicy, cacheWriteBehavior, }) {
|
|
376
|
+
if (cacheWriteBehavior !== 0 /* CacheWriteBehavior.FORBID */) {
|
|
377
|
+
if (shouldWriteResult(result, errorPolicy)) {
|
|
378
|
+
this.cache.write({
|
|
379
|
+
query: document,
|
|
380
|
+
result: result.data,
|
|
381
|
+
dataId: "ROOT_SUBSCRIPTION",
|
|
382
|
+
variables: variables,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
this.queryManager.broadcastQueries();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
201
388
|
}
|
|
202
389
|
function handleIncrementalResult(result, lastDiff) {
|
|
203
390
|
if ("incremental" in result && isNonEmptyArray(result.incremental)) {
|
|
@@ -214,7 +401,7 @@ function handleIncrementalResult(result, lastDiff) {
|
|
|
214
401
|
result.data = merger.merge(lastDiff.result, result.data);
|
|
215
402
|
}
|
|
216
403
|
}
|
|
217
|
-
|
|
404
|
+
function shouldWriteResult(result, errorPolicy = "none") {
|
|
218
405
|
const ignoreErrors = errorPolicy === "ignore" || errorPolicy === "all";
|
|
219
406
|
let writeWithErrors = !graphQLResultHasError(result);
|
|
220
407
|
if (!writeWithErrors && ignoreErrors && result.data) {
|