Skip to content

Commit

Permalink
fix: allow empty query key (with config enabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jul 1, 2020
1 parent cee6175 commit cecf521
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,23 @@ export const defaultConfigRef = {

export function defaultQueryKeySerializerFn(queryKey) {
if (!queryKey) {
invalidQueryKey()
return []
}

if (!Array.isArray(queryKey)) {
queryKey = [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!')
}
4 changes: 2 additions & 2 deletions src/react/tests/useQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Page />)).toThrowError(/query key/)
Expand Down

0 comments on commit cecf521

Please sign in to comment.