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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@iwatakeshi/apollo-next

v0.4.0

Published

Utilities to integrate Apollo and Next.js.

Readme

apollo-next

Utilities to integrate Apollo Client with Next.js, supporting both Pages Router and App Router.

Features

  • Pages Router Support - Full support for getStaticProps and getServerSideProps
  • App Router Support - Next.js 13+ App Router with Server Components
  • Automatic Cache Hydration - Seamless SSR/SSG to client-side transitions
  • TypeScript First - Fully typed with comprehensive JSDoc
  • Performance Optimized - Efficient cache merging and deduplication
  • Debug Mode - Built-in logging for development
  • Cache Persistence - Optional localStorage/sessionStorage support
  • DevTools Integration - Easy Apollo DevTools setup

Installation

npm add @iwatakeshi/apollo-next graphql @apollo/client

Quick Start

Pages Router (Next.js 12 and earlier)

  1. Create your Apollo Client
// lib/apolloClient.ts
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';

export const createApolloClient = () =>
  new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: new HttpLink({
      uri: 'https://api.example.com/graphql',
    }),
    cache: new InMemoryCache(),
  });
  1. Set up Apollo Provider in _app.tsx
// pages/_app.tsx
import { ApolloProvider } from '@apollo/client';
import { useApollo } from '@iwatakeshi/apollo-next';
import { createApolloClient } from '../lib/apolloClient';

function MyApp({ Component, pageProps }) {
  const apolloClient = useApollo(createApolloClient(), pageProps);

  return (
    <ApolloProvider client={apolloClient}>
      <Component {...pageProps} />
    </ApolloProvider>
  );
}

export default MyApp;
  1. Use in your pages with SSR/SSG
// pages/index.tsx
import { useQuery, gql } from '@apollo/client';
import { GetStaticProps } from 'next';
import { withApollo } from '@iwatakeshi/apollo-next';
import { createApolloClient } from '../lib/apolloClient';

const GET_DATA = gql`
  query GetData {
    data {
      id
      name
    }
  }
`;

export default function HomePage() {
  const { data, loading } = useQuery(GET_DATA);
  
  if (loading) return <p>Loading...</p>;
  
  return <div>{/* render data */}</div>;
}

export const getStaticProps = withApollo<GetStaticProps>(
  createApolloClient(),
  async ({ client }) => {
    await client.query({ query: GET_DATA });
    
    return {
      props: {},
      revalidate: 60,
    };
  }
);

App Router (Next.js 13+)

  1. Create a Client Component wrapper
// app/providers.tsx
'use client';

import { ApolloWrapper } from '@iwatakeshi/apollo-next/app-router';
import { createApolloClient } from '../lib/apolloClient';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <ApolloWrapper makeClient={createApolloClient}>
      {children}
    </ApolloWrapper>
  );
}
  1. Use in your layout
// app/layout.tsx
import { Providers } from './providers';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}
  1. Use in Server or Client Components
// app/page.tsx
'use client';

import { useQuery, gql } from '@apollo/client';

const GET_DATA = gql`
  query GetData {
    data {
      id
      name
    }
  }
`;

export default function Page() {
  const { data, loading } = useQuery(GET_DATA);
  
  if (loading) return <p>Loading...</p>;
  
  return <div>{/* render data */}</div>;
}

Advanced Features

Cache Persistence

Persist Apollo cache to localStorage for offline support:

import { usePersistentApollo } from '@iwatakeshi/apollo-next';

function MyApp({ Component, pageProps }) {
  const apolloClient = usePersistentApollo(
    createApolloClient(),
    pageProps,
    { 
      storage: 'localStorage', // or 'sessionStorage'
      key: 'my-app-apollo-cache'
    }
  );

  return (
    <ApolloProvider client={apolloClient}>
      <Component {...pageProps} />
    </ApolloProvider>
  );
}

Query Prefetching

Prefetch multiple queries in parallel during SSR/SSG:

import { prefetchQueries, withApollo } from '@iwatakeshi/apollo-next';

export const getStaticProps = withApollo(
  createApolloClient(),
  async ({ client }) => {
    await prefetchQueries(client, [
      { query: GET_USER, variables: { id: '1' } },
      { query: GET_POSTS },
      { query: GET_COMMENTS, variables: { postId: '1' } },
    ]);
    
    return {
      props: {},
      revalidate: 60,
    };
  }
);

Debug Mode

Enable detailed logging for development:

import { configure } from '@iwatakeshi/apollo-next';

if (process.env.NODE_ENV === 'development') {
  configure({
    debug: true,
    logCacheHydration: true,
    logPerformance: true,
  });
}

Apollo DevTools

Enable Apollo DevTools in the browser:

import { enableApolloDevTools } from '@iwatakeshi/apollo-next';

const client = createApolloClient();

if (process.env.NODE_ENV === 'development') {
  enableApolloDevTools(client);
}

API Reference

useApollo(client, pageProps)

React hook for Pages Router that initializes Apollo Client with SSR/SSG state.

withApollo(client, dataFetchingFn)

HOC that wraps getStaticProps or getServerSideProps to inject Apollo Client.

ApolloWrapper

Client Component wrapper for App Router (Next.js 13+).

usePersistentApollo(client, pageProps, options)

Hook with localStorage/sessionStorage persistence.

prefetchQueries(client, queries)

Utility to prefetch multiple queries in parallel.

configure(options)

Configure debug logging and performance monitoring.

enableApolloDevTools(client)

Enable Apollo DevTools integration.

TypeScript Support

Full TypeScript support with comprehensive types:

import type { ApolloPageProps } from '@iwatakeshi/apollo-next';

interface MyPageProps {
  title: string;
}

export const getStaticProps = async (): Promise<GetStaticPropsResult<ApolloPageProps<MyPageProps>>> => {
  // ...
};

Migration from 0.3.x

Version 0.4.0 adds new features while maintaining backward compatibility:

  • ✅ All existing APIs work unchanged
  • ✅ New App Router support is opt-in
  • merge function is deprecated (use withApollo instead)
  • init is now internal (no breaking changes if you weren't using it directly)

License

MIT