Blog
/
Changelog

Introducing the new React Query client

cover
Eelco Wiersma

Eelco Wiersma

4min read

WunderGraph Cloud Waitlist

Before we get into the blog post. WunderGraph Cloud is being released very soon. We’re looking for Alpha and Beta testers for WunderGraph Cloud.

Testers will receive access to WunderGraph Cloud and 3 months Cloud Pro for free.

After much requests we are excited to announce the release of the new WunderGraph React Query client. This new client is built on top of TanStack React Query , a powerful data fetching library for React. It allows you to consume WunderGraph queries, mutations and subscriptions fully typesafe with React Query.

We took all the lessons learned from building our new SWR integration and applied them to this new client. This means that you can use the same features as with the SWR integration, like optimistic updates, invalidating queries, and more with a similar easy to use and typesafe API.

Currently we only support React, but thanks to TanStack we are now able to add support for other frameworks like Vue, Solid.js and soon Svelte. We would love to have your help in building these integrations. Check out the React Query source code to get started. ❤️

What's new

Let's dive a bit into what's new and how some of the React Query features work with WunderGraph.

Queries

1
2
3
4
5
6
const { data, error } = useQuery({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
})

Conditionally fetching data:

1
2
3
4
5
6
7
8
9
const { data: user } = useUser()
const { data, error, refetch } = useQuery({
operationName: 'ProtectedWeather',
input: {
forCity: 'Berlin',
},
enabled: !!user,
})

Note that refetch is slightly different from SWR, instead mutate we can call refetch to refetch a specific query.

Turn queries into live queries, live queries are refetched on a interval on the WunderGraph server.

1
2
3
4
5
6
7
const { data, error } = useQuery({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
liveQuery: true,
})

Subscriptions

Build realtime apps with subscriptions.

1
2
3
4
5
6
const { data, error, isLoading, isSubscribed } = useSubscription({
operationName: 'Countdown',
input: {
from: 100,
},
})

Mutations

1
2
3
4
5
6
7
8
const { data, error, mutate, mutateAsync } = useMutation({
operationName: 'SetName',
})
mutate({ name: 'Eelco' })
// Async mutate
const result = await mutateAsync({ name: 'Eelco' })

Invalidating queries

Let's say we have a query that fetches the current user's profile in one component and we have a form that updates the profile. We can add an onSuccess handler to the mutation that calls queryClient.invalidateQueries on the GetProfile query and trigger a refetch and update the internal React Query cache.

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
26
27
const Profile = () => {
const { data, error } = useQuery({
operationName: 'GetProfile',
})
return <div>{data?.getProfile.name}</div>
}
const FormComponent = () => {
const queryClient = useQueryClient();
const { data, error, mutate } = useMutation({
operationName: 'UpdateProfile',
onSuccess() {
// invalidate the query
queryClient.invalidateQueries(queryKey({ operationName: 'GetProfile' }));
},
})
const onSubmit = (event) => {
e.preventDefault();
const data = new FormData(event.target);
mutate(data)
}
return <form onSubmit={onSubmit}><input name="name" /><button type="submit">Save></button></form>
}

Now we could even make this fully optimistic by updating the GetProfile cache instead and then refetching it, it would look something like this:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const FormComponent = () => {
const queryClient = useQueryClient();
const { data, error, mutate } = useMutation({
operationName: 'UpdateProfile',
onMutate: async (data) => {
const key = queryKey({ operationName: 'GetProfile' })
// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await queryClient.cancelQueries(key)
// Snapshot the previous value
const previousProfile = queryClient.getQueryData<Profile>(key)
// Optimistically update to the new value
if (previousProfile) {
queryClient.setQueryData<Profile>(key, {
...previousProfile,
...data
})
}
return { previousProfile }
},
// If the mutation fails,
// use the context returned from onMutate to roll back
onError: (err, variables, context) => {
if (context?.previousProfile) {
queryClient.setQueryData<Profile>(queryKey({ operationName: 'GetProfile' }), context.previousProfile)
}
},
// Always refetch after error or success:
onSettled: () => {
queryClient.invalidateQueries(queryKey({ operationName: 'GetProfile' }))
},
})
const onSubmit = (event) => {
e.preventDefault();
const data = new FormData(event.target);
mutate(data)
}
return <form onSubmit={onSubmit}><input name="name" /><button type="submit">Save></button></form>
}

Check out the reference and example app below to learn more about the new React Query integration.

Resources

Summary

You can now easily integrate WunderGraph with React Query and start building end-to-end typesafe applications with WunderGraph. Thanks go out to Tanstack for making this great data fetching library.

We would love to know more about your experience with React Query and looking forward to seeying what you build with it. Share it in the comments below or come join us on our Discord server .

WunderGraph Cloud Early Access

Are you ready for the next generation of Serverless Infraless API Development? Join the waitlist for WunderGraph Cloud Early Access and be the first to try it out.