Skip to content

Commit 3de63eb

Browse files
authored
Unify error behavior of client.mutate and update its type (#12475)
1 parent 7e8380b commit 3de63eb

22 files changed

+782
-433
lines changed

.api-reports/api-report-core.api.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class ApolloClient implements DataProxy_2 {
155155
getResolvers(): Resolvers;
156156
// (undocumented)
157157
link: ApolloLink_2;
158-
mutate<TData = unknown, TVariables extends OperationVariables = OperationVariables, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache_2 = ApolloCache_2>(options: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult_2<MaybeMasked_2<TData>>>;
158+
mutate<TData = unknown, TVariables extends OperationVariables = OperationVariables, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache_2 = ApolloCache_2>(options: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked_2<TData>>>;
159159
onClearStore(cb: () => Promise<any>): () => void;
160160
onResetStore(cb: () => Promise<any>): () => void;
161161
set prioritizeCacheValues(value: boolean);
@@ -1376,6 +1376,13 @@ type Modifiers<T extends Record<string, any> = Record<string, unknown>> = Partia
13761376
[FieldName in keyof T]: Modifier<StoreObjectValueMaybeReference<Exclude<T[FieldName], undefined>>>;
13771377
}>;
13781378

1379+
// @public (undocumented)
1380+
export interface MutateResult<TData = unknown> {
1381+
data: TData | undefined;
1382+
error?: ErrorLike;
1383+
extensions?: Record<string, unknown>;
1384+
}
1385+
13791386
// @public (undocumented)
13801387
export type MutationFetchPolicy = Extract<FetchPolicy, "network-only" | "no-cache">;
13811388

@@ -1797,7 +1804,7 @@ class QueryManager {
17971804
// (undocumented)
17981805
maskOperation<TData = unknown>(options: MaskOperationOptions<TData>): MaybeMasked_2<TData>;
17991806
// (undocumented)
1800-
mutate<TData, TVariables extends OperationVariables, TContext extends Record<string, any>, TCache extends ApolloCache_2>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult_2<MaybeMasked_2<TData>>>;
1807+
mutate<TData, TVariables extends OperationVariables, TContext extends Record<string, any>, TCache extends ApolloCache_2>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked_2<TData>>>;
18011808
// (undocumented)
18021809
mutationStore?: {
18031810
[mutationId: string]: MutationStoreValue;
@@ -2321,8 +2328,8 @@ interface WriteContext extends ReadMergeModifyContext {
23212328
// src/cache/inmemory/types.ts:133:3 - (ae-forgotten-export) The symbol "KeyFieldsFunction" needs to be exported by the entry point index.d.ts
23222329
// src/core/ObservableQuery.ts:128:5 - (ae-forgotten-export) The symbol "QueryManager" needs to be exported by the entry point index.d.ts
23232330
// src/core/ObservableQuery.ts:129:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
2324-
// src/core/QueryManager.ts:184:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
2325-
// src/core/QueryManager.ts:438:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
2331+
// src/core/QueryManager.ts:185:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
2332+
// src/core/QueryManager.ts:456:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
23262333
// src/link/http/selectHttpOptionsAndBody.ts:128:1 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts
23272334

23282335
// (No @packageDocumentation comment for this package)

.api-reports/api-report-react.api.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type { IsStrictlyAny } from '@apollo/client/utilities';
3737
import type { MaybeMasked } from '@apollo/client/masking';
3838
import type { MaybeMasked as MaybeMasked_2 } from '@apollo/client/core';
3939
import type { MissingTree } from '@apollo/client/cache';
40+
import type { MutateResult as MutateResult_2 } from '@apollo/client/core';
4041
import type { MutationFetchPolicy as MutationFetchPolicy_2 } from '@apollo/client/core';
4142
import type { MutationQueryReducersMap as MutationQueryReducersMap_2 } from '@apollo/client/core';
4243
import type { MutationUpdaterFunction as MutationUpdaterFunction_2 } from '@apollo/client/core';
@@ -120,7 +121,8 @@ class ApolloClient_2 implements DataProxy {
120121
link: ApolloLink;
121122
// Warning: (ae-forgotten-export) The symbol "DefaultContext_2" needs to be exported by the entry point index.d.ts
122123
// Warning: (ae-forgotten-export) The symbol "MutationOptions" needs to be exported by the entry point index.d.ts
123-
mutate<TData = unknown, TVariables extends OperationVariables_2 = OperationVariables_2, TContext extends Record<string, any> = DefaultContext_2, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
124+
// Warning: (ae-forgotten-export) The symbol "MutateResult" needs to be exported by the entry point index.d.ts
125+
mutate<TData = unknown, TVariables extends OperationVariables_2 = OperationVariables_2, TContext extends Record<string, any> = DefaultContext_2, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
124126
onClearStore(cb: () => Promise<any>): () => void;
125127
onResetStore(cb: () => Promise<any>): () => void;
126128
set prioritizeCacheValues(value: boolean);
@@ -463,6 +465,13 @@ interface MaskOperationOptions<TData> {
463465
id: string;
464466
}
465467

468+
// @public (undocumented)
469+
interface MutateResult<TData = unknown> {
470+
data: TData | undefined;
471+
error?: ErrorLike;
472+
extensions?: Record<string, unknown>;
473+
}
474+
466475
// Warning: (ae-forgotten-export) The symbol "FetchPolicy" needs to be exported by the entry point index.d.ts
467476
//
468477
// @public (undocumented)
@@ -839,7 +848,7 @@ class QueryManager {
839848
// (undocumented)
840849
maskOperation<TData = unknown>(options: MaskOperationOptions<TData>): MaybeMasked<TData>;
841850
// (undocumented)
842-
mutate<TData, TVariables extends OperationVariables_2, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
851+
mutate<TData, TVariables extends OperationVariables_2, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
843852
// (undocumented)
844853
mutationStore?: {
845854
[mutationId: string]: MutationStoreValue;
@@ -1353,14 +1362,14 @@ export namespace useMutation {
13531362
export interface Result<TData = unknown> {
13541363
called: boolean;
13551364
client: ApolloClient;
1356-
data?: MaybeMasked_2<TData> | null;
1357-
error?: ErrorLike_2;
1365+
data: MaybeMasked_2<TData> | null | undefined;
1366+
error: ErrorLike_2 | undefined;
13581367
loading: boolean;
13591368
reset: () => void;
13601369
}
13611370
// (undocumented)
13621371
export type ResultTuple<TData, TVariables, TContext = DefaultContext, TCache extends ApolloCache_2 = ApolloCache_2> = [
1363-
mutate: (options?: MutationFunctionOptions<TData, TVariables, TContext, TCache>) => Promise<FetchResult_2<MaybeMasked_2<TData>>>,
1372+
mutate: (options?: MutationFunctionOptions<TData, TVariables, TContext, TCache>) => Promise<MutateResult_2<MaybeMasked_2<TData>>>,
13641373
result: Result<TData>
13651374
];
13661375
}
@@ -1646,8 +1655,8 @@ interface WatchQueryOptions_2<TVariables extends OperationVariables_2 = Operatio
16461655
// src/core/LocalState.ts:71:3 - (ae-forgotten-export) The symbol "ApolloClient_2" needs to be exported by the entry point index.d.ts
16471656
// src/core/ObservableQuery.ts:128:5 - (ae-forgotten-export) The symbol "QueryManager" needs to be exported by the entry point index.d.ts
16481657
// src/core/ObservableQuery.ts:129:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
1649-
// src/core/QueryManager.ts:184:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
1650-
// src/core/QueryManager.ts:438:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
1658+
// src/core/QueryManager.ts:185:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
1659+
// src/core/QueryManager.ts:456:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
16511660
// src/core/types.ts:204:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
16521661
// src/core/types.ts:233:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
16531662
// src/core/watchQueryOptions.ts:195:3 - (ae-forgotten-export) The symbol "UpdateQueryOptions" needs to be exported by the entry point index.d.ts

.api-reports/api-report-react_hooks.api.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type { IsStrictlyAny } from '@apollo/client/utilities';
3636
import type { MaybeMasked } from '@apollo/client/masking';
3737
import type { MaybeMasked as MaybeMasked_2 } from '@apollo/client/core';
3838
import type { MissingTree } from '@apollo/client/cache';
39+
import type { MutateResult as MutateResult_2 } from '@apollo/client/core';
3940
import type { MutationFetchPolicy as MutationFetchPolicy_2 } from '@apollo/client/core';
4041
import type { MutationQueryReducersMap as MutationQueryReducersMap_2 } from '@apollo/client/core';
4142
import type { MutationUpdaterFunction as MutationUpdaterFunction_2 } from '@apollo/client/core';
@@ -104,7 +105,8 @@ class ApolloClient_2 implements DataProxy {
104105
link: ApolloLink;
105106
// Warning: (ae-forgotten-export) The symbol "DefaultContext" needs to be exported by the entry point index.d.ts
106107
// Warning: (ae-forgotten-export) The symbol "MutationOptions" needs to be exported by the entry point index.d.ts
107-
mutate<TData = unknown, TVariables extends OperationVariables_2 = OperationVariables_2, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
108+
// Warning: (ae-forgotten-export) The symbol "MutateResult" needs to be exported by the entry point index.d.ts
109+
mutate<TData = unknown, TVariables extends OperationVariables_2 = OperationVariables_2, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
108110
onClearStore(cb: () => Promise<any>): () => void;
109111
onResetStore(cb: () => Promise<any>): () => void;
110112
set prioritizeCacheValues(value: boolean);
@@ -358,6 +360,13 @@ interface MaskOperationOptions<TData> {
358360
id: string;
359361
}
360362

363+
// @public (undocumented)
364+
interface MutateResult<TData = unknown> {
365+
data: TData | undefined;
366+
error?: ErrorLike;
367+
extensions?: Record<string, unknown>;
368+
}
369+
361370
// Warning: (ae-forgotten-export) The symbol "FetchPolicy" needs to be exported by the entry point index.d.ts
362371
//
363372
// @public (undocumented)
@@ -655,7 +664,7 @@ class QueryManager {
655664
// (undocumented)
656665
maskOperation<TData = unknown>(options: MaskOperationOptions<TData>): MaybeMasked<TData>;
657666
// (undocumented)
658-
mutate<TData, TVariables extends OperationVariables_2, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
667+
mutate<TData, TVariables extends OperationVariables_2, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
659668
// (undocumented)
660669
mutationStore?: {
661670
[mutationId: string]: MutationStoreValue;
@@ -1130,14 +1139,14 @@ export namespace useMutation {
11301139
export interface Result<TData = unknown> {
11311140
called: boolean;
11321141
client: ApolloClient;
1133-
data?: MaybeMasked_2<TData> | null;
1134-
error?: ErrorLike_2;
1142+
data: MaybeMasked_2<TData> | null | undefined;
1143+
error: ErrorLike_2 | undefined;
11351144
loading: boolean;
11361145
reset: () => void;
11371146
}
11381147
// (undocumented)
11391148
export type ResultTuple<TData, TVariables, TContext = DefaultContext_2, TCache extends ApolloCache_2 = ApolloCache_2> = [
1140-
mutate: (options?: MutationFunctionOptions<TData, TVariables, TContext, TCache>) => Promise<FetchResult_2<MaybeMasked_2<TData>>>,
1149+
mutate: (options?: MutationFunctionOptions<TData, TVariables, TContext, TCache>) => Promise<MutateResult_2<MaybeMasked_2<TData>>>,
11411150
result: Result<TData>
11421151
];
11431152
}
@@ -1408,8 +1417,8 @@ interface WatchQueryOptions_2<TVariables extends OperationVariables_2 = Operatio
14081417
// src/core/LocalState.ts:71:3 - (ae-forgotten-export) The symbol "ApolloClient_2" needs to be exported by the entry point index.d.ts
14091418
// src/core/ObservableQuery.ts:128:5 - (ae-forgotten-export) The symbol "QueryManager" needs to be exported by the entry point index.d.ts
14101419
// src/core/ObservableQuery.ts:129:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
1411-
// src/core/QueryManager.ts:184:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
1412-
// src/core/QueryManager.ts:438:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
1420+
// src/core/QueryManager.ts:185:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
1421+
// src/core/QueryManager.ts:456:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
14131422
// src/core/types.ts:204:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
14141423
// src/core/types.ts:233:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
14151424
// src/core/watchQueryOptions.ts:195:3 - (ae-forgotten-export) The symbol "UpdateQueryOptions" needs to be exported by the entry point index.d.ts

.api-reports/api-report.api.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class ApolloClient implements DataProxy {
119119
getResolvers(): Resolvers;
120120
// (undocumented)
121121
link: ApolloLink;
122-
mutate<TData = unknown, TVariables extends OperationVariables = OperationVariables, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
122+
mutate<TData = unknown, TVariables extends OperationVariables = OperationVariables, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache = ApolloCache>(options: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
123123
onClearStore(cb: () => Promise<any>): () => void;
124124
onResetStore(cb: () => Promise<any>): () => void;
125125
set prioritizeCacheValues(value: boolean);
@@ -1470,6 +1470,13 @@ type Modifiers<T extends Record<string, any> = Record<string, unknown>> = Partia
14701470
[FieldName in keyof T]: Modifier<StoreObjectValueMaybeReference<Exclude<T[FieldName], undefined>>>;
14711471
}>;
14721472

1473+
// @public (undocumented)
1474+
export interface MutateResult<TData = unknown> {
1475+
data: TData | undefined;
1476+
error?: ErrorLike;
1477+
extensions?: Record<string, unknown>;
1478+
}
1479+
14731480
// @public (undocumented)
14741481
export type MutationFetchPolicy = Extract<FetchPolicy, "network-only" | "no-cache">;
14751482

@@ -1913,7 +1920,7 @@ class QueryManager {
19131920
// (undocumented)
19141921
maskOperation<TData = unknown>(options: MaskOperationOptions<TData>): MaybeMasked<TData>;
19151922
// (undocumented)
1916-
mutate<TData, TVariables extends OperationVariables, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<MaybeMasked<TData>>>;
1923+
mutate<TData, TVariables extends OperationVariables, TContext extends Record<string, any>, TCache extends ApolloCache>({ mutation, variables, optimisticResponse, updateQueries, refetchQueries, awaitRefetchQueries, update: updateWithProxyFn, onQueryUpdated, fetchPolicy, errorPolicy, keepRootFields, context, }: MutationOptions<TData, TVariables, TContext>): Promise<MutateResult<MaybeMasked<TData>>>;
19171924
// (undocumented)
19181925
mutationStore?: {
19191926
[mutationId: string]: MutationStoreValue;
@@ -2463,8 +2470,8 @@ interface WriteContext extends ReadMergeModifyContext {
24632470
// src/cache/inmemory/types.ts:133:3 - (ae-forgotten-export) The symbol "KeyFieldsFunction" needs to be exported by the entry point index.d.ts
24642471
// src/core/ObservableQuery.ts:128:5 - (ae-forgotten-export) The symbol "QueryManager" needs to be exported by the entry point index.d.ts
24652472
// src/core/ObservableQuery.ts:129:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
2466-
// src/core/QueryManager.ts:184:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
2467-
// src/core/QueryManager.ts:438:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
2473+
// src/core/QueryManager.ts:185:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
2474+
// src/core/QueryManager.ts:456:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
24682475
// src/link/http/selectHttpOptionsAndBody.ts:128:1 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts
24692476

24702477
// (No @packageDocumentation comment for this package)

.changeset/grumpy-vans-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
Unify error behavior on mutations for GraphQL errors and network errors by ensuring network errors are subject to the `errorPolicy`. Network errors created when using an `errorPolicy` of `all` will now resolve the promise and be returned on the `error` property of the result, or stripped away when the `errorPolicy` is `none`.

.changeset/little-spoons-kick.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
`client.mutate` now returns a `MutateResult` instead of `FetchResult`. As a result, the `errors` property has been removed in favor of `error` which is set if either a network error occured or GraphQL errors are returned from the server.
6+
7+
`useMutation` now also returns a `MutateResult` instead of a `FetchResult`.

.changeset/many-buses-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fix an issue where passing `onError` to `useMutation` would resolve the promise returned by the `mutate` function instead of rejecting when using an `errorPolicy` of `none`.

.changeset/poor-eels-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
Mutations no longer report errors if the GraphQL result from the server contains an empty array of errors.

.changeset/tough-rockets-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fix an issue where additional response properties were returned on the result returned from `client.mutate`, such as `@defer` payload fields. These properties are now stripped out to correspond to the TypeScript type.

0 commit comments

Comments
 (0)