Skip to content

Commit

Permalink
refactor(solid-query): separate queryOptions from createQuery (#7806)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
lachlancollins and TkDodo authored Aug 1, 2024
1 parent 23c3da2 commit 1dcd532
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { describe } from 'node:test'
import { expectTypeOf, it } from 'vitest'
import { type InfiniteData, dataTagSymbol } from '@tanstack/query-core'
import {
createInfiniteQuery,
infiniteQueryOptions,
} from '../createInfiniteQuery'
import { createInfiniteQuery } from '../createInfiniteQuery'
import { infiniteQueryOptions } from '../infiniteQueryOptions'
import type {
DefinedInitialDataInfiniteOptions,
UndefinedInitialDataInfiniteOptions,
} from '../createInfiniteQuery'
} from '../infiniteQueryOptions'

const doNotRun = (_callback: () => void) => {}

Expand Down
3 changes: 2 additions & 1 deletion packages/solid-query/src/__tests__/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, expectTypeOf, it } from 'vitest'
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
import { createQuery, queryOptions } from '../createQuery'
import { createQuery } from '../createQuery'
import { queryOptions } from '../queryOptions'

describe('queryOptions', () => {
it('should not allow excess properties', () => {
Expand Down
108 changes: 4 additions & 104 deletions packages/solid-query/src/createInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { InfiniteQueryObserver } from '@tanstack/query-core'
import { createMemo } from 'solid-js'
import { createBaseQuery } from './createBaseQuery'
import type {
DataTag,
DefaultError,
InfiniteData,
QueryKey,
Expand All @@ -13,111 +12,12 @@ import type {
CreateInfiniteQueryOptions,
CreateInfiniteQueryResult,
DefinedCreateInfiniteQueryResult,
FunctionedParams,
SolidInfiniteQueryOptions,
} from './types'
import type { Accessor } from 'solid-js'

export type UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = FunctionedParams<
SolidInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
> & {
initialData?: undefined
}
>
type NonUndefinedGuard<T> = T extends undefined ? never : T
export type DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError = DefaultError,
// should we handle page param correctly
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = FunctionedParams<
SolidInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
> & {
initialData:
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
}
>
function infiniteQueryOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: ReturnType<
DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
>,
): ReturnType<
DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
> & {
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>>
}
function infiniteQueryOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: ReturnType<
UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
>,
): ReturnType<
UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
> & {
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>>
}

function infiniteQueryOptions(options: unknown) {
return options
}

export { infiniteQueryOptions }
import type {
DefinedInitialDataInfiniteOptions,
UndefinedInitialDataInfiniteOptions,
} from './infiniteQueryOptions'

export function createInfiniteQuery<
TQueryFnData,
Expand Down
74 changes: 5 additions & 69 deletions packages/solid-query/src/createQuery.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,18 @@
import { QueryObserver } from '@tanstack/query-core'
import { createMemo } from 'solid-js'
import { createBaseQuery } from './createBaseQuery'
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
import type { DefaultError, QueryKey } from '@tanstack/query-core'
import type { QueryClient } from './QueryClient'
import type { Accessor } from 'solid-js'
import type {
CreateQueryOptions,
CreateQueryResult,
DefinedCreateQueryResult,
FunctionedParams,
SolidQueryOptions,
} from './types'

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

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

export function queryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TOptions extends ReturnType<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
> = ReturnType<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
>(
options: ReturnType<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
): ReturnType<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
> & {
queryKey: DataTag<TQueryKey, TQueryFnData>
}

export function queryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TOptions extends ReturnType<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
> = ReturnType<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
>(
options: ReturnType<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
): ReturnType<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
> & {
queryKey: DataTag<TQueryKey, TQueryFnData>
}

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

export function createQuery<
TQueryFnData = unknown,
Expand Down
13 changes: 6 additions & 7 deletions packages/solid-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ export type {
QueryClientConfig,
InfiniteQueryObserverOptions,
} from './QueryClient'
export { createQuery, queryOptions } from './createQuery'
export { createQuery } from './createQuery'
export { queryOptions } from './queryOptions'
export type {
DefinedInitialDataOptions,
UndefinedInitialDataOptions,
} from './createQuery'
} from './queryOptions'
export {
QueryClientContext,
QueryClientProvider,
useQueryClient,
} from './QueryClientProvider'
export type { QueryClientProviderProps } from './QueryClientProvider'
export { useIsFetching } from './useIsFetching'
export {
createInfiniteQuery,
infiniteQueryOptions,
} from './createInfiniteQuery'
export { createInfiniteQuery } from './createInfiniteQuery'
export { infiniteQueryOptions } from './infiniteQueryOptions'
export type {
DefinedInitialDataInfiniteOptions,
UndefinedInitialDataInfiniteOptions,
} from './createInfiniteQuery'
} from './infiniteQueryOptions'
export { createMutation } from './createMutation'
export { useIsMutating } from './useIsMutating'
export { useMutationState } from './useMutationState'
Expand Down
108 changes: 108 additions & 0 deletions packages/solid-query/src/infiniteQueryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import type {
DataTag,
DefaultError,
InfiniteData,
QueryKey,
} from '@tanstack/query-core'
import type { FunctionedParams, SolidInfiniteQueryOptions } from './types'

export type UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = FunctionedParams<
SolidInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
> & {
initialData?: undefined
}
>

type NonUndefinedGuard<T> = T extends undefined ? never : T

export type DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError = DefaultError,
// should we handle page param correctly
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = FunctionedParams<
SolidInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
> & {
initialData:
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
}
>
export function infiniteQueryOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: ReturnType<
DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
>,
): ReturnType<
DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
> & {
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>>
}
export function infiniteQueryOptions<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: ReturnType<
UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
>,
): ReturnType<
UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
> & {
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>>
}

export function infiniteQueryOptions(options: unknown) {
return options
}
Loading

0 comments on commit 1dcd532

Please sign in to comment.