From 14cf3420a8d1e5430bb04b3eadf73ea772a2b146 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Fri, 13 Aug 2021 14:14:05 -0400 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20refetch=20when=20fetchPolicy=20?= =?UTF-8?q?is=20standby?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #8270 --- src/core/ObservableQuery.ts | 4 ++- src/react/hooks/__tests__/useQuery.test.tsx | 34 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/core/ObservableQuery.ts b/src/core/ObservableQuery.ts index 6038a87755f..cb4b57c6f06 100644 --- a/src/core/ObservableQuery.ts +++ b/src/core/ObservableQuery.ts @@ -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'; diff --git a/src/react/hooks/__tests__/useQuery.test.tsx b/src/react/hooks/__tests__/useQuery.test.tsx index 91f3b18c463..83b039845a9 100644 --- a/src/react/hooks/__tests__/useQuery.test.tsx +++ b/src/react/hooks/__tests__/useQuery.test.tsx @@ -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 }) => ( + + {children} + + ), + }, + ); + + 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', () => {