Skip to content

Commit

Permalink
Don’t refetch when fetchPolicy is standby
Browse files Browse the repository at this point in the history
Fixes #8270
  • Loading branch information
brainkim committed Aug 16, 2021
1 parent 9e523d5 commit 14cf342
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ export class ObservableQuery<
// (no-cache, network-only, or cache-and-network), override it with
// network-only to force the refetch for this fetchQuery call.
const { fetchPolicy } = this.options;
if (fetchPolicy === 'no-cache') {
if (fetchPolicy === 'standby') {
// do nothing
} else if (fetchPolicy === 'no-cache') {
reobserveOptions.fetchPolicy = 'no-cache';
} else if (fetchPolicy !== 'cache-and-network') {
reobserveOptions.fetchPolicy = 'network-only';
Expand Down
34 changes: 34 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,40 @@ describe('useQuery Hook', () => {
expect(result.current.loading).toBeFalsy();
expect(result.current.data).toEqual({ hello: 'world' });
});

it('should not refetch when skip is true', async () => {
const query = gql`{ hello }`;
const link = ApolloLink.empty();
const requestSpy = jest.spyOn(link, 'request');

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});

const { result, waitForNextUpdate } = renderHook(
() => useQuery(query, { skip: true }),
{
wrapper: ({ children }) => (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
),
},
);

expect(result.current.loading).toBe(false);
expect(result.current.data).toBe(undefined);
await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');

result.current.refetch();

await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');
expect(result.current.loading).toBe(false);
expect(result.current.data).toBe(undefined);
expect(requestSpy).toHaveBeenCalledTimes(0);
requestSpy.mockRestore();
});
});

describe('Missing Fields', () => {
Expand Down

0 comments on commit 14cf342

Please sign in to comment.