--- id: createQuery title: createQuery --- ## Call Signature ```ts function createQuery(options: Accessor>, queryClient?: Accessor): CreateQueryResult; ``` Defined in: [packages/svelte-query/src/createQuery.ts:77](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/createQuery.ts#L77) Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key. The query runs when the options call for it — `enabled: false` skips the initial fetch. ### Type Parameters #### TQueryFnData `TQueryFnData` = `unknown` #### TError `TError` = `Error` #### TData `TData` = `TQueryFnData` #### TQueryKey `TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[] ### Parameters #### options [`Accessor`](../type-aliases/Accessor.md)\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use — everything you can pass to `createQuery`, wrapped in an [Accessor](../type-aliases/Accessor.md) so options can be reactive. #### queryClient? [`Accessor`](../type-aliases/Accessor.md)\<[`QueryClient`](../classes/QueryClient.md)\> Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will be used. ### Returns [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`TData`, `TError`\> The current query result. `status` is `pending` if there is no cached data to display, `error` if the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience. ### See [queryOptions](queryOptions.md) to share these options between `createQuery` and imperative APIs like `queryClient.query`. ### Examples ```svelte {#if query.status === 'pending'} Loading... {:else if query.status === 'error'} Error: {query.error.message} {:else}
    {#each query.data as post (post.id)}
  • {post.title}
  • {/each}
{/if} ``` The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you: ```svelte {#if query.isPending} Loading... {:else if query.isError} Error: {query.error.message} {:else}
    {#each query.data as post (post.id)}
  • {post.title}
  • {/each}
{/if} ``` ## Call Signature ```ts function createQuery(options: Accessor>, queryClient?: Accessor): DefinedCreateQueryResult; ``` Defined in: [packages/svelte-query/src/createQuery.ts:128](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/createQuery.ts#L128) Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key. The query runs when the options call for it — `enabled: false` skips the initial fetch. This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`. ### Type Parameters #### TQueryFnData `TQueryFnData` = `unknown` #### TError `TError` = `Error` #### TData `TData` = `TQueryFnData` #### TQueryKey `TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[] ### Parameters #### options [`Accessor`](../type-aliases/Accessor.md)\<[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use — everything you can pass to `createQuery`, with `initialData` set, wrapped in an [Accessor](../type-aliases/Accessor.md) so options can be reactive. #### queryClient? [`Accessor`](../type-aliases/Accessor.md)\<[`QueryClient`](../classes/QueryClient.md)\> Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will be used. ### Returns [`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`TData`, `TError`\> The current query result, typed so that `status` is `success` — or `error` if a fetch attempt fails while keeping the existing data (`status` never resolves to `pending` in this overload's type, since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience. ### See [queryOptions](queryOptions.md) to share these options between `createQuery` and imperative APIs like `queryClient.query`. ### Example ```svelte {#if query.isError} Error: {query.error.message} {/if}
    {#each query.data as post (post.id)}
  • {post.title}
  • {/each}
``` ## Call Signature ```ts function createQuery(options: Accessor>, queryClient?: Accessor): CreateQueryResult; ``` Defined in: [packages/svelte-query/src/createQuery.ts:254](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/createQuery.ts#L254) ### Type Parameters #### TQueryFnData `TQueryFnData` #### TError `TError` = `Error` #### TData `TData` = `TQueryFnData` #### TQueryKey `TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[] ### Parameters #### options [`Accessor`](../type-aliases/Accessor.md)\<[`CreateQueryOptions`](../type-aliases/CreateQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> The [CreateQueryOptions](../type-aliases/CreateQueryOptions.md) to use — everything you can pass to `createQuery`, wrapped in an [Accessor](../type-aliases/Accessor.md) so options can be reactive. #### queryClient? [`Accessor`](../type-aliases/Accessor.md)\<[`QueryClient`](../classes/QueryClient.md)\> Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will be used. ### Returns [`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`TData`, `TError`\> The current query result. `status` is `pending` if there is no cached data to display, `error` if the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience. ### See [queryOptions](queryOptions.md) to share these options between `createQuery` and imperative APIs like `queryClient.query`. ### Examples `select` derives whatever `data` a component needs from the cached value, without changing what's actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`: ```svelte {#if query.isPending} Loading... {:else if query.isError} Error: {query.error.message} {:else} {query.data} posts {/if} ``` A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled: ```svelte {#if postId == null} Select a post {:else if query.isLoading} Loading... {:else if query.isError} Error: {query.error.message} {:else}

{query.data?.title}

{/if} ``` Seeding a detail query from an already-cached list, to skip the loading state: ```svelte {#if query.isError} Error: {query.error.message} {/if}

{query.data?.title}

``` Paginated data, keeping the previous page's data visible while the next page loads: ```svelte {#if query.isError} Error: {query.error.message} {/if}
    {#each query.data ?? [] as post (post.id)}
  • {post.title}
  • {/each}
```