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', () => {