0.2.3 • Published 12 months ago

@iwatakeshi/apollo-next v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

apollo-next

Utilities to integrate Apollo and Next.js.

Usage

  1. Install apollo-next:
npm add @iwatakeshi/apollo-next graphql @apollo/client
  1. Create a custom Apollo client
export const createApolloClient = () =>
  new ApolloClient({
    ssrMode: typeof window === "undefined",
    // ...
  });
  1. Set the ApolloProvider in _app file and initialize Apollo.
import { ApolloProvider } from "@apollo/client";
import { useApollo } from "@iwatakeshi/apollo-next";
// Import your custom client
import { createApolloClient } from "../utils/client.apollo";

function MyApp({ Component, pageProps }) {
  return (
    <ApolloProvider client={useApollo(createApolloClient(), pageProps)}>
      <Component {...pageProps} />
    </ApolloProvider>
  );
}

export default MyApp;
  1. Use it in your static or server-side rendered pages.
import { useQuery } from "@apollo/client";
import { GetStaticProps } from "next";
import { withApollo } from "@iwatakeshi/apollo-next";
// Import your custom client
import { createApolloClient } from "../utils/client.apollo";

export default function MyPage() {
  const { data } = useQuery(/* ... */);
  return <div>{/* ... */}</div>;
}

// Wrap `getStaticProps` or `getServerSideProps` with Apollo
export const getStaticProps = withApollo<GetStaticProps>(
  createApolloClient(),
  async ({ client }) => {
    const { data } = await client.query(/*...*/);
    return {
      props: {
        data,
      },
    };
  }
);

// Or pass a function to access the context: (context) => createApolloClient()
export const getStaticProps = withApollo<GetStaticProps>(
  (context) => createApolloClient(),
  async ({ client }) => {
    const { data } = await client.query(/*...*/);
    return {
      props: {
        data,
      },
    };
  }
);
0.2.1

12 months ago

0.2.0

12 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.1.0

1 year ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago