import type { DefaultError, InfiniteData, InitialDataFunction, NonUndefinedGuard, QueryKey, QueryKeyWithDataTag, } from '@tanstack/query-core' import type { InfiniteQueryOptions } from './types' import type { Accessor } from 'solid-js' /** * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted, may be * `undefined`, or is a function that may return `undefined` — `data` may be `undefined` while the query is * `pending`. `infiniteQueryOptions` itself accepts and returns a plain object (its parameter type is * `ReturnType>`, i.e. this `Accessor` called); Solid's reactivity * applies where the result is consumed instead, e.g. `useInfiniteQuery(() => options)`. * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it. * @template TError - The type of errors your `queryFn` may throw. * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData`, * the shape of all fetched pages plus their page params. * @template TQueryKey - The type of your `queryKey`. * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page. */ export type UndefinedInitialDataInfiniteOptions< TQueryFnData, TError = DefaultError, TData = InfiniteData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, > = Accessor< InfiniteQueryOptions & { initialData?: | undefined | NonUndefinedGuard> | InitialDataFunction< NonUndefinedGuard> > } > /** * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be * defined — `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`). * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it. * @template TError - The type of errors your `queryFn` may throw. * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData`, * the shape of all fetched pages plus their page params. * @template TQueryKey - The type of your `queryKey`. * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page. */ export type DefinedInitialDataInfiniteOptions< TQueryFnData, TError = DefaultError, // should we handle page param correctly TData = InfiniteData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, > = Accessor< InfiniteQueryOptions & { initialData: | NonUndefinedGuard> | (() => NonUndefinedGuard>) } > /** * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`. * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`. * `options.queryKey` is required and is the query key to generate options for. * * This overload is selected when `initialData` is known to be defined. * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` known to be defined. * @returns The same options object, typed so that `queryKey` carries the inferred data type. * @see {@link useInfiniteQuery} to run an infinite query with these options. * @example * ```tsx * import { For } from 'solid-js' * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query' * * const projectsOptions = infiniteQueryOptions({ * queryKey: ['projects'], * queryFn: ({ pageParam }) => fetchProjects(pageParam), * initialPageParam: 0, * getNextPageParam: (lastPage) => lastPage.nextId, * initialData: { pages: [], pageParams: [] }, * }) * * function Projects() { * // `projectsQuery.data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the * // list stays visible alongside the error. * const projectsQuery = useInfiniteQuery(() => projectsOptions) * * return ( *
* {projectsQuery.isError ? Error: {projectsQuery.error.message} : null} *
    * * {(page) => {(p) =>
  • {p.name}
  • }
    } *
    *
*
* ) * } * ``` */ export function infiniteQueryOptions< TQueryFnData, TError = DefaultError, TData = InfiniteData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, >( options: ReturnType< DefinedInitialDataInfiniteOptions< TQueryFnData, TError, TData, TQueryKey, TPageParam > >, ): ReturnType< DefinedInitialDataInfiniteOptions< TQueryFnData, TError, TData, TQueryKey, TPageParam > > & QueryKeyWithDataTag, TError> /** * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`. * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`. * `options.queryKey` is required and is the query key to generate options for. * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`. * @returns The same options object, typed so that `queryKey` carries the inferred data type. * @see {@link useInfiniteQuery} to run an infinite query with these options. * @example * A parameterized factory, so the same options object can be reused per `postId`: * ```tsx * import { For, Match, Switch } from 'solid-js' * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query' * * const commentsOptions = (postId: string) => * infiniteQueryOptions({ * queryKey: ['post', postId, 'comments'], * queryFn: ({ pageParam }) => fetchComments(postId, pageParam), * initialPageParam: 0, * getNextPageParam: (lastPage) => lastPage.nextId, * }) * * function Comments(props: { postId: string }) { * const commentsQuery = useInfiniteQuery(() => commentsOptions(props.postId)) * * return ( * * Loading... * Error: {commentsQuery.error.message} * *
    * * {(page) => {(c) =>
  • {c.text}
  • }
    } *
    *
*
*
* ) * } * ``` */ export function infiniteQueryOptions< TQueryFnData, TError = DefaultError, TData = InfiniteData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown, >( options: ReturnType< UndefinedInitialDataInfiniteOptions< TQueryFnData, TError, TData, TQueryKey, TPageParam > >, ): ReturnType< UndefinedInitialDataInfiniteOptions< TQueryFnData, TError, TData, TQueryKey, TPageParam > > & QueryKeyWithDataTag, TError> export function infiniteQueryOptions(options: unknown) { return options }