Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-rabbits-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': minor
---

Added queryHash to QueryFunctionContext, giving query functions direct access to the computed query hash. This provides first-class access to each query’s unique identifier and removes the need to manually import or recompute hashQueryKeyByOptions.
1 change: 1 addition & 0 deletions docs/framework/react/guides/query-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function fetchTodoList({ queryKey }) {
The `QueryFunctionContext` is the object passed to each query function. It consists of:

- `queryKey: QueryKey`: [Query Keys](../query-keys.md)
- `queryHash: string`: The hash of the query key, used as a unique identifier for the query
- `client: QueryClient`: [QueryClient](../../../../reference/QueryClient.md)
- `signal?: AbortSignal`
- [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instance provided by TanStack Query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ describe('injectQuery', () => {
client: queryClient,
meta: undefined,
queryKey: ['key8'],
queryHash: '["key8"]',
signal: expect.anything(),
})
})
Expand Down
10 changes: 10 additions & 0 deletions packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { CancelledError, InfiniteQueryObserver, QueryClient } from '..'
import { hashQueryKeyByOptions } from '../utils'
import type { InfiniteData, InfiniteQueryObserverResult, QueryCache } from '..'

describe('InfiniteQueryBehavior', () => {
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 1,
meta: undefined,
Expand All @@ -96,6 +98,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 2,
direction: 'forward',
Expand All @@ -115,6 +118,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 0,
direction: 'backward',
Expand All @@ -135,6 +139,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: -1,
meta: undefined,
Expand All @@ -154,6 +159,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 1,
meta: undefined,
Expand All @@ -176,6 +182,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 0,
meta: undefined,
Expand All @@ -185,6 +192,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(2, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 1,
meta: undefined,
Expand Down Expand Up @@ -237,6 +245,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 1,
meta: undefined,
Expand Down Expand Up @@ -293,6 +302,7 @@ describe('InfiniteQueryBehavior', () => {

expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
client: queryClient,
pageParam: 2,
meta: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ describe('query', () => {
expect(args).toBeDefined()
expect(args.pageParam).toBeUndefined()
expect(args.queryKey).toEqual(key)
expect(args.queryHash).toBe(hashQueryKeyByOptions(key))
expect(args.signal).toBeInstanceOf(AbortSignal)
expect(args.client).toEqual(queryClient)
})
Expand Down
1 change: 1 addition & 0 deletions packages/query-core/src/__tests__/queryClient.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ describe('defaultOptions', () => {
expectTypeOf(context).toEqualTypeOf<{
client: QueryClient
queryKey: QueryKey
queryHash: string
meta: Record<string, unknown> | undefined
signal: AbortSignal
pageParam?: unknown
Expand Down
2 changes: 2 additions & 0 deletions packages/query-core/src/infiniteQueryBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
> = {
client: context.client,
queryKey: context.queryKey,
queryHash: query.queryHash,
pageParam: param,
direction: previous ? 'backward' : 'forward',
meta: context.options.meta,
Expand Down Expand Up @@ -119,6 +120,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
{
client: context.client,
queryKey: context.queryKey,
queryHash: query.queryHash,
meta: context.options.meta,
signal: context.signal,
},
Expand Down
1 change: 1 addition & 0 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ export class Query<
> = {
client: this.#client,
queryKey: this.queryKey,
queryHash: this.queryHash,
meta: this.meta,
}
addSignalProperty(queryFnContext)
Expand Down
2 changes: 2 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export type QueryFunctionContext<
? {
client: QueryClient
queryKey: TQueryKey
queryHash: string
signal: AbortSignal
meta: QueryMeta | undefined
pageParam?: unknown
Expand All @@ -154,6 +155,7 @@ export type QueryFunctionContext<
: {
client: QueryClient
queryKey: TQueryKey
queryHash: string
signal: AbortSignal
pageParam: TPageParam
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ function setupPersister(
persisterOptions: StoragePersisterOptions,
) {
const client = new QueryClient()
const queryHash = hashKey(queryKey)
const context = {
meta: { foo: 'bar' },
client,
queryKey,
queryHash,
// @ts-expect-error
signal: undefined as AbortSignal,
} satisfies QueryFunctionContext
const queryHash = hashKey(queryKey)
const storageKey = `${PERSISTER_KEY_PREFIX}-${queryHash}`

const queryFn = vi.fn()
Expand Down