From f664d8713f760c179d2f82f2a576eb47e094cad4 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 3b339d84401..7fac0de70bf 100644 --- a/src/core/ObservableQuery.ts +++ b/src/core/ObservableQuery.ts @@ -288,7 +288,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 8e17b4dea7f..68a314d5f12 100644 --- a/src/react/hooks/__tests__/useQuery.test.tsx +++ b/src/react/hooks/__tests__/useQuery.test.tsx @@ -2719,6 +2719,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', () => {