0.9.33 • Published 6 days ago

@wundergraph/react-query v0.9.33

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 days ago

WunderGraph React Query Integration

wunderctl

This package provides a type-safe integration of React Query with WunderGraph. React Query is a data fetching library for React. With just one hook, you can significantly simplify the data fetching logic in your project. And it also covered in all aspects of speed, correctness, and stability to help you build better experiences.

Warning: Only works with WunderGraph.

Getting Started

npm install @wundergraph/react-query @tanstack/react-query

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

// 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',
    },
  ],
});

Second, run wunderctl generate to generate the code.

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

import { createHooks } from '@wundergraph/react-query';
import { createClient, Operations } from './components/generated/client';

const client = createClient(); // Typesafe WunderGraph client

export const { useQuery, useMutation, useSubscription, useUser, useFileUpload, useAuth } =
  createHooks<Operations>(client);

In your App.tsx add QueryClientProvider:

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

export default App() {
  return (
    <QueryClientProvider client={queryClient}>
      <div>...</div>
    </QueryClientProvider>
  );
}

Now you can use the hooks in your components:

useQuery

const { data, error, isLoading } = useQuery({
  operationName: 'Weather',
  input: { forCity: city },
});

useQuery (Live query)

const { data, error, isLoading, isSubscribed } = useQuery({
  operationName: 'Weather',
  input: { forCity: city },
  liveQuery: true,
});

useSubscription

const { data, error, isLoading, isSubscribed } = useSubscription({
  operationName: 'Weather',
  input: {
    forCity: 'Berlin',
  },
});

useMutation

const { data, mutate, mutateAsync } = useMutation({
  operationName: 'SetName',
});

mutate({ name: 'WunderGraph' });

await mutateAsync({ name: 'WunderGraph' });

useFileUpload

const { upload, uploadAsync, data: fileKeys, error } = useFileUpload();

upload({
  provider: 'minio',
  files: new FileList(),
});

await upload({
  provider: 'minio',
  files: new FileList(),
});

useAuth

const { login, logout } = useAuth();

login('github');

logout({ logoutOpenidConnectProvider: true });

useUser

const { data: user, error } = useUser();

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.

const queryClient = useQueryClient();

const { mutate, mutateAsync } = useMutation({
  operationName: 'SetName',
  onSuccess() {
    queryClient.invalidateQueries(queryKey({ operationName: 'Profile' }));
  },
});

mutate({ name: 'WunderGraph' });

Options

You can use all available options from React Query with the hooks.

0.9.33

6 days ago

0.9.32

1 month ago

0.9.31

2 months ago

0.9.30

3 months ago

0.9.29

4 months ago

0.9.28

5 months ago

0.9.27

5 months ago

0.9.26

6 months ago

0.9.8

9 months ago

0.9.7

9 months ago

0.9.9

9 months ago

0.9.4

10 months ago

0.9.3

10 months ago

0.9.6

10 months ago

0.9.5

10 months ago

0.9.23

7 months ago

0.9.24

6 months ago

0.9.25

6 months ago

0.9.20

7 months ago

0.9.21

7 months ago

0.9.22

7 months ago

0.8.50

10 months ago

0.9.12

9 months ago

0.9.13

9 months ago

0.9.14

8 months ago

0.8.47

11 months ago

0.9.15

8 months ago

0.9.10

9 months ago

0.9.11

9 months ago

0.9.16

8 months ago

0.8.49

10 months ago

0.9.17

7 months ago

0.8.48

11 months ago

0.9.18

7 months ago

0.9.19

7 months ago

0.9.0

10 months ago

0.9.2

10 months ago

0.9.1

10 months ago

0.8.45

11 months ago

0.8.44

12 months ago

0.8.46

11 months ago

0.8.41

12 months ago

0.8.40

12 months ago

0.8.43

12 months ago

0.8.42

12 months ago

0.8.34

12 months ago

0.8.33

1 year ago

0.8.36

12 months ago

0.8.35

12 months ago

0.8.30

1 year ago

0.8.32

1 year ago

0.8.31

1 year ago

0.8.38

12 months ago

0.8.37

12 months ago

0.8.39

12 months ago

0.8.27

1 year ago

0.8.29

1 year ago

0.8.28

1 year ago

0.8.9

1 year ago

0.8.8

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.7

1 year ago

0.8.6

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.8.23

1 year ago

0.8.22

1 year ago

0.8.25

1 year ago

0.8.24

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.8.21

1 year ago

0.7.8

1 year ago

0.8.20

1 year ago

0.7.7

1 year ago

0.8.26

1 year ago

0.8.12

1 year ago

0.8.11

1 year ago

0.8.14

1 year ago

0.8.13

1 year ago

0.8.10

1 year ago

0.8.19

1 year ago

0.8.16

1 year ago

0.8.15

1 year ago

0.8.18

1 year ago

0.8.17

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.5

1 year ago

0.5.4

1 year ago

0.5.3

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago