Skip to main content
This guide will walk you through creating your first Svelte Query application, covering queries, mutations, and common patterns.

Prerequisites

Before starting, make sure you have:
  • Svelte 5.25.0 or higher installed
  • A Svelte or SvelteKit project set up
  • @tanstack/svelte-query installed
If you haven’t installed Svelte Query yet, see the Installation Guide.

Your First Query

1

Set up the QueryClient

First, create a QueryClient and wrap your app with QueryClientProvider in your root layout:
+layout.svelte
2

Create your first query

Now create a component that fetches data using createQuery:
Posts.svelte
The createQuery function takes an accessor function () => options that returns the query configuration. This allows the query to react to changes in dependencies.
3

Understanding query states

Svelte Query provides several state properties to handle different scenarios:
Key states:
  • isPending - Query has no data yet (initial load)
  • isError - Query failed
  • isSuccess - Query succeeded
  • isFetching - Query is fetching (includes background refetches)
  • data - The actual query data
  • error - The error object if query failed

Dynamic Queries

Queries can depend on reactive variables. The query automatically refetches when dependencies change:
PostDetail.svelte
When postId changes, the query key ['post', postId] changes, triggering an automatic refetch with the new ID.

Mutations

Use createMutation to create, update, or delete data:
CreatePost.svelte

Mutation States

Mutations provide similar state properties:
  • isPending - Mutation is in progress
  • isError - Mutation failed
  • isSuccess - Mutation succeeded
  • data - The mutation result data
  • error - The error object if mutation failed
  • mutate() - Function to trigger the mutation
  • mutateAsync() - Promise-based mutation function

Query Options

Customize query behavior with various options:
All time values are in milliseconds. Use staleTime to control when data is considered “stale” and needs refetching.

Infinite Queries

For paginated or infinite scroll data, use createInfiniteQuery:
InfinitePosts.svelte

Infinite Query Properties

  • data.pages - Array of all fetched pages
  • data.pageParams - Array of all page parameters
  • hasNextPage - Whether more pages are available
  • hasPreviousPage - Whether previous pages are available
  • fetchNextPage() - Load the next page
  • fetchPreviousPage() - Load the previous page
  • isFetchingNextPage - Next page is loading
  • isFetchingPreviousPage - Previous page is loading

Query Invalidation

Invalidate queries to force them to refetch:

Using queryOptions Helper

For better type safety and reusability, use the queryOptions helper:
queries.ts
Then use it in your components:
Posts.svelte
The queryOptions helper provides better type inference and makes it easier to share query configurations across components.

Best Practices

1

Use meaningful query keys

Query keys should describe the data uniquely:
2

Handle loading and error states

Always provide feedback for pending and error states:
3

Configure staleTime appropriately

Set staleTime based on how often your data changes:
4

Invalidate queries after mutations

Keep your UI in sync by invalidating related queries:

Common Patterns

Dependent Queries

Execute a query only after another query succeeds:

Optimistic Updates

Update UI immediately before server confirmation:

Prefetching

Prefetch data before it’s needed:

Next Steps

1

TypeScript Integration

Learn how to get full type safety with TypeScript.TypeScript Guide →
2

DevTools

Install and use the Svelte Query DevTools for debugging.DevTools Setup →
3

Advanced Guides

Explore advanced patterns like SSR, persisting, and more.Guides →