Skip to content

Commit

Permalink
Merge pull request #4315 from rkofman/master
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored May 9, 2024
2 parents 3e021de + e22da96 commit 5ec40d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
13 changes: 8 additions & 5 deletions packages/toolkit/src/query/defaultSerializeQueryArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({
if (typeof cached === 'string') {
serialized = cached
} else {
const stringified = JSON.stringify(queryArgs, (key, value) =>
isPlainObject(value)
const stringified = JSON.stringify(queryArgs, (key, value) => {
// Handle bigints
value = typeof value === 'bigint' ? { $bigint: value.toString() } : value
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
value = isPlainObject(value)
? Object.keys(value)
.sort()
.reduce<any>((acc, key) => {
acc[key] = (value as any)[key]
return acc
}, {})
: value,
)
: value
return value
})
if (isPlainObject(queryArgs)) {
cache?.set(queryArgs, stringified)
}
serialized = stringified
}
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
return `${endpointName}(${serialized})`
}

Expand Down
38 changes: 36 additions & 2 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const api = createApi({
getError: build.query({
query: () => '/error',
}),
listItems: build.query<Item[], { pageNumber: number }>({
listItems: build.query<Item[], { pageNumber: number | bigint }>({
serializeQueryArgs: ({ endpointName }) => {
return endpointName
},
Expand Down Expand Up @@ -151,6 +151,7 @@ beforeEach(() => {
})

afterEach(() => {
nextItemId = 0
amount = 0
listenerMiddleware.clearListeners()
})
Expand Down Expand Up @@ -630,7 +631,40 @@ describe('hooks tests', () => {
test(`useQuery refetches when query args object changes even if serialized args don't change`, async () => {
function ItemList() {
const [pageNumber, setPageNumber] = useState(0)
const { data = [] } = api.useListItemsQuery({ pageNumber })
const { data = [] } = api.useListItemsQuery({
pageNumber: pageNumber,
})

const renderedItems = data.map((item) => (
<li key={item.id}>ID: {item.id}</li>
))
return (
<div>
<button onClick={() => setPageNumber(pageNumber + 1)}>
Next Page
</button>
<ul>{renderedItems}</ul>
</div>
)
}

render(<ItemList />, { wrapper: storeRef.wrapper })

await screen.findByText('ID: 0')

await act(async () => {
screen.getByText('Next Page').click()
})

await screen.findByText('ID: 3')
})

test(`useQuery gracefully handles bigint types`, async () => {
function ItemList() {
const [pageNumber, setPageNumber] = useState(0)
const { data = [] } = api.useListItemsQuery({
pageNumber: BigInt(pageNumber),
})

const renderedItems = data.map((item) => (
<li key={item.id}>ID: {item.id}</li>
Expand Down
10 changes: 10 additions & 0 deletions packages/toolkit/src/query/tests/defaultSerializeQueryArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ test('number arg', () => {
).toMatchInlineSnapshot(`"test(5)"`)
})

test('bigint arg has non-default serialization (intead of throwing)', () => {
expect(
defaultSerializeQueryArgs({
endpointDefinition,
endpointName,
queryArgs: BigInt(10),
}),
).toMatchInlineSnapshot(`"test({"$bigint":"10"})"`)
})

test('simple object arg is sorted', () => {
expect(
defaultSerializeQueryArgs({
Expand Down

0 comments on commit 5ec40d8

Please sign in to comment.