Socket
Socket
Sign inDemoInstall

graphql-codegen-typescript-urql-suspense

Package Overview
Dependencies
194
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-codegen-typescript-urql-suspense

GraphQL Code Generator plugin to generate React hooks for usage with Urql, Suspense and Error Boundaries


Version published
Maintainers
1
Created

Readme

Source

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.

mit licence npm version

Installation

$ npm i -D graphql-codegen-typescript-urql-suspense
# --- or ---
$ yarn add -D graphql-codegen-typescript-urql-suspense

📘 Usage

  1. First, setup the GraphQL Code Generator plugin:
# codegen.yml

generates:
  path/to/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-urql-suspense" # replaces "typescript-urql"
    config:
      # The config is similar as typescript-urql, without `withComponent` and `withHooks`
      # see: https://www.graphql-code-generator.com/plugins/typescript/typescript-urql
      # …
  1. Then create an Urql client with suspense enabled:
const client = createClient({
  url: "/graphql",
  suspense: true,
  exchanges: [
    // …
  ],
});
  1. 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>
);
  1. Use hooks!
// Suspense queries:
// - The fetching state is delegated to the closest React.Suspense
// - The error state to the closest Error Boundary
const {
  data: { project }, // data cannot be undefined here, fetching doesn't exists
} = useSuspenseProjectPageQuery({
  variables: { projectId: "foo" },
  // Note that the "pause" param is not handled for now
});

// But also more basic queries:
// - The error state to the closest Error Boundary
const {
  data: { currentUser },
  fetching, // initial fetching
  fetchingMore, // fetching more (ex: for pagination)
} = useHomePageQuery({
  variables: { page },
  // Note that the "pause" param is not handled for now
});

Keywords

FAQs

Last updated on 27 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc