Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"dependencies": {
"@mui/icons-material": "^5.16.12",
"@mui/material": "^5.16.12",
"@tanstack/react-query": "^5.83.0",
"@tanstack/react-query-devtools": "^5.83.0",
"@tanstack/react-query": "^5.90.2",
"@tanstack/react-query-devtools": "^5.90.2",
"fakerest": "^4.1.3",
"jsonexport": "^3.2.0",
"lodash": "~4.17.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"devDependencies": {
"@hookform/resolvers": "^3.2.0",
"@tanstack/react-query-devtools": "^5.21.7",
"@tanstack/react-query": "^5.90.2",
"@tanstack/react-query-devtools": "^5.90.2",
"@testing-library/react": "^15.0.7",
"@types/jest": "^29.5.2",
"@types/jscodeshift": "^0.11.11",
Expand Down
12 changes: 8 additions & 4 deletions packages/ra-core/src/controller/create/CreateBase.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('CreateBase', () => {
test: 'test',
},
{ data: { test: 'test' }, resource: 'posts' },
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand Down Expand Up @@ -87,7 +88,8 @@ describe('CreateBase', () => {
test: 'test',
},
{ data: { test: 'test' }, resource: 'posts' },
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
expect(onSuccess).not.toHaveBeenCalled();
Expand All @@ -114,7 +116,8 @@ describe('CreateBase', () => {
expect(onError).toHaveBeenCalledWith(
{ message: 'test' },
{ data: { test: 'test' }, resource: 'posts' },
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand All @@ -141,7 +144,8 @@ describe('CreateBase', () => {
expect(onErrorOverride).toHaveBeenCalledWith(
{ message: 'test' },
{ data: { test: 'test' }, resource: 'posts' },
{ snapshot: [] }
expect.anything(),
expect.anything()
);
});
expect(onError).not.toHaveBeenCalled();
Expand Down
10 changes: 6 additions & 4 deletions packages/ra-core/src/controller/create/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ export const useCreateController = <
MutationOptionsError,
ResultRecordType
>(resource, undefined, {
onSuccess: async (data, variables, context) => {
onSuccess: async (...args) => {
if (onSuccess) {
return onSuccess(data, variables, context);
return onSuccess(...args);
}
const [data] = args;
notify(`resources.${resource}.notifications.created`, {
type: 'info',
messageArgs: {
Expand All @@ -108,10 +109,11 @@ export const useCreateController = <
});
redirect(finalRedirectTo, resource, data.id, data);
},
onError: (error: MutationOptionsError, variables, context) => {
onError: (...args) => {
if (onError) {
return onError(error, variables, context);
return onError(...args);
}
const [error] = args;
// Don't trigger a notification if this is a validation error
// (notification will be handled by the useNotifyIsFormInvalid hook)
const validationErrors = (error as HttpError)?.body?.errors;
Expand Down
12 changes: 8 additions & 4 deletions packages/ra-core/src/controller/edit/EditBase.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ describe('EditBase', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand Down Expand Up @@ -125,7 +126,8 @@ describe('EditBase', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
expect(onSuccess).not.toHaveBeenCalled();
Expand Down Expand Up @@ -162,7 +164,8 @@ describe('EditBase', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand Down Expand Up @@ -199,7 +202,8 @@ describe('EditBase', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
expect(onError).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ describe('useEditController', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: expect.any(Array) }
{ snapshot: expect.any(Array) },
expect.anything()
)
);
expect(notificationsSpy).toEqual([]);
Expand Down Expand Up @@ -513,7 +514,8 @@ describe('useEditController', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: expect.any(Array) }
{ snapshot: expect.any(Array) },
expect.anything()
)
);
expect(notificationsSpy).toEqual([]);
Expand Down Expand Up @@ -566,7 +568,8 @@ describe('useEditController', () => {
resource: 'posts',
meta: undefined,
},
{ snapshot: expect.any(Array) }
{ snapshot: expect.any(Array) },
expect.anything()
)
);
expect(notificationsSpy).toEqual([]);
Expand Down
10 changes: 6 additions & 4 deletions packages/ra-core/src/controller/edit/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ export const useEditController = <
resource,
recordCached,
{
onSuccess: async (data, variables, context) => {
onSuccess: async (...args) => {
if (onSuccess) {
return onSuccess(data, variables, context);
return onSuccess(...args);
}
const [data] = args;
notify(`resources.${resource}.notifications.updated`, {
type: 'info',
messageArgs: {
Expand All @@ -191,10 +192,11 @@ export const useEditController = <
});
redirect(redirectTo, resource, data.id, data);
},
onError: (error, variables, context) => {
onError: (...args) => {
if (onError) {
return onError(error, variables, context);
return onError(...args);
}
const [error] = args;
// Don't trigger a notification if this is a validation error
// (notification will be handled by the useNotifyIsFormInvalid hook)
const validationErrors = (error as HttpError)?.body?.errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ describe('useListController', () => {
);

// Check that the permanent filter is not included in the displayedFilters and filterValues (passed to Filter form and button)
expect(children).toHaveBeenCalledTimes(2);
expect(children).toHaveBeenCalledWith(
expect.objectContaining({
displayedFilters: {},
Expand All @@ -264,7 +263,7 @@ describe('useListController', () => {
'posts',
expect.objectContaining({ filter: { foo: 2 } })
);
expect(children).toHaveBeenCalledTimes(4);
expect(children).toHaveBeenCalledTimes(3);
});
});

Expand Down
74 changes: 35 additions & 39 deletions packages/ra-core/src/dataProvider/useCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ export const useCreate = <
.then(({ data }) => data);
},
...mutationOptions,
onMutate: async (
variables: Partial<UseCreateMutateParams<RecordType>>
) => {
onMutate: async (...args) => {
if (mutationOptions.onMutate) {
const userContext =
(await mutationOptions.onMutate(variables)) || {};
(await mutationOptions.onMutate(...args)) || {};
return {
snapshot: snapshot.current,
// @ts-ignore
Expand All @@ -200,27 +198,27 @@ export const useCreate = <
return { snapshot: snapshot.current };
}
},
onError: (error, variables, context: { snapshot: Snapshot }) => {
onError: (...args) => {
if (mode.current === 'optimistic' || mode.current === 'undoable') {
const [, , onMutateResult] = args;
// If the mutation fails, use the context returned from onMutate to rollback
context.snapshot.forEach(([key, value]) => {
queryClient.setQueryData(key, value);
});
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
([key, value]) => {
queryClient.setQueryData(key, value);
}
);
}
if (callTimeOnError.current) {
return callTimeOnError.current(error, variables, context);
return callTimeOnError.current(...args);
}
if (mutationOptions.onError) {
return mutationOptions.onError(error, variables, context);
return mutationOptions.onError(...args);
}
// call-time error callback is executed by react-query
},
onSuccess: (
data: ResultRecordType,
variables: Partial<UseCreateMutateParams<RecordType>> = {},
context: unknown
) => {
onSuccess: (...args) => {
if (mode.current === 'pessimistic') {
const [data, variables] = args;
const { resource: callTimeResource = resource } = variables;
queryClient.setQueryData(
[callTimeResource, 'getOne', { id: String(data.id) }],
Expand All @@ -243,38 +241,26 @@ export const useCreate = <
mutationOptions.onSuccess &&
!hasCallTimeOnSuccess.current
) {
mutationOptions.onSuccess(data, variables, context);
mutationOptions.onSuccess(...args);
}
}
},
onSettled: (
data,
error,
variables,
context: { snapshot: Snapshot }
) => {
onSettled: (...args) => {
if (mode.current === 'optimistic' || mode.current === 'undoable') {
const [, , , onMutateResult] = args;
// Always refetch after error or success:
context.snapshot.forEach(([queryKey]) => {
queryClient.invalidateQueries({ queryKey });
});
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
([queryKey]) => {
queryClient.invalidateQueries({ queryKey });
}
);
}

if (callTimeOnSettled.current) {
return callTimeOnSettled.current(
data,
error,
variables,
context
);
return callTimeOnSettled.current(...args);
}
if (mutationOptions.onSettled) {
return mutationOptions.onSettled(
data,
error,
variables,
context
);
return mutationOptions.onSettled(...args);
}
},
});
Expand Down Expand Up @@ -409,7 +395,12 @@ export const useCreate = <
onSuccess(
callTimeData as unknown as ResultRecordType,
{ resource: callTimeResource, ...callTimeParams },
{ snapshot: snapshot.current }
{ snapshot: snapshot.current },
{
client: queryClient,
mutationKey: [resource, 'create', params],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While reviewing I wondered if we shouldn't use callTimeParams here instead, but I realize that we use the same mutationKey while creating the mutation, which may (or may not) be wrong.
In any case, at least it's consistent, so I guess we can ignore this for the context of this PR...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same query key as the one passed to useMutation otherwise it will cause issue with offline as you won't be able to target the mutation

meta: mutationOptions.meta,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here however I wonder if you shouldn't use callTimeMeta instead...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that's react-query meta, not react-admin meta

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we actually override them but this is the 4th parameter introduced in latest react-query and I think we should follow the same rule as the query key here

}
);
} else if (
mutationOptions.onSuccess &&
Expand All @@ -418,7 +409,12 @@ export const useCreate = <
mutationOptions.onSuccess(
callTimeData as unknown as ResultRecordType,
{ resource: callTimeResource, ...callTimeParams },
{ snapshot: snapshot.current }
{ snapshot: snapshot.current },
{
client: queryClient,
mutationKey: [resource, 'create', params],
meta: mutationOptions.meta,
}
);
}
}, 0);
Expand Down
6 changes: 4 additions & 2 deletions packages/ra-core/src/dataProvider/useDelete.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ describe('useDelete', () => {
expect(onSuccess).toHaveBeenCalledWith(
{ id: 1 },
{ id: 1, previousData: { foo: 456 }, resource: 'foo' },
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand Down Expand Up @@ -372,7 +373,8 @@ describe('useDelete', () => {
expect(onError).toHaveBeenCalledWith(
new Error('not good'),
{ id: 1, previousData: { foo: 456 }, resource: 'foo' },
{ snapshot: [] }
{ snapshot: [] },
expect.anything()
);
});
});
Expand Down
Loading