From cecf521bc0a82e0a5537ff38f9c2f8ec17421d22 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 1 Jul 2020 14:07:33 -0600 Subject: [PATCH] fix: allow empty query key (with config enabled) --- src/core/config.js | 10 +++------- src/react/tests/useQuery.test.js | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/core/config.js b/src/core/config.js index 301bbb7247..9222c5c808 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -39,7 +39,7 @@ export const defaultConfigRef = { export function defaultQueryKeySerializerFn(queryKey) { if (!queryKey) { - invalidQueryKey() + return [] } if (!Array.isArray(queryKey)) { @@ -47,19 +47,15 @@ export function defaultQueryKeySerializerFn(queryKey) { } if (queryKey.some(d => typeof d === 'function')) { - invalidQueryKey() + throw new Error('A valid query key is required!') } const queryHash = stableStringify(queryKey) queryKey = JSON.parse(queryHash) if (!queryHash) { - invalidQueryKey() + return [] } return [queryHash, queryKey] } - -function invalidQueryKey() { - throw new Error('A valid query key is required!') -} diff --git a/src/react/tests/useQuery.test.js b/src/react/tests/useQuery.test.js index 444ff3223a..4a61c1983e 100644 --- a/src/react/tests/useQuery.test.js +++ b/src/react/tests/useQuery.test.js @@ -599,14 +599,14 @@ describe('useQuery', () => { rendered.getByText('status: idle') }) - it('it should throw when using query syntax and missing required keys', async () => { + it('it should throw when using a bad query syntax', async () => { // mock console.error to avoid the wall of red text, // you could also do this on beforeEach/afterEach jest.spyOn(console, 'error') console.error.mockImplementation(() => {}) function Page() { - useQuery({}) + useQuery(() => {}) return null } expect(() => render()).toThrowError(/query key/)