Blog
/
Changelog

Introducing the new Svelte Query client

cover
Dani Akash

Dani Akash

min read

Cosmo: Full Lifecycle GraphQL API Management

Are you looking for an Open Source Graph Manager? Cosmo is the most complete solution including Schema Registry, Router, Studio, Metrics, Analytics, Distributed Tracing, Breaking Change detection and more.

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 , 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 or join the conversation on 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:

1

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.

1
2
3
4
5
6
7
8
9
10
11

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

createQuery

1
2
3
4

createQuery (Live query)

1
2
3
4
5

createSubscription

1
2
3
4
5
6

createMutation

1
2
3
4
5
6
7

createFileUpload

1
2
3
4
5
6
7
8
9
10
11
12
13

getAuth

1
2
3
4
5

getUser

1

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.

1
2
3
4
5
6
7
8
9
10

SSR

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

1
2
3
4
5
6
7
8
9
10

This implementation is based on TanStack Svelte Query's prefetchQuery approach

Options

You can use all available options from Svelte Query 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 config.

Resources

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 .