|
1 |
| -# webRPC react-query client |
| 1 | +@webrpc/react-query -- a react-query adapter for webrpc |
| 2 | +======================================================= |
| 3 | + |
| 4 | +Enjoy the RPC code-gen ergonomics + type-safety benefits of webrpc, with the comfort |
| 5 | +of react-query from your React apps :) |
| 6 | + |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +From your webapps: `npm install @webrpc/react-query` |
| 11 | + |
| 12 | +As well, make sure you install `@tanstack/react-query` and `react` in your app, which are |
| 13 | +peer-dependencies of this library. |
| 14 | + |
| 15 | + |
| 16 | +## Example |
| 17 | + |
| 18 | +Please see a full example project with both server and client [here](./example). |
| 19 | + |
| 20 | +To run the example: |
| 21 | + |
| 22 | +1. git clone this repo |
| 23 | +2. `cd example/webapp` |
| 24 | +3. `pnpm install` |
| 25 | +4. Start the server -- in one terminal: `make run-server` |
| 26 | +5. Start the webapp -- in another terminal: `make run-webapp` |
| 27 | +6. Open the webapp at http://localhost:4444 and open your browser console |
| 28 | + |
2 | 29 |
|
3 |
| -Server synchronization made easy & type-safe! |
4 | 30 | ## How to use
|
5 | 31 |
|
6 |
| -First, you'll need an api contract, ideally in the webRPC format. |
7 |
| -Then create an instance of your contract and pass it as an argument to the WebRPCClient constructor. |
| 32 | +First, you'll need a webrpc schema definition either in JSON or RIDL. |
| 33 | + |
| 34 | +Then create an instance of your RPC client and pass it as an argument to the WebRpcClient constructor. |
8 | 35 | It should look something like this:
|
9 | 36 |
|
10 | 37 | ```ts
|
11 |
| -const ContractInstance = new Chat('hostname', customFetch) |
12 |
| -export const client = new WebRpcClient(ContractInstance) |
| 38 | +import { Example } from './client.gen' |
| 39 | +import { WebRpcQueryClient } from '@webrpc/react-query' |
| 40 | + |
| 41 | +const rpc = new Example('http://localhost:4242', fetch) |
| 42 | +const client = new WebRpcQueryClient(rpc) |
13 | 43 | ```
|
14 | 44 |
|
15 | 45 | Import `client` where you need to make your API calls and let type inference guide your way!
|
16 | 46 |
|
| 47 | + |
17 | 48 | ## Differentiating queries and mutations
|
18 | 49 |
|
19 | 50 | If you want to make the distinction between a query and a mutation even clearer, you can define custom _query prefixes_.
|
20 | 51 | You do so by adding a generic type to your `WebRpcClient` instance.
|
21 | 52 |
|
22 | 53 | ```ts
|
23 |
| -const ContractInstance = new Chat('hostname', fetch) |
24 |
| -const client = new WebRpcClient<typeof ContractInstance, ['get', 'list']>( |
25 |
| - ContractInstance, |
26 |
| -) |
| 54 | +import { Example } from './client.gen' |
| 55 | +import { WebRpcQueryClient } from '@webrpc/react-query |
| 56 | + |
| 57 | +const rpc = new Example('http://localhost:4242', fetch) |
| 58 | +const client = new WebRpcQueryClient<typeof rpc, ['get', 'list']>(rpc) |
27 | 59 | ```
|
28 | 60 |
|
29 | 61 | With this configuration, you can only use `client.useQuery` hook with paths that start with either `'get'` or `'list'`.
|
30 |
| -Any other method from your contract will be considered a _mutation_. If you choose not to provide _query prefixes_, you will be able to call both `client.useQuery` and `client.useMutation` with any path from your contract. |
| 62 | + |
| 63 | +Any other method from your contract will be considered a _mutation_. If you choose not to provide _query prefixes_, you will be able to call both `client.useQuery` and `client.useMutation` with any path from your contract. |
| 64 | + |
| 65 | + |
| 66 | +## Local dev |
| 67 | + |
| 68 | +You can update `example/webapp/package.json" package to `"@webrpc/react-query": "workspace:../../"` |
| 69 | +which will use the build from the local repo. |
| 70 | + |
| 71 | + |
| 72 | +## Credits |
| 73 | + |
| 74 | +Thank you to @vojoup from the github.com/golang-cz team for the idea and original implementation of this library :) |
| 75 | + |
| 76 | + |
| 77 | +## LICENSE |
| 78 | + |
| 79 | +Licensed under [MIT License](./LICENSE) |
0 commit comments