Skip to content

📋 Next.js app using graphQL with Apollo to retrieve API data.

Notifications You must be signed in to change notification settings

AndrewJBateman/next-apollo-graphql

Repository files navigation

⚡ Next Apollo GraphQL

  • A Next.js React app using Apollo GraphQL to get data from an API.
  • Note: to open web links in a new window use: ctrl+click on link

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

📄 Table of contents

📚 General Info

  • Previously displayed data from the Netlify fruits API but this is no longer live. The API is now down.
  • Next.js is for server-rendered react apps. It has automatic code splitting, simple page-based routing, built-in CSS support and hot reloading. Every component file in the pages folder is treated as a page.
  • Apollo Client used to fetch data using GraphQL.
  • GraphQL only return the data requested. Data only served from a single end-point. Lots of companies use it. GraphQL makes tasks more complex and there are possible performance issues that would not occur using REST with a web cache.
  • Tailwind CSS added to Next.js
  • Picsum API random photo is displayed
  • Latest Next/Image optimisation used so image downloaded as Webp format and automatically sized. This is a major advantage over non-optimised Images.
  • Converted to Progressive Web App (PWA).
  • Custom Document file ./pages/_document.js created to override and extend default Document. It injects meta tags into every page

📷 Screenshots

Example screenshot.

📶 Technologies

💾 Setup

  • npm run codegen to run codegen to generate output to generated/index.tsx
  • npm run dev runs the app in the development mode. Open http://localhost:3000 to view it in the browser.
  • npm run build builds the app for production to the build folder. It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.

💻 Code Examples

  • withApollo function to return instance of Apollo client
const withApollo = nextWithApollo(

  ({ initialState, headers }) => {
    return new ApolloClient({
      ssrMode: typeof window === "undefined",
      link: new HttpLink({
        uri: "https://rickandmortyapi.com/graphql",
      }),
      headers: {
        ...(headers as Record<string, string>),
      },
      cache: new InMemoryCache().restore(initialState || {}),
    });
  },
  {
    // eslint-disable-next-line react/display-name
    render: ({ Page, props }) => {
      // eslint-disable-next-line react-hooks/rules-of-hooks
      const router = useRouter();
      return (
        <ApolloProvider client={props.apollo}>
          <Page {...props} {...router} />
        </ApolloProvider>
      );
    },
  }
);

📋 Status & To-Do List

  • Status: Working
  • To-Do: Update sitemap, robots files. Add styles

👏 Inspiration

📁 License

  • N/A

✉️ Contact