Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: react query hooks * chore: update tests and changelog * feat: improve tests * chore: lock file * feat: fix subscription test * docs: update readme * docs: added client references * docs: update nextjs client docs * feat: add testapp react-query examples * feat: add async upload * chore: update test app * chore: add to comments * fix: update test * chore: removed unused files * feat: init react-query example * feat: add react-query fetch support * feat: add tailwind css * feat: add nav * feat: add welcome message * fix: use object id as key instead of index * chore: update example * feat: add type tests * chore: update gen * docs: add missing docs * chore: move type tests into packages * chore: add readme to vite example * chore: update gen files Co-authored-by: “Kaleemullah” <“nixamani5@gmail.com”>
- Loading branch information
1 parent
ce2f130
commit c5769a4
Showing
63 changed files
with
3,289 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
docs-website/src/pages/docs/clients-reference/nextjs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
--- | ||
title: Next.js Client | ||
pageTitle: WunderGraph - Next.js | ||
description: | ||
--- | ||
|
||
The Next.js client uses [SWR](/docs/clients-reference/swr) under the hood. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i @wundergraph/nextjs swr@2.0.0-rc.0 | ||
``` | ||
|
||
## Configuration | ||
|
||
Add `NextJsTemplate` to your WunderGraph configuration: | ||
|
||
```typescript | ||
import { NextJsTemplate } from '@wundergraph/nextjs/dist/template' | ||
|
||
// wundergraph.config.ts | ||
configureWunderGraphApplication({ | ||
// ... your config | ||
codeGenerators: [ | ||
{ | ||
templates: [...templates.typescript.all], | ||
}, | ||
{ | ||
templates: [new NextJsTemplate()], | ||
path: '../components/generated', | ||
}, | ||
], | ||
}) | ||
``` | ||
|
||
## Hooks | ||
|
||
### useQuery | ||
|
||
This hook accepts most [useSWR Options](https://swr.vercel.app/docs/options) except for key and fetcher. | ||
|
||
```typescript | ||
const { data, error, isValidating, isLoading, mutate } = useQuery({ | ||
operationName: 'Weather', | ||
input: { | ||
forCity: 'Berlin', | ||
}, | ||
enabled: true, | ||
}) | ||
``` | ||
|
||
Calling `mutate` will invalidate and refetch the query. | ||
|
||
```typescript | ||
const { data, mutate } = useQuery({ | ||
operationName: 'Weather', | ||
input: { | ||
forCity: 'Berlin', | ||
}, | ||
}) | ||
|
||
mutate() | ||
``` | ||
|
||
### useQuery (Live query) | ||
|
||
You can turn any query into a live query by adding the `liveQuery` option. | ||
|
||
```typescript | ||
const { data, error, isLoading, isSubscribed, mutate } = useQuery({ | ||
operationName: 'Weather', | ||
input: { | ||
forCity: 'Berlin', | ||
}, | ||
liveQuery: true, | ||
}) | ||
``` | ||
|
||
### useMutation | ||
|
||
This hook accepts most [useSWRMutation Options](https://swr.vercel.app/docs/options) except for key and fetcher. | ||
|
||
```typescript | ||
const { data, error, trigger } = useMutation({ | ||
operationName: 'SetName', | ||
}) | ||
|
||
await trigger({ | ||
name: 'test', | ||
}) | ||
|
||
trigger( | ||
{ | ||
name: 'test', | ||
}, | ||
{ | ||
throwOnError: false, | ||
} | ||
) | ||
``` | ||
|
||
### useSubscription | ||
|
||
```typescript | ||
const { data, error, isLoading, isSubscribed } = useSubscription({ | ||
operationName: 'Weather', | ||
input: { | ||
forCity: 'Berlin', | ||
}, | ||
enabled: true, | ||
onSuccess(data, key, config) { | ||
// called when the subscription is established. | ||
}, | ||
onError(data, key, config) { | ||
// called when the subscription failed to establish. | ||
}, | ||
}) | ||
``` | ||
|
||
### useAuth | ||
|
||
```typescript | ||
const { login, logout } = useAuth() | ||
|
||
login('github') | ||
|
||
logout({ logoutOpenidConnectProvider: true }) | ||
``` | ||
|
||
### useUser | ||
|
||
This hook accepts most [useSWR Options](https://swr.vercel.app/docs/options) except for key and fetcher. | ||
|
||
```typescript | ||
const { data, error, isLoading } = useUser() | ||
``` | ||
|
||
## File upload | ||
|
||
This hook accepts most [useSWRMutation Options](https://swr.vercel.app/docs/options) except for key and fetcher. | ||
|
||
```typescript | ||
const { upload, data, error } = useFileUpload() | ||
|
||
upload( | ||
{ | ||
provider: 'minio', | ||
files: [new File([''], 'test.txt')], | ||
}, | ||
{ | ||
throwOnError: false, | ||
} | ||
) | ||
``` | ||
|
||
## SSR | ||
|
||
Wrapping the App or Page in `withWunderGraph` will make sure that Server Side Rendering (SSR) works, | ||
that's it. | ||
|
||
```typescript | ||
import { NextPage } from 'next' | ||
import { useQuery, withWunderGraph } from '../components/generated/nextjs' | ||
|
||
const Home: NextPage = () => { | ||
const dragons = useQuery({ operationName: 'Dragons' }) | ||
return <div>{JSON.stringify(dragons)}</div> | ||
} | ||
export default withWunderGraph(Home) | ||
``` | ||
|
||
## Global Configuration | ||
|
||
You can configure the hooks globally by using the [SWRConfig](https://swr.vercel.app/docs/global-configuration) context. | ||
|
||
In case the context configuration isn't working, it's likely due to multiple versions of SWR being installed or due to how PNPM or Yarn PnP link packages. | ||
To resolve this you can import SWR directly from `@wundergraph/nextjs` to make sure the same instance is used. | ||
|
||
```ts | ||
import { SWRConfig, useSWRConfig } from '@wundergraph/nextjs' | ||
``` |
Oops, something went wrong.
c5769a4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
wundergraph-docs – ./
wundergraph-docs.vercel.app
docs.wundergraph.com
wundergraph-docs-git-main-wunder-graph.vercel.app
wundergraph-docs-wunder-graph.vercel.app