@apollo/client 4.0.0-alpha.22 → 4.0.0-alpha.23
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 +238 -2
- 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/key-extractor.cjs.map +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 +15 -13
- package/__cjs/core/ApolloClient.cjs.map +1 -1
- package/__cjs/core/ApolloClient.d.cts +8 -3
- package/__cjs/core/ObservableQuery.cjs +52 -48
- package/__cjs/core/ObservableQuery.cjs.map +1 -1
- package/__cjs/core/ObservableQuery.d.cts +48 -14
- package/__cjs/core/QueryInfo.cjs +146 -168
- package/__cjs/core/QueryInfo.cjs.map +1 -1
- package/__cjs/core/QueryInfo.d.cts +14 -11
- package/__cjs/core/QueryManager.cjs +121 -111
- package/__cjs/core/QueryManager.cjs.map +1 -1
- package/__cjs/core/QueryManager.d.cts +4 -0
- package/__cjs/core/index.cjs +2 -1
- package/__cjs/core/index.cjs.map +1 -1
- package/__cjs/core/index.d.cts +4 -4
- package/__cjs/core/types.d.cts +68 -15
- package/__cjs/core/watchQueryOptions.d.cts +2 -24
- package/__cjs/dev/setErrorMessageHandler.cjs.map +1 -1
- package/__cjs/errors/CombinedGraphQLErrors.cjs +6 -3
- package/__cjs/errors/CombinedGraphQLErrors.cjs.map +1 -1
- package/__cjs/errors/CombinedGraphQLErrors.d.cts +7 -2
- package/__cjs/errors/index.cjs +1 -1
- package/__cjs/errors/index.cjs.map +1 -1
- package/__cjs/errors/index.d.cts +1 -3
- package/__cjs/incremental/handlers/defer20220824.cjs +102 -0
- package/__cjs/incremental/handlers/defer20220824.cjs.map +1 -0
- package/__cjs/incremental/handlers/defer20220824.d.cts +55 -0
- package/__cjs/incremental/handlers/notImplemented.cjs +19 -0
- package/__cjs/incremental/handlers/notImplemented.cjs.map +1 -0
- package/__cjs/incremental/handlers/notImplemented.d.cts +9 -0
- package/__cjs/incremental/index.cjs +9 -0
- package/__cjs/incremental/index.cjs.map +1 -0
- package/__cjs/incremental/index.d.cts +4 -0
- package/__cjs/incremental/types.cjs +3 -0
- package/__cjs/incremental/types.cjs.map +1 -0
- package/__cjs/incremental/types.d.cts +24 -0
- package/__cjs/invariantErrorCodes.cjs +62 -45
- package/__cjs/link/context/index.cjs +36 -25
- package/__cjs/link/context/index.cjs.map +1 -1
- package/__cjs/link/context/index.d.cts +8 -1
- package/__cjs/link/core/types.d.cts +4 -32
- package/__cjs/link/error/index.cjs +68 -65
- package/__cjs/link/error/index.cjs.map +1 -1
- package/__cjs/link/error/index.d.cts +15 -14
- package/__cjs/link/http/BaseHttpLink.cjs +0 -6
- package/__cjs/link/http/BaseHttpLink.cjs.map +1 -1
- package/__cjs/link/http/HttpLink.cjs +4 -0
- package/__cjs/link/http/HttpLink.cjs.map +1 -1
- package/__cjs/link/http/HttpLink.d.cts +4 -0
- package/__cjs/link/http/parseAndCheckHttpResponse.cjs +45 -57
- package/__cjs/link/http/parseAndCheckHttpResponse.cjs.map +1 -1
- package/__cjs/link/index.cjs.map +1 -1
- package/__cjs/link/index.d.cts +1 -1
- package/__cjs/link/persisted-queries/index.cjs +158 -142
- package/__cjs/link/persisted-queries/index.cjs.map +1 -1
- package/__cjs/link/persisted-queries/index.d.cts +19 -19
- package/__cjs/link/remove-typename/index.cjs +2 -1
- package/__cjs/link/remove-typename/index.cjs.map +1 -1
- package/__cjs/link/remove-typename/index.d.cts +1 -1
- package/__cjs/link/remove-typename/removeTypenameFromVariables.cjs +34 -23
- package/__cjs/link/remove-typename/removeTypenameFromVariables.cjs.map +1 -1
- package/__cjs/link/remove-typename/removeTypenameFromVariables.d.cts +8 -9
- package/__cjs/local-state/LocalState.cjs.map +1 -1
- package/__cjs/local-state/LocalState.d.cts +3 -4
- package/__cjs/masking/types.d.cts +6 -5
- package/__cjs/react/hooks/useBackgroundQuery.cjs +1 -0
- package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useFragment.cjs +1 -0
- package/__cjs/react/hooks/useFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useLazyQuery.cjs +2 -2
- package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLazyQuery.d.cts +2 -7
- package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useMutation.cjs.map +1 -1
- package/__cjs/react/hooks/useMutation.d.cts +2 -2
- package/__cjs/react/hooks/useQuery.cjs +23 -21
- package/__cjs/react/hooks/useQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useQuery.d.cts +3 -8
- package/__cjs/react/hooks/useQueryRefHandlers.cjs +1 -0
- package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
- package/__cjs/react/hooks/useReadQuery.cjs +1 -0
- package/__cjs/react/hooks/useReadQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseFragment.cjs +1 -0
- package/__cjs/react/hooks/useSuspenseFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseQuery.cjs +1 -0
- package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
- package/__cjs/react/internal/cache/QueryReference.cjs +4 -4
- package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
- package/__cjs/react/internal/cache/QueryReference.d.cts +2 -3
- package/__cjs/react/internal/types.d.cts +2 -7
- package/__cjs/utilities/graphql/isFormattedExecutionResult.cjs +9 -0
- package/__cjs/utilities/graphql/isFormattedExecutionResult.cjs.map +1 -0
- package/__cjs/utilities/graphql/isFormattedExecutionResult.d.cts +3 -0
- package/__cjs/utilities/index.cjs +3 -1
- package/__cjs/utilities/index.cjs.map +1 -1
- package/__cjs/utilities/index.d.cts +1 -0
- package/__cjs/utilities/internal/getGraphQLErrorsFromResult.cjs +1 -12
- package/__cjs/utilities/internal/getGraphQLErrorsFromResult.cjs.map +1 -1
- package/__cjs/utilities/internal/getGraphQLErrorsFromResult.d.cts +4 -2
- package/__cjs/utilities/internal/graphQLResultHasError.cjs +1 -4
- package/__cjs/utilities/internal/graphQLResultHasError.cjs.map +1 -1
- package/__cjs/utilities/internal/graphQLResultHasError.d.cts +2 -2
- package/__cjs/utilities/internal/index.cjs +2 -10
- package/__cjs/utilities/internal/index.cjs.map +1 -1
- package/__cjs/utilities/internal/index.d.cts +0 -4
- package/__cjs/version.cjs +3 -2
- package/__cjs/version.cjs.map +1 -1
- package/__cjs/version.d.cts +1 -0
- 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/key-extractor.js.map +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.d.ts +8 -3
- package/core/ApolloClient.js +15 -13
- package/core/ApolloClient.js.map +1 -1
- package/core/ObservableQuery.d.ts +48 -14
- package/core/ObservableQuery.js +53 -49
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.d.ts +14 -11
- package/core/QueryInfo.js +147 -169
- package/core/QueryInfo.js.map +1 -1
- package/core/QueryManager.d.ts +4 -0
- package/core/QueryManager.js +123 -113
- package/core/QueryManager.js.map +1 -1
- package/core/index.d.ts +4 -4
- package/core/index.js +1 -1
- package/core/index.js.map +1 -1
- package/core/types.d.ts +68 -15
- package/core/watchQueryOptions.d.ts +2 -24
- package/dev/setErrorMessageHandler.js.map +1 -1
- package/errors/CombinedGraphQLErrors.d.ts +7 -2
- package/errors/CombinedGraphQLErrors.js +6 -3
- package/errors/CombinedGraphQLErrors.js.map +1 -1
- package/errors/index.d.ts +1 -3
- package/errors/index.js +1 -1
- package/errors/index.js.map +1 -1
- package/incremental/handlers/defer20220824.d.ts +55 -0
- package/incremental/handlers/defer20220824.js +97 -0
- package/incremental/handlers/defer20220824.js.map +1 -0
- package/incremental/handlers/notImplemented.d.ts +9 -0
- package/incremental/handlers/notImplemented.js +15 -0
- package/incremental/handlers/notImplemented.js.map +1 -0
- package/incremental/index.d.ts +4 -0
- package/incremental/index.js +3 -0
- package/incremental/index.js.map +1 -0
- package/incremental/types.d.ts +24 -0
- package/incremental/types.js +2 -0
- package/incremental/types.js.map +1 -0
- package/invariantErrorCodes.js +63 -49
- package/legacyEntryPoints/incremental/incremental.cjs +1 -0
- package/legacyEntryPoints/incremental/incremental.d.cts +1 -0
- package/legacyEntryPoints/incremental/index.d.ts +1 -0
- package/legacyEntryPoints/incremental/index.js +1 -0
- package/link/context/index.d.ts +8 -1
- package/link/context/index.js +34 -25
- package/link/context/index.js.map +1 -1
- package/link/core/types.d.ts +4 -32
- package/link/error/index.d.ts +15 -14
- package/link/error/index.js +68 -65
- package/link/error/index.js.map +1 -1
- package/link/http/BaseHttpLink.js +1 -7
- package/link/http/BaseHttpLink.js.map +1 -1
- package/link/http/HttpLink.d.ts +4 -0
- package/link/http/HttpLink.js +4 -0
- package/link/http/HttpLink.js.map +1 -1
- package/link/http/parseAndCheckHttpResponse.js +45 -57
- package/link/http/parseAndCheckHttpResponse.js.map +1 -1
- package/link/index.d.ts +1 -1
- package/link/index.js.map +1 -1
- package/link/persisted-queries/index.d.ts +19 -19
- package/link/persisted-queries/index.js +156 -141
- package/link/persisted-queries/index.js.map +1 -1
- package/link/remove-typename/index.d.ts +1 -1
- package/link/remove-typename/index.js +1 -1
- package/link/remove-typename/index.js.map +1 -1
- package/link/remove-typename/removeTypenameFromVariables.d.ts +8 -9
- package/link/remove-typename/removeTypenameFromVariables.js +32 -22
- package/link/remove-typename/removeTypenameFromVariables.js.map +1 -1
- package/local-state/LocalState.d.ts +3 -4
- package/local-state/LocalState.js.map +1 -1
- package/masking/types.d.ts +6 -5
- package/package.json +9 -2
- package/react/hooks/useApolloClient.js +14 -1
- package/react/hooks/useApolloClient.js.map +1 -1
- package/react/hooks/useBackgroundQuery.js +7 -6
- package/react/hooks/useBackgroundQuery.js.map +1 -1
- package/react/hooks/useFragment.js +12 -11
- package/react/hooks/useFragment.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts +2 -7
- package/react/hooks/useLazyQuery.js +15 -15
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useLoadableQuery.js +229 -69
- package/react/hooks/useLoadableQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts +2 -2
- package/react/hooks/useMutation.js +191 -81
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +3 -8
- package/react/hooks/useQuery.js +243 -70
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useQueryRefHandlers.js +64 -23
- package/react/hooks/useQueryRefHandlers.js.map +1 -1
- package/react/hooks/useReadQuery.js +3 -2
- package/react/hooks/useReadQuery.js.map +1 -1
- package/react/hooks/useSubscription.js +4 -4
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSuspenseFragment.js +1 -0
- package/react/hooks/useSuspenseFragment.js.map +1 -1
- package/react/hooks/useSuspenseQuery.js +59 -33
- package/react/hooks/useSuspenseQuery.js.map +1 -1
- package/react/internal/cache/QueryReference.d.ts +2 -3
- package/react/internal/cache/QueryReference.js +4 -4
- package/react/internal/cache/QueryReference.js.map +1 -1
- package/react/internal/types.d.ts +2 -7
- package/utilities/graphql/isFormattedExecutionResult.d.ts +3 -0
- package/utilities/graphql/isFormattedExecutionResult.js +6 -0
- package/utilities/graphql/isFormattedExecutionResult.js.map +1 -0
- package/utilities/index.d.ts +1 -0
- package/utilities/index.js +1 -0
- package/utilities/index.js.map +1 -1
- package/utilities/internal/getGraphQLErrorsFromResult.d.ts +4 -2
- package/utilities/internal/getGraphQLErrorsFromResult.js +1 -12
- package/utilities/internal/getGraphQLErrorsFromResult.js.map +1 -1
- package/utilities/internal/graphQLResultHasError.d.ts +2 -2
- package/utilities/internal/graphQLResultHasError.js +1 -4
- package/utilities/internal/graphQLResultHasError.js.map +1 -1
- package/utilities/internal/index.d.ts +0 -4
- package/utilities/internal/index.js +0 -4
- package/utilities/internal/index.js.map +1 -1
- package/version.d.ts +1 -0
- package/version.js +2 -1
- package/version.js.map +1 -1
- package/__cjs/utilities/internal/isExecutionPatchIncrementalResult.cjs +0 -12
- package/__cjs/utilities/internal/isExecutionPatchIncrementalResult.cjs.map +0 -1
- package/__cjs/utilities/internal/isExecutionPatchIncrementalResult.d.cts +0 -8
- package/__cjs/utilities/internal/isExecutionPatchIninitialResult.cjs +0 -12
- package/__cjs/utilities/internal/isExecutionPatchIninitialResult.cjs.map +0 -1
- package/__cjs/utilities/internal/isExecutionPatchIninitialResult.d.cts +0 -8
- package/__cjs/utilities/internal/isExecutionPatchResult.cjs +0 -15
- package/__cjs/utilities/internal/isExecutionPatchResult.cjs.map +0 -1
- package/__cjs/utilities/internal/isExecutionPatchResult.d.cts +0 -8
- package/__cjs/utilities/internal/mergeIncrementalData.cjs +0 -30
- package/__cjs/utilities/internal/mergeIncrementalData.cjs.map +0 -1
- package/__cjs/utilities/internal/mergeIncrementalData.d.cts +0 -8
- package/utilities/internal/isExecutionPatchIncrementalResult.d.ts +0 -8
- package/utilities/internal/isExecutionPatchIncrementalResult.js +0 -9
- package/utilities/internal/isExecutionPatchIncrementalResult.js.map +0 -1
- package/utilities/internal/isExecutionPatchIninitialResult.d.ts +0 -8
- package/utilities/internal/isExecutionPatchIninitialResult.js +0 -9
- package/utilities/internal/isExecutionPatchIninitialResult.js.map +0 -1
- package/utilities/internal/isExecutionPatchResult.d.ts +0 -8
- package/utilities/internal/isExecutionPatchResult.js +0 -12
- package/utilities/internal/isExecutionPatchResult.js.map +0 -1
- package/utilities/internal/mergeIncrementalData.d.ts +0 -8
- package/utilities/internal/mergeIncrementalData.js +0 -27
- package/utilities/internal/mergeIncrementalData.js.map +0 -1
package/core/QueryInfo.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { DocumentNode } from "graphql";
|
|
1
|
+
import type { DocumentNode, FormattedExecutionResult } from "graphql";
|
|
2
2
|
import type { ApolloCache } from "@apollo/client/cache";
|
|
3
3
|
import type { FetchResult } from "@apollo/client/link";
|
|
4
|
+
import type { Unmasked } from "@apollo/client/masking";
|
|
4
5
|
import type { ObservableQuery } from "./ObservableQuery.js";
|
|
5
6
|
import type { QueryManager } from "./QueryManager.js";
|
|
6
|
-
import type { DefaultContext, InternalRefetchQueriesInclude, MutationUpdaterFunction, OnQueryUpdated, OperationVariables, TypedDocumentNode } from "./types.js";
|
|
7
|
+
import type { DefaultContext, InternalRefetchQueriesInclude, MutationUpdaterFunction, NormalizedExecutionResult, OnQueryUpdated, OperationVariables, Streaming, TypedDocumentNode } from "./types.js";
|
|
7
8
|
import type { ErrorPolicy, MutationOptions, WatchQueryOptions } from "./watchQueryOptions.js";
|
|
8
9
|
type UpdateQueries<TData> = MutationOptions<TData, any, any>["updateQueries"];
|
|
9
10
|
export declare const enum CacheWriteBehavior {
|
|
@@ -12,7 +13,7 @@ export declare const enum CacheWriteBehavior {
|
|
|
12
13
|
MERGE = 2
|
|
13
14
|
}
|
|
14
15
|
interface LastWrite {
|
|
15
|
-
result:
|
|
16
|
+
result: FormattedExecutionResult<any>;
|
|
16
17
|
variables: WatchQueryOptions["variables"];
|
|
17
18
|
dmCount: number | undefined;
|
|
18
19
|
}
|
|
@@ -22,12 +23,13 @@ interface OperationInfo<TData, TVariables extends OperationVariables, AllowedCac
|
|
|
22
23
|
errorPolicy: ErrorPolicy;
|
|
23
24
|
cacheWriteBehavior: AllowedCacheWriteBehavior;
|
|
24
25
|
}
|
|
25
|
-
export declare class QueryInfo {
|
|
26
|
+
export declare class QueryInfo<TData, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache> {
|
|
26
27
|
lastRequestId: number;
|
|
27
28
|
private cache;
|
|
28
29
|
private queryManager;
|
|
29
30
|
readonly id: string;
|
|
30
31
|
private readonly observableQuery?;
|
|
32
|
+
private incremental?;
|
|
31
33
|
constructor(queryManager: QueryManager, observableQuery?: ObservableQuery<any, any>);
|
|
32
34
|
/**
|
|
33
35
|
* @internal
|
|
@@ -41,25 +43,26 @@ export declare class QueryInfo {
|
|
|
41
43
|
private set lastWrite(value);
|
|
42
44
|
resetLastWrite(): void;
|
|
43
45
|
private shouldWrite;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
get hasNext(): boolean;
|
|
47
|
+
private maybeHandleIncrementalResult;
|
|
48
|
+
markQueryResult(incoming: FetchResult<TData>, { document: query, variables, errorPolicy, cacheWriteBehavior, }: OperationInfo<TData, TVariables>): FormattedExecutionResult<TData | Streaming<TData>>;
|
|
49
|
+
markMutationResult(incoming: FetchResult<TData>, mutation: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE> & {
|
|
47
50
|
context?: DefaultContext;
|
|
48
51
|
updateQueries: UpdateQueries<TData>;
|
|
49
52
|
update?: MutationUpdaterFunction<TData, TVariables, TCache>;
|
|
50
53
|
awaitRefetchQueries?: boolean;
|
|
51
|
-
refetchQueries?: InternalRefetchQueriesInclude;
|
|
54
|
+
refetchQueries?: ((result: NormalizedExecutionResult<Unmasked<TData>>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
|
|
52
55
|
removeOptimistic?: string;
|
|
53
56
|
onQueryUpdated?: OnQueryUpdated<any>;
|
|
54
57
|
keepRootFields?: boolean;
|
|
55
|
-
}, cache?:
|
|
56
|
-
markMutationOptimistic
|
|
58
|
+
}, cache?: TCache): Promise<FormattedExecutionResult<TData | Streaming<TData>>>;
|
|
59
|
+
markMutationOptimistic(optimisticResponse: any, mutation: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE> & {
|
|
57
60
|
context?: DefaultContext;
|
|
58
61
|
updateQueries: UpdateQueries<TData>;
|
|
59
62
|
update?: MutationUpdaterFunction<TData, TVariables, TCache>;
|
|
60
63
|
keepRootFields?: boolean;
|
|
61
64
|
}): boolean;
|
|
62
|
-
markSubscriptionResult
|
|
65
|
+
markSubscriptionResult(result: FormattedExecutionResult<TData>, { document, variables, errorPolicy, cacheWriteBehavior, }: OperationInfo<TData, TVariables, CacheWriteBehavior.FORBID | CacheWriteBehavior.MERGE>): void;
|
|
63
66
|
}
|
|
64
67
|
export {};
|
|
65
68
|
//# sourceMappingURL=QueryInfo.d.ts.map
|
package/core/QueryInfo.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
2
2
|
import { equal } from "@wry/equality";
|
|
3
|
-
import {
|
|
3
|
+
import { getOperationName, graphQLResultHasError, } from "@apollo/client/utilities/internal";
|
|
4
4
|
import { invariant } from "@apollo/client/utilities/invariant";
|
|
5
5
|
const IGNORE = {};
|
|
6
6
|
const destructiveMethodCounts = new WeakMap();
|
|
@@ -33,6 +33,7 @@ export class QueryInfo {
|
|
|
33
33
|
queryManager;
|
|
34
34
|
id;
|
|
35
35
|
observableQuery;
|
|
36
|
+
incremental;
|
|
36
37
|
constructor(queryManager, observableQuery) {
|
|
37
38
|
const cache = (this.cache = queryManager.cache);
|
|
38
39
|
const id = (queryInfoIds.get(queryManager) || 0) + 1;
|
|
@@ -79,8 +80,20 @@ export class QueryInfo {
|
|
|
79
80
|
equal(variables, lastWrite.variables) &&
|
|
80
81
|
equal(result.data, lastWrite.result.data));
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
get hasNext() {
|
|
84
|
+
return this.incremental ? this.incremental.hasNext : false;
|
|
85
|
+
}
|
|
86
|
+
maybeHandleIncrementalResult(cacheData, incoming, query) {
|
|
87
|
+
const { incrementalHandler } = this.queryManager;
|
|
88
|
+
if (incrementalHandler.isIncrementalResult(incoming)) {
|
|
89
|
+
this.incremental ||= incrementalHandler.startRequest({
|
|
90
|
+
query,
|
|
91
|
+
});
|
|
92
|
+
return this.incremental.handle(cacheData, incoming);
|
|
93
|
+
}
|
|
94
|
+
return incoming;
|
|
95
|
+
}
|
|
96
|
+
markQueryResult(incoming, { document: query, variables, errorPolicy, cacheWriteBehavior, }) {
|
|
84
97
|
const diffOptions = {
|
|
85
98
|
query,
|
|
86
99
|
variables,
|
|
@@ -90,147 +103,130 @@ export class QueryInfo {
|
|
|
90
103
|
// Cancel the pending notify timeout (if it exists) to prevent extraneous network
|
|
91
104
|
// requests. To allow future notify timeouts, diff and dirty are reset as well.
|
|
92
105
|
this.observableQuery?.["resetNotifications"]();
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.lastDiff = {
|
|
99
|
-
diff: { result: result.data, complete: true },
|
|
100
|
-
options: diffOptions,
|
|
101
|
-
};
|
|
106
|
+
const skipCache = cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
|
|
107
|
+
const lastDiff = skipCache ? undefined : this.cache.diff(diffOptions);
|
|
108
|
+
let result = this.maybeHandleIncrementalResult(lastDiff?.result, incoming, query);
|
|
109
|
+
if (skipCache) {
|
|
110
|
+
return result;
|
|
102
111
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// received, when possible.
|
|
171
|
-
result.data = lastDiff.result;
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
// If the previous this.diff was incomplete, fall through to
|
|
175
|
-
// re-reading the latest data with cache.diff, below.
|
|
176
|
-
}
|
|
177
|
-
const diff = cache.diff(diffOptions);
|
|
178
|
-
// If we're allowed to write to the cache, and we can read a
|
|
179
|
-
// complete result from the cache, update result.data to be the
|
|
180
|
-
// result from the cache, rather than the raw network result.
|
|
181
|
-
// Set without setDiff to avoid triggering a notify call, since
|
|
182
|
-
// we have other ways of notifying for this result.
|
|
183
|
-
if (diff.complete) {
|
|
184
|
-
result.data = diff.result;
|
|
112
|
+
if (shouldWriteResult(result, errorPolicy)) {
|
|
113
|
+
// Using a transaction here so we have a chance to read the result
|
|
114
|
+
// back from the cache before the watch callback fires as a result
|
|
115
|
+
// of writeQuery, so we can store the new diff quietly and ignore
|
|
116
|
+
// it when we receive it redundantly from the watch callback.
|
|
117
|
+
this.cache.batch({
|
|
118
|
+
onWatchUpdated: (
|
|
119
|
+
// all additional options on ObservableQuery.CacheWatchOptions are
|
|
120
|
+
// optional so we can use the type here
|
|
121
|
+
watch, diff) => {
|
|
122
|
+
if (watch.watcher === this.observableQuery) {
|
|
123
|
+
// see comment on `lastOwnDiff` for explanation
|
|
124
|
+
watch.lastOwnDiff = diff;
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
update: (cache) => {
|
|
128
|
+
if (this.shouldWrite(result, variables)) {
|
|
129
|
+
cache.writeQuery({
|
|
130
|
+
query,
|
|
131
|
+
data: result.data,
|
|
132
|
+
variables,
|
|
133
|
+
overwrite: cacheWriteBehavior === 1 /* CacheWriteBehavior.OVERWRITE */,
|
|
134
|
+
});
|
|
135
|
+
this.lastWrite = {
|
|
136
|
+
result,
|
|
137
|
+
variables,
|
|
138
|
+
dmCount: destructiveMethodCounts.get(this.cache),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
// If result is the same as the last result we received from
|
|
143
|
+
// the network (and the variables match too), avoid writing
|
|
144
|
+
// result into the cache again. The wisdom of skipping this
|
|
145
|
+
// cache write is far from obvious, since any cache write
|
|
146
|
+
// could be the one that puts the cache back into a desired
|
|
147
|
+
// state, fixing corruption or missing data. However, if we
|
|
148
|
+
// always write every network result into the cache, we enable
|
|
149
|
+
// feuds between queries competing to update the same data in
|
|
150
|
+
// incompatible ways, which can lead to an endless cycle of
|
|
151
|
+
// cache broadcasts and useless network requests. As with any
|
|
152
|
+
// feud, eventually one side must step back from the brink,
|
|
153
|
+
// letting the other side(s) have the last word(s). There may
|
|
154
|
+
// be other points where we could break this cycle, such as
|
|
155
|
+
// silencing the broadcast for cache.writeQuery (not a good
|
|
156
|
+
// idea, since it just delays the feud a bit) or somehow
|
|
157
|
+
// avoiding the network request that just happened (also bad,
|
|
158
|
+
// because the server could return useful new data). All
|
|
159
|
+
// options considered, skipping this cache write seems to be
|
|
160
|
+
// the least damaging place to break the cycle, because it
|
|
161
|
+
// reflects the intuition that we recently wrote this exact
|
|
162
|
+
// result into the cache, so the cache *should* already/still
|
|
163
|
+
// contain this data. If some other query has clobbered that
|
|
164
|
+
// data in the meantime, that's too bad, but there will be no
|
|
165
|
+
// winners if every query blindly reverts to its own version
|
|
166
|
+
// of the data. This approach also gives the network a chance
|
|
167
|
+
// to return new data, which will be written into the cache as
|
|
168
|
+
// usual, notifying only those queries that are directly
|
|
169
|
+
// affected by the cache updates, as usual. In the future, an
|
|
170
|
+
// even more sophisticated cache could perhaps prevent or
|
|
171
|
+
// mitigate the clobbering somehow, but that would make this
|
|
172
|
+
// particular cache write even less important, and thus
|
|
173
|
+
// skipping it would be even safer than it is today.
|
|
174
|
+
if (lastDiff && lastDiff.complete) {
|
|
175
|
+
// Reuse data from the last good (complete) diff that we
|
|
176
|
+
// received, when possible.
|
|
177
|
+
result = { ...result, data: lastDiff.result };
|
|
178
|
+
return;
|
|
185
179
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
180
|
+
// If the previous this.diff was incomplete, fall through to
|
|
181
|
+
// re-reading the latest data with cache.diff, below.
|
|
182
|
+
}
|
|
183
|
+
const diff = cache.diff(diffOptions);
|
|
184
|
+
// If we're allowed to write to the cache, and we can read a
|
|
185
|
+
// complete result from the cache, update result.data to be the
|
|
186
|
+
// result from the cache, rather than the raw network result.
|
|
187
|
+
// Set without setDiff to avoid triggering a notify call, since
|
|
188
|
+
// we have other ways of notifying for this result.
|
|
189
|
+
if (diff.complete) {
|
|
190
|
+
result = { ...result, data: diff.result };
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
});
|
|
192
194
|
}
|
|
195
|
+
else {
|
|
196
|
+
this.lastWrite = void 0;
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
193
199
|
}
|
|
194
|
-
markMutationResult(
|
|
200
|
+
markMutationResult(incoming, mutation, cache = this.cache) {
|
|
195
201
|
const cacheWrites = [];
|
|
196
202
|
const skipCache = mutation.cacheWriteBehavior === 0 /* CacheWriteBehavior.FORBID */;
|
|
203
|
+
let result = this.maybeHandleIncrementalResult(skipCache ? undefined : (cache.diff({
|
|
204
|
+
id: "ROOT_MUTATION",
|
|
205
|
+
// The cache complains if passed a mutation where it expects a
|
|
206
|
+
// query, so we transform mutations and subscriptions to queries
|
|
207
|
+
// (only once, thanks to this.transformCache).
|
|
208
|
+
query: this.queryManager.getDocumentInfo(mutation.document).asQuery,
|
|
209
|
+
variables: mutation.variables,
|
|
210
|
+
optimistic: false,
|
|
211
|
+
returnPartialData: true,
|
|
212
|
+
}).result), incoming, mutation.document);
|
|
213
|
+
if (mutation.errorPolicy === "ignore") {
|
|
214
|
+
result = { ...result, errors: [] };
|
|
215
|
+
}
|
|
216
|
+
if (graphQLResultHasError(result) && mutation.errorPolicy === "none") {
|
|
217
|
+
return Promise.resolve(result);
|
|
218
|
+
}
|
|
219
|
+
const getResultWithDataState = () => ({
|
|
220
|
+
...result,
|
|
221
|
+
dataState: this.hasNext ? "streaming" : "complete",
|
|
222
|
+
});
|
|
197
223
|
if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
}
|
|
224
|
+
cacheWrites.push({
|
|
225
|
+
result: result.data,
|
|
226
|
+
dataId: "ROOT_MUTATION",
|
|
227
|
+
query: mutation.document,
|
|
228
|
+
variables: mutation.variables,
|
|
229
|
+
});
|
|
234
230
|
const { updateQueries } = mutation;
|
|
235
231
|
if (updateQueries) {
|
|
236
232
|
this.queryManager
|
|
@@ -248,7 +244,7 @@ export class QueryInfo {
|
|
|
248
244
|
if (complete && currentQueryResult) {
|
|
249
245
|
// Run our reducer using the current query result and the mutation result.
|
|
250
246
|
const nextQueryResult = updater(currentQueryResult, {
|
|
251
|
-
mutationResult:
|
|
247
|
+
mutationResult: getResultWithDataState(),
|
|
252
248
|
queryName: (document && getOperationName(document)) || void 0,
|
|
253
249
|
queryVariables: variables,
|
|
254
250
|
});
|
|
@@ -265,8 +261,12 @@ export class QueryInfo {
|
|
|
265
261
|
});
|
|
266
262
|
}
|
|
267
263
|
}
|
|
264
|
+
let refetchQueries = mutation.refetchQueries;
|
|
265
|
+
if (typeof refetchQueries === "function") {
|
|
266
|
+
refetchQueries = refetchQueries(getResultWithDataState());
|
|
267
|
+
}
|
|
268
268
|
if (cacheWrites.length > 0 ||
|
|
269
|
-
(
|
|
269
|
+
(refetchQueries || "").length > 0 ||
|
|
270
270
|
mutation.update ||
|
|
271
271
|
mutation.onQueryUpdated ||
|
|
272
272
|
mutation.removeOptimistic) {
|
|
@@ -283,8 +283,6 @@ export class QueryInfo {
|
|
|
283
283
|
const { update } = mutation;
|
|
284
284
|
// Determine whether result is a SingleExecutionResult,
|
|
285
285
|
// or the final ExecutionPatchResult.
|
|
286
|
-
const isFinalResult = !isExecutionPatchResult(result) ||
|
|
287
|
-
(isExecutionPatchIncrementalResult(result) && !result.hasNext);
|
|
288
286
|
if (update) {
|
|
289
287
|
if (!skipCache) {
|
|
290
288
|
// Re-read the ROOT_MUTATION data we just wrote into the cache
|
|
@@ -303,19 +301,14 @@ export class QueryInfo {
|
|
|
303
301
|
returnPartialData: true,
|
|
304
302
|
});
|
|
305
303
|
if (diff.complete) {
|
|
306
|
-
result = {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
if ("hasNext" in result) {
|
|
311
|
-
delete result.hasNext;
|
|
312
|
-
}
|
|
304
|
+
result = {
|
|
305
|
+
...result,
|
|
306
|
+
data: diff.result,
|
|
307
|
+
};
|
|
313
308
|
}
|
|
314
309
|
}
|
|
315
|
-
// If we've received the whole response,
|
|
316
|
-
|
|
317
|
-
// call the update function.
|
|
318
|
-
if (isFinalResult) {
|
|
310
|
+
// If we've received the whole response, call the update function.
|
|
311
|
+
if (!this.hasNext) {
|
|
319
312
|
update(cache, result, {
|
|
320
313
|
context: mutation.context,
|
|
321
314
|
variables: mutation.variables,
|
|
@@ -324,7 +317,7 @@ export class QueryInfo {
|
|
|
324
317
|
}
|
|
325
318
|
// TODO Do this with cache.evict({ id: 'ROOT_MUTATION' }) but make it
|
|
326
319
|
// shallow to allow rolling back optimistic evictions.
|
|
327
|
-
if (!skipCache && !mutation.keepRootFields &&
|
|
320
|
+
if (!skipCache && !mutation.keepRootFields && !this.hasNext) {
|
|
328
321
|
cache.modify({
|
|
329
322
|
id: "ROOT_MUTATION",
|
|
330
323
|
fields(value, { fieldName, DELETE }) {
|
|
@@ -333,7 +326,7 @@ export class QueryInfo {
|
|
|
333
326
|
});
|
|
334
327
|
}
|
|
335
328
|
},
|
|
336
|
-
include:
|
|
329
|
+
include: refetchQueries,
|
|
337
330
|
// Write the final mutation.result to the root layer of the cache.
|
|
338
331
|
optimistic: false,
|
|
339
332
|
// Remove the corresponding optimistic layer at the same time as we
|
|
@@ -386,21 +379,6 @@ export class QueryInfo {
|
|
|
386
379
|
}
|
|
387
380
|
}
|
|
388
381
|
}
|
|
389
|
-
function handleIncrementalResult(result, lastDiff) {
|
|
390
|
-
if ("incremental" in result && isNonEmptyArray(result.incremental)) {
|
|
391
|
-
const mergedData = mergeIncrementalData(lastDiff.result, result);
|
|
392
|
-
result.data = mergedData;
|
|
393
|
-
// Detect the first chunk of a deferred query and merge it with existing
|
|
394
|
-
// cache data. This ensures a `cache-first` fetch policy that returns
|
|
395
|
-
// partial cache data or a `cache-and-network` fetch policy that already
|
|
396
|
-
// has full data in the cache does not complain when trying to merge the
|
|
397
|
-
// initial deferred server data with existing cache data.
|
|
398
|
-
}
|
|
399
|
-
else if ("hasNext" in result && result.hasNext) {
|
|
400
|
-
const merger = new DeepMerger();
|
|
401
|
-
result.data = merger.merge(lastDiff.result, result.data);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
382
|
function shouldWriteResult(result, errorPolicy = "none") {
|
|
405
383
|
const ignoreErrors = errorPolicy === "ignore" || errorPolicy === "all";
|
|
406
384
|
let writeWithErrors = !graphQLResultHasError(result);
|