Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba5cc33

Browse files
authored
Don't callonError if errors are thrown inonCompleted (#12174)
1 parent8dfc90d commitba5cc33

File tree

5 files changed

+137
-67
lines changed

5 files changed

+137
-67
lines changed

‎.changeset/fluffy-worms-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client":minor
3+
---
4+
5+
Ensure errors thrown in the`onCompleted` callback from`useMutation` don't call`onError`.

‎.changeset/sharp-windows-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client":minor
3+
---
4+
5+
Reject the mutation promise if errors are thrown in the`onCompleted` callback of`useMutation`.

‎.size-limits.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs":41615,
2+
"dist/apollo-client.min.cjs":41613,
33
"import { ApolloClient, InMemoryCache, HttpLink } from\"dist/index.js\" (production)":34349
44
}

‎src/react/hooks/__tests__/useMutation.test.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,61 @@ describe("useMutation Hook", () => {
12071207
expect.objectContaining({ variables})
12081208
);
12091209
});
1210+
1211+
// https://github.com/apollographql/apollo-client/issues/12008
1212+
it("does not call onError if errors are thrown in the onCompleted callback",async()=>{
1213+
constCREATE_TODO_DATA={
1214+
createTodo:{
1215+
id:1,
1216+
priority:"Low",
1217+
description:"Get milk!",
1218+
__typename:"Todo",
1219+
},
1220+
};
1221+
1222+
constvariables={
1223+
priority:"Low",
1224+
description:"Get milk2.",
1225+
};
1226+
1227+
constmocks=[
1228+
{
1229+
request:{
1230+
query:CREATE_TODO_MUTATION,
1231+
variables,
1232+
},
1233+
result:{
1234+
data:CREATE_TODO_DATA,
1235+
},
1236+
},
1237+
];
1238+
1239+
constonError=jest.fn();
1240+
1241+
using_disabledAct=disableActEnvironment();
1242+
const{ takeSnapshot}=awaitrenderHookToSnapshotStream(
1243+
()=>
1244+
useMutation(CREATE_TODO_MUTATION,{
1245+
onCompleted:()=>{
1246+
thrownewError("Oops");
1247+
},
1248+
onError,
1249+
}),
1250+
{
1251+
wrapper:({ children})=>(
1252+
<MockedProvidermocks={mocks}>{children}</MockedProvider>
1253+
),
1254+
}
1255+
);
1256+
1257+
const[createTodo]=awaittakeSnapshot();
1258+
1259+
awaitexpect(createTodo({ variables})).rejects.toEqual(
1260+
newError("Oops")
1261+
);
1262+
1263+
expect(onError).not.toHaveBeenCalled();
1264+
});
12101265
});
12111266

12121267
describe("ROOT_MUTATION cache data",()=>{

‎src/react/hooks/useMutation.ts

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -138,82 +138,87 @@ export function useMutation<
138138

139139
returnclient
140140
.mutate(clientOptionsasMutationOptions<TData,OperationVariables>)
141-
.then((response)=>{
142-
const{ data, errors}=response;
143-
consterror=
144-
errors&&errors.length>0 ?
145-
newApolloError({graphQLErrors:errors})
146-
:void0;
147-
148-
constonError=
149-
executeOptions.onError||ref.current.options?.onError;
150-
151-
if(error&&onError){
152-
onError(
153-
error,
154-
clientOptionsasMutationOptions<TData,OperationVariables>
155-
);
156-
}
141+
.then(
142+
(response)=>{
143+
const{ data, errors}=response;
144+
consterror=
145+
errors&&errors.length>0 ?
146+
newApolloError({graphQLErrors:errors})
147+
:void0;
148+
149+
constonError=
150+
executeOptions.onError||ref.current.options?.onError;
151+
152+
if(error&&onError){
153+
onError(
154+
error,
155+
clientOptionsasMutationOptions<TData,OperationVariables>
156+
);
157+
}
157158

158-
if(
159-
mutationId===ref.current.mutationId&&
160-
!clientOptions.ignoreResults
161-
){
162-
constresult={
163-
called:true,
164-
loading:false,
165-
data,
166-
error,
167-
client,
168-
};
169-
170-
if(ref.current.isMounted&&!equal(ref.current.result,result)){
171-
setResult((ref.current.result=result));
159+
if(
160+
mutationId===ref.current.mutationId&&
161+
!clientOptions.ignoreResults
162+
){
163+
constresult={
164+
called:true,
165+
loading:false,
166+
data,
167+
error,
168+
client,
169+
};
170+
171+
if(ref.current.isMounted&&!equal(ref.current.result,result)){
172+
setResult((ref.current.result=result));
173+
}
172174
}
173-
}
174175

175-
constonCompleted=
176-
executeOptions.onCompleted||ref.current.options?.onCompleted;
176+
constonCompleted=
177+
executeOptions.onCompleted||ref.current.options?.onCompleted;
177178

178-
if(!error){
179-
onCompleted?.(
180-
response.data!,
181-
clientOptionsasMutationOptions<TData,OperationVariables>
182-
);
183-
}
179+
if(!error){
180+
onCompleted?.(
181+
response.data!,
182+
clientOptionsasMutationOptions<TData,OperationVariables>
183+
);
184+
}
184185

185-
returnresponse;
186-
})
187-
.catch((error)=>{
188-
if(mutationId===ref.current.mutationId&&ref.current.isMounted){
189-
constresult={
190-
loading:false,
191-
error,
192-
data:void0,
193-
called:true,
194-
client,
195-
};
196-
197-
if(!equal(ref.current.result,result)){
198-
setResult((ref.current.result=result));
186+
returnresponse;
187+
},
188+
(error)=>{
189+
if(
190+
mutationId===ref.current.mutationId&&
191+
ref.current.isMounted
192+
){
193+
constresult={
194+
loading:false,
195+
error,
196+
data:void0,
197+
called:true,
198+
client,
199+
};
200+
201+
if(!equal(ref.current.result,result)){
202+
setResult((ref.current.result=result));
203+
}
199204
}
200-
}
201205

202-
constonError=
203-
executeOptions.onError||ref.current.options?.onError;
206+
constonError=
207+
executeOptions.onError||ref.current.options?.onError;
204208

205-
if(onError){
206-
onError(
207-
error,
208-
clientOptionsasMutationOptions<TData,OperationVariables>
209-
);
209+
if(onError){
210+
onError(
211+
error,
212+
clientOptionsasMutationOptions<TData,OperationVariables>
213+
);
210214

211-
// TODO(brian): why are we returning this here???
212-
return{data:void0,errors:error};
213-
}
215+
// TODO(brian): why are we returning this here???
216+
return{data:void0,errors:error};
217+
}
214218

215-
throwerror;
216-
});
219+
throwerror;
220+
}
221+
);
217222
},
218223
[]
219224
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp