Skip to content

Commit

Permalink
refactor: move queryOptions to its own file (#5480)
Browse files Browse the repository at this point in the history
* refactor: remove 'use client' from non-components

* refactor: move queryOptions to its own file

where we don't have a 'use client' directive, because we can call this function on the server as well

* Revert "refactor: remove 'use client' from non-components"

This reverts commit 8ecac66.

* refactor: rename isRestoring file

* chore: fix import
  • Loading branch information
TkDodo authored Jun 17, 2023
1 parent b99bf33 commit ba7fc56
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion packages/react-query/src/__tests__/useQuery.types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { queryOptions, useQuery } from '../useQuery'
import { useQuery } from '../useQuery'
import { queryOptions } from '../queryOptions'
import type { Expect, Equal } from './utils'
import { doNotExecute } from './utils'

Expand Down
3 changes: 2 additions & 1 deletion packages/react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export * from '@tanstack/query-core'
export * from './types'
export { useQueries } from './useQueries'
export type { QueriesResults, QueriesOptions } from './useQueries'
export { useQuery, queryOptions } from './useQuery'
export { useQuery } from './useQuery'
export { queryOptions } from './queryOptions'
export {
QueryClientContext,
QueryClientProvider,
Expand Down
File renamed without changes.
42 changes: 42 additions & 0 deletions packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { DefaultError, QueryKey } from '@tanstack/query-core'
import type { UseQueryOptions } from './types'

export type UndefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined
}

export type DefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData: TQueryFnData | (() => TQueryFnData)
}

export function queryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

export function queryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

export function queryOptions(options: unknown) {
return options
}
43 changes: 4 additions & 39 deletions packages/react-query/src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,12 @@ import type {
UseQueryResult,
} from './types'
import { useBaseQuery } from './useBaseQuery'

export function queryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

export function queryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

export function queryOptions(options: unknown) {
return options
}
import type {
DefinedInitialDataOptions,
UndefinedInitialDataOptions,
} from './queryOptions'

// HOOK
type UndefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined
}

type DefinedInitialDataOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData: TQueryFnData | (() => TQueryFnData)
}

export function useQuery<
TQueryFnData = unknown,
Expand Down

0 comments on commit ba7fc56

Please sign in to comment.