I am new to Next.js & GraphQL, need to work on a project using both. So trying to setup a dummy project with Next.js(JavaScript, React), Apollo Client, Apollo Server. The Server side is up and running without hassle. But Client side is not able to load Apollo Server data in UI, I was able create a basic API call from client side and load the data but not able to make it render using ApolloProvider / ApolloWrapper.
I have tried to follow many articles available online, but none worked. The final one which looked promising and easy but written for TypeScript - Medium Article & Repo. I tried to follow it but still getting below error:
src/app/layout.js
import { Inter } from "next/font/google";
import "./globals.css";
import { ApolloProvider } from "@apollo/client";
// import { ApolloWrapper } from "../lib/apollo-wrapper";
import client from "../lib/client";
const inter = Inter({ subsets: ["latin"] });
export const metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};
export default function RootLayout({ children }) {
  return (
      <html lang="en">
        <body className={inter.className}>
          <ApolloProvider client={client}>{children}</ApolloProvider>
        </body>
      </html>
  );
}
src/lib/client.js
import { ApolloClient, HttpLink, InMemoryCache, from } from "@apollo/client";
const httpLink = new HttpLink({
  uri: '/'
})
const client = new ApolloClient({
    cache: new InMemoryCache(),
    uri: "http://localhost:4000/",
    link: from([httpLink])
  });
  
export default client;
Dependencies from package.json
  "dependencies": {
    "@apollo/client": "^3.9.0-rc.1",
    "@apollo/experimental-nextjs-app-support": "^0.8.0",
    "graphql": "^16.8.1",
    "next": "14.1.0",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "14.1.0",
    "postcss": "^8",
    "tailwindcss": "^3.3.0"
  }
				
                        
If you want to use Apollo Client with the App Router, you have to use it with the
@apollo/experimental-nextjs-app-supportlibrary. Refer to this post - NextJS (AppRouter), Apollo (Client), error on using ApolloProvider in root layoutSeems like you added
@apollo/experimental-nextjs-app-supportin your dependencies but didn't use it at all. Also, your@apollo/clientlibrary may need to be updated to the latest version -npm install @apollo/client@latestTo wrap your
RootLayoutwithApolloProvider, try following (refer to https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support for more info):First, create a new file
src/app/ApolloWrapper.jsx:Now you can wrap your
RootLayoutin this wrapper component in thesrc/app/layout.jsfile: