Clients

Solid Query client

This package provides a type-safe integration of Solid Query with WunderGraph. Solid Query is a data fetching library for Solid.js.

Installation

1
npm install @wundergraph/solid-query @tanstack/solid-query

Configuration

Before you can use the hooks, you need to modify your code generation to include the base typescript client.

1
2
3
4
5
6
7
8
9
10
11
// wundergraph.config.ts
configureWunderGraphApplication({
// ... omitted for brevity
codeGenerators: [
{
templates: [templates.typescript.client],
// the location where you want to generate the client
path: '../src/components/generated',
},
],
});

Now you can configure the hooks. Create a new file, for example lib/wundergraph.ts and add the following code:

1
2
3
4
5
6
7
import { createHooks } from '@wundergraph/solid-query';
import { createClient, Operations } from './components/generated/client';
const client = createClient(); // Typesafe WunderGraph client
export const { createQuery, createMutation, createSubscription, createFileUpload, useUser, useAuth, queryKey } =
createHooks<Operations>(client);

In your App.tsx add QueryClientProvider:

1
2
3
4
5
6
7
8
9
10
11
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
export default App() {
return (
<QueryClientProvider client={queryClient}>
<div>...</div>
</QueryClientProvider>
);
}

Usage

Now you can use the hooks in your components:

createQuery

1
2
3
4
const weather = createQuery({
operationName: 'Weather',
input: { forCity: city },
});

createQuery (Live query)

1
2
3
4
5
const weather = createQuery({
operationName: 'Weather',
input: { forCity: city },
liveQuery: true,
});

createSubscription

1
2
3
4
5
6
const weather = createSubscription({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
});

createMutation

1
2
3
4
5
6
7
const mutation = createMutation({
operationName: 'SetName',
});
mutation.mutate({ name: 'WunderGraph' });
await mutation.mutateAsync({ name: 'WunderGraph' });

createFileUpload

1
2
3
4
5
6
const uploader = createFileUpload();
uploader.upload({
provider: 'minio',
files: new FileList(),
});

useAuth

1
2
3
4
5
const { login, logout } = useAuth();
login('github');
logout({ logoutOpenidConnectProvider: true });

useUser

1
2
3
const user = useUser();
user.data;

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
const queryClient = useQueryClient();
const mutation = createMutation({
operationName: 'SetName',
onSuccess() {
queryClient.invalidateQueries(queryKey({ operationName: 'Profile' }));
},
});
mutation.mutate({ name: 'WunderGraph' });

Options

You can use all available options from Solid Query .

Was this article helpful to you?
Provide feedback

Edit this page