graphql-codegen-typescript-urql-suspense
GraphQL Code Generator plugin to generate React hooks for usage with Urql, Suspense and Error Boundaries.
⚠️ Highly experimental: subject to changes until v1.0.0 is available.
Installation
$ npm i -D graphql-codegen-typescript-urql-suspense
$ yarn add -D graphql-codegen-typescript-urql-suspense
📘 Usage
- First, setup the GraphQL Code Generator plugin:
generates:
path/to/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-urql-suspense"
config:
- Then create an Urql client with suspense enabled:
const client = createClient({
url: "/graphql",
suspense: true,
exchanges: [
],
});
- Wrap your App with the Urql Provider, Suspense and an Error Boundary:
import * as React from "react";
import { ErrorBoundary } from "react-error-boundary";
import { Provider } from "urql";
export const App = () => (
<Provider value={client}>
<ErrorBoundary FallbackComponent={ErrorView}>
<React.Suspense fallback={<LoadingView />}>{/* … */}</React.Suspense>
</ErrorBoundary>
</Provider>
);
- Use hooks!
const {
data: { project },
} = useSuspenseProjectPageQuery({
variables: { projectId: "foo" },
});
const {
data: { currentUser },
fetching,
fetchingMore,
} = useHomePageQuery({
variables: { page },
});