Skip to content

Commit

Permalink
refactor(core): prevent early access to signal from query context (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemir committed Apr 24, 2024
1 parent 7c22b8e commit 8d2dd43
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 116 deletions.
9 changes: 9 additions & 0 deletions .changeset/eleven-kangaroos-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/core": patch
---

refactor: prevented early accessing `signal` from `useQuery` of `@tanstack/react-query`

In query hooks, `signal` was accessed directly by destructuring which was causing issues in development mode with duplicated requests. This change accesses `queryContext` instead of destructured `signal` properly to prevent `@tanstack/react-query` from setting `abortSignalConsumed` flag unexpectedly.

Resolves [#5843](https://github.com/refinedev/refine/issues/5843)
10 changes: 2 additions & 8 deletions examples/data-provider-strapi-v4/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ describe("data-provider-strapi-v4", () => {
});

it("should list with pagination", () => {
cy.wait("@strapiV4GetPosts");
cy.getAntdLoadingOverlay().should("not.exist");

cy.wait("@strapiV4GetPosts").then(({ request }) => {
const query = request.query;
expect(query.pagination).to.deep.equal({
page: "1",
pageSize: "10",
});
});
cy.getAntdLoadingOverlay().should("not.exist");

cy.get(".ant-pagination-item-2 > a").click();
cy.wait("@strapiV4GetPosts").then(({ request }) => {
Expand All @@ -111,13 +109,11 @@ describe("data-provider-strapi-v4", () => {
});

it("should sort", () => {
cy.wait("@strapiV4GetPosts");
cy.getAntdLoadingOverlay().should("not.exist");

cy.wait("@strapiV4GetPosts").then(({ request }) => {
const query = request.query;
expect(query.sort).to.eq("id:desc");
});
cy.getAntdLoadingOverlay().should("not.exist");

cy.getAntdColumnSorter(0).click();
cy.wait("@strapiV4GetPosts").then(({ request }) => {
Expand All @@ -135,7 +131,6 @@ describe("data-provider-strapi-v4", () => {
it("should filter", () => {
cy.wait("@strapiV4GetCategories");
cy.wait("@strapiV4GetPosts");
cy.wait("@strapiV4GetPosts");
cy.getAntdLoadingOverlay().should("not.exist");

cy.getAntdFilterTrigger(0).click();
Expand Down Expand Up @@ -298,7 +293,6 @@ describe("data-provider-strapi-v4", () => {
});

it("should change locale", () => {
cy.wait("@strapiV4GetPosts");
cy.get("#locale").contains("Deutsch").click();
cy.wait("@strapiV4GetPosts").then(({ request }) => {
const query = request.query;
Expand Down
7 changes: 1 addition & 6 deletions examples/data-provider-supabase/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe("data-provider-supabase", () => {
cy.getAntdLoadingOverlay().should("not.exist");

cy.get(".ant-pagination-item-2 > a").click();
cy.wait("@supabaseGetPosts");
cy.wait("@supabaseGetPosts").then(({ request }) => {
const query = request.query;
expect(query).to.deep.equal({
Expand All @@ -108,9 +107,6 @@ describe("data-provider-supabase", () => {
});

it("should sort", () => {
cy.wait("@supabaseGetPosts");
cy.getAntdLoadingOverlay().should("not.exist");

cy.wait("@supabaseGetPosts").then(({ request }) => {
const query = request.query;
expect(query).to.deep.equal({
Expand All @@ -120,6 +116,7 @@ describe("data-provider-supabase", () => {
order: "id.asc",
});
});
cy.getAntdLoadingOverlay().should("not.exist");

cy.getAntdColumnSorter(0).click();
cy.wait("@supabaseGetPosts").then(({ request }) => {
Expand Down Expand Up @@ -158,7 +155,6 @@ describe("data-provider-supabase", () => {
.click();
cy.contains(/filter/i).click({ force: true });

cy.wait("@supabaseGetPosts");
cy.wait("@supabaseGetPosts").then(({ request }) => {
const query = request.query;
expect(query).to.deep.equal({
Expand All @@ -181,7 +177,6 @@ describe("data-provider-supabase", () => {

it("should show", () => {
cy.wait("@supabaseGetPosts");
cy.wait("@supabaseGetPosts");

cy.getShowButton().first().click();
cy.location("pathname").should("contains", "/blog-posts/show/");
Expand Down
1 change: 0 additions & 1 deletion examples/form-antd-mutation-mode/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ describe("form-antd-mutation-mode", () => {
cy.getEditButton().first().should("not.be.disabled");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForEditPageLoading();

fillForm();
Expand Down
3 changes: 1 addition & 2 deletions examples/inferencer-mantine/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ describe("inferencer-mantine", () => {
});

it("should work with pagination", () => {
cy.wait("@getBlogPosts");
cy.wait("@getBlogPosts");
cy.wait("@getBlogPosts");
cy.wait("@getCategories");
Expand All @@ -285,8 +286,6 @@ describe("inferencer-mantine", () => {
cy.get(".mantine-Pagination-item").contains("2").click();
cy.url().should("include", "current=2");
cy.getMantineLoadingOverlay().should("not.exist");
cy.wait("@getBlogPosts");
cy.wait("@getBlogPosts");
cy.wait("@getBlogPosts").then((interception) => {
const { request } = interception;
const { _start, _end } = request.query;
Expand Down
1 change: 0 additions & 1 deletion examples/table-antd-table-filter/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe("table-antd-table-filter", () => {
cy.interceptGETCategories();
cy.visit("http://localhost:5173");
cy.wait("@getPosts");
cy.wait("@getPosts");
cy.wait("@getCategories");
});

Expand Down
3 changes: 1 addition & 2 deletions examples/table-chakra-ui-basic/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ describe("table-chakra-ui-basic", () => {

cy.wait("@getPosts").then((interception) => {
const { request } = interception;
const { _sort } = request.query;

expect(_sort).to.contains("id");
expect(request.query).not.to.ownProperty("_sort");
});

cy.intercept(
Expand Down
5 changes: 1 addition & 4 deletions examples/table-mantine-basic/cypress/e2e/all.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ describe("table-mantine-basic", () => {

cy.wait("@getPosts").then((interception) => {
const { request } = interception;
const { _sort } = request.query;

expect(_sort).not.to.contains("id");
expect(request.query).not.to.ownProperty("_sort");
});
});

Expand Down Expand Up @@ -94,8 +93,6 @@ describe("table-mantine-basic", () => {
});

it("should work with pagination", () => {
cy.wait("@getPosts");

cy.get(".mantine-Pagination-item").contains("2").click();

cy.url().should("include", "current=2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe("table-material-ui-table-filter", () => {
});

it("the table should be filterable by form", () => {
cy.wait("@getPosts");
cy.wait("@getPosts");
cy.getMaterialUILoadingCircular().should("not.exist");

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/definitions/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export { propertyPathToArray } from "./property-path-to-array";
export { downloadInBrowser } from "./downloadInBrowser";
export { deferExecution } from "./defer-execution";
export { asyncDebounce } from "./async-debounce";
export { prepareQueryContext } from "./prepare-query-context";
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { QueryFunctionContext, QueryKey } from "@tanstack/react-query";

export const prepareQueryContext = (
context: QueryFunctionContext<QueryKey, any>,
): Omit<QueryFunctionContext<QueryKey, any>, "meta"> => {
const queryContext = {
queryKey: context.queryKey,
pageParam: context.pageParam,
};

Object.defineProperty(queryContext, "signal", {
enumerable: true,
get: () => {
console.log("with getter prepare");
return context.signal;
},
});

return queryContext;
};
20 changes: 8 additions & 12 deletions packages/core/src/hooks/data/useCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
useQuery,
} from "@tanstack/react-query";

import { pickNotDeprecated, useActiveAuthProvider } from "@definitions/helpers";
import {
pickNotDeprecated,
useActiveAuthProvider,
prepareQueryContext,
} from "@definitions/helpers";
import {
useDataProvider,
useHandleNotification,
Expand Down Expand Up @@ -156,26 +160,18 @@ export const useCustom = <
...(preferredMeta || {}),
})
.get(preferLegacyKeys),
queryFn: ({ queryKey, pageParam, signal }) =>
queryFn: (context) =>
custom<TQueryFnData>({
url,
method,
...config,
meta: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
queryContext: prepareQueryContext(context),
},
metaData: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
queryContext: prepareQueryContext(context),
},
}),
...queryOptions,
Expand Down
28 changes: 10 additions & 18 deletions packages/core/src/hooks/data/useInfiniteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
handlePaginationParams,
pickDataProvider,
pickNotDeprecated,
prepareQueryContext,
useActiveAuthProvider,
} from "@definitions/helpers";
import {
Expand Down Expand Up @@ -250,10 +251,15 @@ export const useInfiniteList = <
}),
})
.get(preferLegacyKeys),
queryFn: ({ queryKey, pageParam, signal }) => {
queryFn: (context) => {
const paginationProperties = {
...prefferedPagination,
current: pageParam,
current: context.pageParam,
};

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

return getList<TQueryFnData>({
Expand All @@ -263,22 +269,8 @@ export const useInfiniteList = <
filters: prefferedFilters,
sort: prefferedSorters,
sorters: prefferedSorters,
meta: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
},
metaData: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
},
meta,
metaData: meta,
}).then(({ data, total, ...rest }) => {
return {
data,
Expand Down
25 changes: 8 additions & 17 deletions packages/core/src/hooks/data/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
handlePaginationParams,
pickDataProvider,
pickNotDeprecated,
prepareQueryContext,
useActiveAuthProvider,
} from "@definitions/helpers";
import {
Expand Down Expand Up @@ -248,30 +249,20 @@ export const useList = <
}),
})
.get(preferLegacyKeys),
queryFn: ({ queryKey, pageParam, signal }) => {
queryFn: (context) => {
const meta = {
...combinedMeta,
queryContext: prepareQueryContext(context),
};
return getList<TQueryFnData>({
resource: resource?.name ?? "",
pagination: prefferedPagination,
hasPagination: isServerPagination,
filters: prefferedFilters,
sort: prefferedSorters,
sorters: prefferedSorters,
meta: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
},
metaData: {
...combinedMeta,
queryContext: {
queryKey,
pageParam,
signal,
},
},
meta,
metaData: meta,
});
},
...queryOptions,
Expand Down

0 comments on commit 8d2dd43

Please sign in to comment.