Introducing the new Svelte Query client
Tags: javascript, webdev, programming, frontend
Published: 2023-05-11 13:00:00


![Svelte FTW](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzs2zpu3vmquubowfavr.png)


We're thrilled to announce the official launch of our **Svelte Query Client** with Tanstack Svelte Query support for the Svelte ecosystem! 🚀🎉

This new client is built on top of [TanStack Svelte Query](https://tanstack.com/query/v4/docs/svelte/overview), and seamlessly integrates the power of WunderGraph into the Svelte ecosystem. Enjoy fully typesafe querying, mutating, and subscribing to your WunderGraph API in Svelte!

🔥 _Remember, this release is still in beta!_ 🔥 Your feedback is invaluable to us! If you have any suggestions or issues, please don't hesitate to [open an issue on Github](https://github.com/wundergraph/wundergraph/issues/new/choose) or [join the conversation on Discord](https://wundergraph.com/discord)!

Let's build amazing things together with Svelte and WunderGraph! 🌟

Let's go through quickly how to set it up and how it works.

## Installation

Install the Svelte Query client:

```bash
npm install @wundergraph/svelte-query @tanstack/svelte-query
```

## Configuration

Svelte Query client provide a set of utilities to connect your APIs with svelte. Before we can use these functions, you need to modify your code generation to include the base typescript client.

```typescript
// wundergraph.config.ts
configureWunderGraphApplication({
  // ... omitted for brevity
  codeGenerators: [
    {
      templates: [templates.typescript.client],
      // the location where you want to generate the client
      path: '../src/generated',
    },
  ],
})
```

Run `wunderctl generate` to generate the code.

> In SvelteKit, you'll have to keep the generated files under the `src/` directory

Now you can configure the svelte query functions. Create a new file, for example `src/lib/wundergraph.ts` and add the following code:

```ts
import { createSvelteClient } from '@wundergraph/svelte-query'
import { createClient } from '../generated/client'
import type { Operations } from '../generated/client'

const client = createClient() // Typesafe WunderGraph client

// These utility functions needs to be imported into your app
export const {
  createQuery,
  createFileUpload,
  createMutation,
  createSubscription,
  getAuth,
  getUser,
  queryKey,
} = createSvelteClient<Operations>(client)
```

Now, in your svelte layout setup Svelte Query Provider such that it is always wrapping above the rest of the app.

```svelte
<script>
	import Header from './Header.svelte';
	import { browser } from '$app/environment'
	import './styles.css';
	import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'

	const queryClient = new QueryClient({
		defaultOptions: {
			queries: {
				enabled: browser,
			},
		},
	})
</script>

<div class="app">
  <QueryClientProvider client={queryClient}>
    <slot />
  </QueryClientProvider>
</div>
```

Now you can use svelte-query to call your wundergraph operations!

```svelte
<script lang="ts">
	import { createQuery } from '../lib/wundergraph';

	const query = createQuery({
		operationName: "Starwars",
	})
</script>

<div class="counter">
	<h1>Simple Query</h1>
	<div>
		{#if $query.isLoading}
			Loading...
		{/if}
		{#if $query.error}
			An error has occurred:
			{$query.error.message}
		{/if}
		{#if $query.isSuccess}
      <div>
        <pre>{JSON.stringify($query.data.starwars_allPeople)}</pre>
      </div>
    {/if}
	</div>
</div>
```

### createQuery

```ts
createQuery({
  operationName: 'Weather',
  input: { forCity: city },
})
```

### createQuery (Live query)

```ts
createQuery({
  operationName: 'Weather',
  input: { forCity: city },
  liveQuery: true,
})
```

### createSubscription

```ts
createSubscription({
  operationName: 'Weather',
  input: {
    forCity: 'Berlin',
  },
})
```

### createMutation

```ts
const mutation = createMutation({
  operationName: 'SetName',
})

$mutation.mutate({ name: 'WunderGraph' })

await $mutation.mutateAsync({ name: 'WunderGraph' })
```

### createFileUpload

```ts
const fileUploader = createFileUpload()

$fileUploader.upload({
  provider: 'minio',
  files: new FileList(),
})

await $fileUploader.upload({
  provider: 'minio',
  files: new FileList(),
})

$fileUploader.fileKeys // files that have been uploaded
```

### getAuth

```ts
const auth = getAuth()

$auth.login('github')

$auth.logout({ logoutOpenidConnectProvider: true })
```

### getUser

```ts
const userQuery = getUser()
```

### queryKey

You can use the `queryKey` helper function to create a unique key for the query in a typesafe way. This is useful if you want to invalidate the query after mutating.

```ts
const queryClient = useQueryClient()

const mutation = createMutation({
  operationName: 'SetName',
  onSuccess() {
    queryClient.invalidateQueries(queryKey({ operationName: 'Profile' }))
  },
})

$mutation.mutate({ name: 'WunderGraph' })
```

## SSR

If you are working with SvelteKit, this package provides `prefetchQuery` utility to help with SSR

```ts
export const load: PageLoad = async ({ parent }) => {
  const { queryClient } = await parent()

  await prefetchQuery(
    {
      operationName: 'Dragons',
    },
    queryClient
  )
}
```

This implementation is based on TanStack Svelte Query's [prefetchQuery](https://tanstack.com/query/v4/docs/svelte/ssr#using-prefetchquery) approach

## Options

You can use all available options from [Svelte Query](https://tanstack.com/query/latest/docs/svelte/overview) with the generated functions.
Due to the fact that we use the operationName + variables as **key**, you can't use the `key` option as usual.
In order to use conditional-fetching you can use the `enabled` option.

You can configure the utilities globally by using the Svelte Query's [QueryClient](https://tanstack.com/query/v4/docs/react/reference/QueryClient) config.

## Resources

- [Svelte Query Client Reference](https://docs.wundergraph.com/docs/clients-reference/svelte-query)
- [Vite + Svelte Example](https://docs.wundergraph.com/docs/examples/vite-svelte)
- [SvelteKit with SSR Example](https://docs.wundergraph.com/docs/examples/sveltekit)
- [TanStack Svelte Query](https://tanstack.com/query/v4/docs/svelte/overview)

## Summary

Svelte Query is now officially supported by WunderGraph.

We would love to know more about your experience with Svelte. Do you use Vite, or SvelteKit? How often does your projects require SSR? What do you like about it?

Share it in the comments below or come join us on our [Discord server](https://wundergraph.com/discord).