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

next-with-apollo

v5.3.0

Published

Apollo HOC for Next.js

Downloads

67,531

Readme

next-with-apollo

Actions Status

Apollo HOC for Next.js.

For Next v9 use the latest version.

For Next v6-v8 use the version 3.4.0.

For Next v5 and lower go here and use the version 1.0.

How to use

Install the package with npm:

npm install next-with-apollo

or with yarn:

yarn add next-with-apollo

Create the HOC using a basic setup and apollo-boost:

// lib/withApollo.js
import withApollo from 'next-with-apollo';
import ApolloClient, { InMemoryCache } from 'apollo-boost';
import { ApolloProvider } from '@apollo/react-hooks';

export default withApollo(
  ({ initialState }) => {
    return new ApolloClient({
      uri: 'https://mysite.com/graphql',
      cache: new InMemoryCache().restore(initialState || {})
    });
  },
  {
    render: ({ Page, props }) => {
      return (
        <ApolloProvider client={props.apollo}>
          <Page {...props} />
        </ApolloProvider>
      );
    }
  }
);

Note: apollo-boost is used in this example because is the fastest way to create an ApolloClient, but is not required.

Note: If using react-apollo, you will need to import the ApolloProvider from react-apollo instead of @apollo/react-hooks.

Now let's use lib/withApollo.js in one of our pages:

// pages/index.js
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';
import withApollo from '../lib/withApollo';
// import { getDataFromTree } from '@apollo/react-ssr';

const QUERY = gql`
  {
    title
  }
`;

const Index = () => {
  const { loading, data } = useQuery(QUERY);

  if (loading || !data) {
    return <h1>loading...</h1>;
  }
  return <h1>{data.title}</h1>;
};

export default withApollo(Index);

// You can also override the configs for withApollo here, so if you want
// this page to have SSR (and to be a lambda) for SEO purposes and remove
// the loading state, uncomment the import at the beginning and this:
//
// export default withApollo(Index, { getDataFromTree });

Now your page can use anything from @apollo/react-hooks or react-apollo. If you want to add Apollo in _app instead of per page, go to Using _app.

withApollo API

withApollo receives 2 parameters, the first one is a function that returns the Apollo Client, this function receives an object with the following properties:

  • ctx - This is the context object sent by Next.js to the getInitialProps of your page. It's only available for SSR, in the client it will be undefined
  • initialState - If getDataFromTree is sent, this will be the initial data required by the queries in your page, otherwise it will be undefined
  • headers - This is ctx.req.headers, in the client it will be undefined
  • router - This is the router object sent by getInitialProps. Only available in the server and when using getInitialProps

The second, optional parameter, received by withApollo, is an object with the following props:

  • render - A function that receives an object ({ Page, props }) with the current Page Component to be rendered, and its props. It can be used to wrap your pages with <ApolloProvider>. It's optional
  • getDataFromTree - implementation of getDataFromTree, defaults to undefined. It's recommended to never set this prop, otherwise the page will be a lambda without Automatic Static Optimization
  • onError - A function that will be called if getDataFromTree encounters errors. If not supplied errors will be silently ignored. It will be called with 2 parameters:

Using getInitialProps

Pages with getInitialProps can access the Apollo Client like so:

Page.getInitialProps = ctx => {
  const apolloClient = ctx.apolloClient;
};

Next.js applies very good optimizations by default, including Automatic Static Optimization, and as long as the getDataFromTree config is not added, your pages will always be static and can be served directly from a CDN, instead of having a serverless function being executed for every new request, which is also more expensive.

If your page has getDataFromTree to remove the loading states of Apollo Queries, you should consider handling the loading states by yourself, fetching all queries per request and before sending the initial HTML will slow down the first render, and the user may end up waiting a long time without any feedback.

Using _app

If you want to add Apollo to all pages, you can use pages/_app.js, like so:

import withApollo from 'next-with-apollo';
import { ApolloProvider } from '@apollo/react-hooks';
import ApolloClient, { InMemoryCache } from 'apollo-boost';

const App = ({ Component, pageProps, apollo }) => (
  <ApolloProvider client={apollo}>
    <Component {...pageProps} />
  </ApolloProvider>
);

export default withApollo(({ initialState }) => {
  return new ApolloClient({
    uri: 'https://mysite.com/graphql',
    cache: new InMemoryCache().restore(initialState || {})
  });
})(App);

It's better to add Apollo in every page instead if you have pages that don't need Apollo.

To access Apollo Client in each page's getInitialProps, add getInitialProps to App like so:

import App from 'next/app';

MyApp.getInitialProps = async appContext => {
  const appProps = await App.getInitialProps(appContext);
  return { ...appProps };
};

If you either add the getDataFromTree config or getInitialProps, it will turn all pages into lambdas and disable Automatic Static Optimization.

Migration guide

from 4.3.0 to 5.0.0

In 4.3.0 all the queries where fetched on server by default (getDataFromTree option was set to 'always' by default). In order to get the same behaviour in 5.0.0 you need to explicitly pass getDataFromTree (from @apollo/react-ssr) to withApollo.