Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where setting defaultOptions would sometimes break startTransition for suspense hooks #11713

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/tasty-hotels-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix issue where setting a default `watchQuery` option in the `ApolloClient` constructor could break `startTransition` when used with suspense hooks.
204 changes: 204 additions & 0 deletions src/react/hooks/__tests__/useBackgroundQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5240,6 +5240,210 @@ describe("fetchMore", () => {

await expect(Profiler).not.toRerender();
});

// https://github.com/apollographql/apollo-client/issues/11708
it("`fetchMore` works with startTransition when setting errorPolicy as default option in ApolloClient constructor", async () => {
type Variables = {
offset: number;
};

interface Todo {
__typename: "Todo";
id: string;
name: string;
completed: boolean;
}
interface Data {
todos: Todo[];
}
const user = userEvent.setup();

const query: TypedDocumentNode<Data, Variables> = gql`
query TodosQuery($offset: Int!) {
todos(offset: $offset) {
id
name
completed
}
}
`;

const mocks: MockedResponse<Data, Variables>[] = [
{
request: { query, variables: { offset: 0 } },
result: {
data: {
todos: [
{
__typename: "Todo",
id: "1",
name: "Clean room",
completed: false,
},
],
},
},
delay: 10,
},
{
request: { query, variables: { offset: 1 } },
result: {
data: {
todos: [
{
__typename: "Todo",
id: "2",
name: "Take out trash",
completed: true,
},
],
},
},
delay: 10,
},
];

const Profiler = createProfiler({
initialSnapshot: {
isPending: false,
result: null as UseReadQueryResult<Data> | null,
},
});

const { SuspenseFallback, ReadQueryHook } =
createDefaultTrackedComponents(Profiler);

const client = new ApolloClient({
link: new MockLink(mocks),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
todos: offsetLimitPagination(),
},
},
},
}),
defaultOptions: {
watchQuery: {
errorPolicy: "all",
},
},
});

function App() {
useTrackRenders();
const [isPending, startTransition] = React.useTransition();
const [queryRef, { fetchMore }] = useBackgroundQuery(query, {
variables: { offset: 0 },
});

Profiler.mergeSnapshot({ isPending });

return (
<>
<button
onClick={() => {
startTransition(() => {
fetchMore({ variables: { offset: 1 } });
});
}}
>
Load more
</button>
<Suspense fallback={<SuspenseFallback />}>
<ReadQueryHook queryRef={queryRef} />
</Suspense>
</>
);
}

renderWithClient(<App />, { client, wrapper: Profiler });

{
const { renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual([App, SuspenseFallback]);
}

{
const { snapshot } = await Profiler.takeRender();

expect(snapshot).toEqual({
isPending: false,
result: {
data: {
todos: [
{
__typename: "Todo",
id: "1",
name: "Clean room",
completed: false,
},
],
},
error: undefined,
networkStatus: NetworkStatus.ready,
},
});
}

await act(() => user.click(screen.getByText("Load more")));

{
const { snapshot, renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual([App, ReadQueryHook]);
expect(snapshot).toEqual({
isPending: true,
result: {
data: {
todos: [
{
__typename: "Todo",
id: "1",
name: "Clean room",
completed: false,
},
],
},
error: undefined,
networkStatus: NetworkStatus.ready,
},
});
}

{
const { snapshot, renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual([App, ReadQueryHook]);
expect(snapshot).toEqual({
isPending: false,
result: {
data: {
todos: [
{
__typename: "Todo",
id: "1",
name: "Clean room",
completed: false,
},
{
__typename: "Todo",
id: "2",
name: "Take out trash",
completed: true,
},
],
},
error: undefined,
networkStatus: NetworkStatus.ready,
},
});
}

await expect(Profiler).not.toRerender();
});
});

describe.skip("type tests", () => {
Expand Down
Loading
Loading