Skip to content
Open
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
11 changes: 11 additions & 0 deletions .changeset/chatty-cameras-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@refinedev/core": patch
---

fix(core): keep query context signal lazy when merging meta

`prepareQueryContext` was previously spread into new `meta` objects inside core data hooks. Because `signal` is exposed as a getter, object spread eagerly accessed it during merge and marked the query as abortable earlier than intended.

This keeps `signal` lazy by merging `meta` inside `prepareQueryContext` and preserving the getter on the returned object. The fix is applied across the affected hooks in `@refinedev/core`, and a regression test was added for the lazy `signal` behavior.

Resolves #7132
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { prepareQueryContext } from ".";

describe("prepareQueryContext", () => {
it("should keep signal lazy when merging it into meta", () => {
const signal = new AbortController().signal;
const signalGetter = vi.fn(() => signal);
const context = {
queryKey: ["posts", "list"],
get signal() {
return signalGetter();
},
} as any;

const meta = prepareQueryContext(context, { foo: "bar" });

expect(meta.foo).toBe("bar");
expect(meta.queryKey).toEqual(["posts", "list"]);
expect(signalGetter).not.toHaveBeenCalled();

expect(meta.signal).toBe(signal);
expect(signalGetter).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ type Context =
| QueryFunctionContext<QueryKey, any>
| QueryFunctionContext<QueryKey, never>;

type QueryContextMeta<TMeta extends Record<string, unknown> | undefined> = (
TMeta extends Record<string, unknown> ? TMeta : Record<string, never>
) &
Pick<Context, "queryKey" | "signal">;

export const prepareQueryContext = (
context: Context,
): Pick<Context, "queryKey" | "signal"> => {
const queryContext: Pick<Context, "queryKey" | "signal"> = {
meta?: Record<string, unknown>,
): QueryContextMeta<typeof meta> => {
const queryContext = {
...(meta ?? {}),
queryKey: context.queryKey,
signal: undefined as any,
};
} as QueryContextMeta<typeof meta>;

Object.defineProperty(queryContext, "signal", {
enumerable: true,
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/hooks/data/useCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ export const useCustom = <
url,
method,
...config,
meta: {
...combinedMeta,
...prepareQueryContext(context as any),
},
meta: prepareQueryContext(context as any, combinedMeta),
}),
...queryOptions,
meta: {
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/hooks/data/useInfiniteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ export const useInfiniteList = <
(context.pageParam as number) ?? prefferedPagination.currentPage,
};

const meta = {
...combinedMeta,
...prepareQueryContext(context),
};
const meta = prepareQueryContext(context, combinedMeta);

return getList<TQueryFnData>({
resource: resource?.name || "",
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/hooks/data/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ export const useList = <
})
.get(),
queryFn: (context) => {
const meta = {
...combinedMeta,
...prepareQueryContext(context),
};
const meta = prepareQueryContext(context, combinedMeta);
return getList<TQueryFnData>({
resource: resource?.name ?? "",
pagination: prefferedPagination,
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/hooks/data/useMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ export const useMany = <
})
.get(),
queryFn: (context) => {
const meta = {
...combinedMeta,
...prepareQueryContext(context as any),
};
const meta = prepareQueryContext(context as any, combinedMeta);

if (getMany) {
return getMany({
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/hooks/data/useOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ export const useOne = <
getOne<TQueryFnData>({
resource: resource?.name ?? "",
id: id!,
meta: {
...combinedMeta,
...prepareQueryContext(context as any),
},
meta: prepareQueryContext(context as any, combinedMeta),
}),
...queryOptions,
enabled: isEnabled,
Expand Down