npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

codegen-apollo-suspense

v1.1.0

Published

Generate apollo data resolvers (with cache) for use with react suspense

Downloads

7

Readme

GraphQL Codegen - Apollo React Suspense

This is a plugin for graphl codegen, it is designed for use with apollo along with React Suspense.

What is this?

This creates a data repository for each query and mutation in your application (subscriptions are not supported yet).
By using these repositories you can use suspense with apollo or migrate your app to use suspense.

Install

yarn add -D codegen-apollo-suspense

Setup

Follow the official setup guide if your are not already using codegen. Then update your config to use this package.

schema: https://graphql-weather-api.herokuapp.com
overwrite: true
documents: src/**/*.graphql
generates:
  src/generated/schema.tsx:
    - "typescript"
    - "typescript-operations"
-   - typescript-react-apollo
+   - codegen-apollo-suspense

In the setup example we have this query.

query GetWeather($city: String!, $country: String!) {
  getCityByName(name: $city, country: $country, config: {
    units: metric,
    lang: en,
  }) {
    id
    name
    country
    weather {
      summary {
        title
        description
        icon
      }
      temperature {
        actual
        feelsLike
      }
      wind {
        speed
      }
      clouds {
        all
        visibility
        humidity
      }
      timestamp
    }
  }
}

Suspense can now be used in the application.

Here is sn example with hooks.

import React from 'react';
import { useGetWeatherQuery } from '../generated/schema';

const Temperature: React.FC = () => {
    const { data, loading, error } = useGetWeatherQuery({
        variables: {
            country: 'au',
            city: 'melbourne',
        }
    });

    if (loading) return <div>Loading</div>;
    if (error) throw new Error(error.message);

    return <div>{ data.getCityByName?.weather?.temperature?.actual }</div>);
}

const Widget: React.FC = () => (
    <ErrorBoundary fallback={ErrorPanel}>
        <Temperature />
    </ErrorBoundary>
)

note: the throw is to just make the examples more comparable

Now with suspense.

import React, { Suspense } from 'react';
import { useGetWeatherSuspenseQuery } from '../generated/schema';

const Temperature: React.FC = () => {
    const data = useGetWeatherSuspenseQuery({
        variables: {
            country: 'au',
            city: 'melbourne',
        }
    });

    return <div>{ data.getCityByName?.weather?.temperature?.actual }</div>);
}

const Widget: React.FC = () => (
    <ErrorBoundary fallback={ErrorPanel}>
        <Suspense fallback={<div>Loading</div>}>
            <Temperature />
        </Suspense>
    </ErrorBoundary>
)

Using with the React Apollo plugin

To ensure that duplicate documents are not created please add

schema: https://graphql-weather-api.herokuapp.com
overwrite: true
documents: src/**/*.graphql
generates:
  src/generated/schema.tsx:
    - "typescript"
    - "typescript-operations"
+   - typescript-react-apollo
   - codegen-apollo-suspense
+ config:
+  useExternalDocument: true