apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
358 stars 25 forks source link

Attempted import error: 'registerApolloClient' is not exported from '@apollo/experimental-nextjs-app-support/rsc' (imported as 'registerApolloClient'). #289

Closed Whitebytes closed 3 weeks ago

Whitebytes commented 3 weeks ago

to reproduce:

npx create-next-app@latest What is your project named? ... my-app √ Would you like to use TypeScript? ... No √ Would you like to use ESLint? ... No √ Would you like to use Tailwind CSS? ... No √ Would you like to use src/ directory? ... Yes √ Would you like to use App Router? (recommended) ... Yes ? Would you like to customize the default import alias (@/*)? » No

npm install @apollo/client@latest @apollo/experimental-nextjs-app-support graphql

Seems to me incompatability when using nextjs without typescript. When installing Nextjs with typescript, the error dont exist.

phryneas commented 3 weeks ago

This should not have anything to do with TypeScript or not TypeScript.

You will get this error message if you import registerApolloClient from a "use client" file or a file that is imported from a "use client" and so on. (It oftentimes happens if you have index.js barrel files that just mix up Client and Server exports).

registerApolloClient is only exported in a React Server Component context, just like useState is only exported from react in a "use client" context, but not in a RSC context.

Whitebytes commented 3 weeks ago

ok, can confirm this was the issue, thanx. Nature of the error gave me not any clue this was a ssr issue.. (like the one you get when using useState while ssr)..

github-actions[bot] commented 3 weeks ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

leonchabbey commented 2 weeks ago

@phryneas I'm having the same issue when using the RSC client from the middleware. Not sure whether that's a bug in the library and this RSC client is not compatible with the middleware's context?

leonchabbey commented 2 weeks ago

I think i got my answer there: https://github.com/apollographql/apollo-client-nextjs/issues/280#issuecomment-2059443275 So the client doesn't seem to have middleware support

phryneas commented 2 weeks ago

Yeah, middleware are yet another layer of Next.js that lives outside of the React world. Just create a new ApolloClient instance inside of your middleware - there's no extra support or orchestration needed from our side.

(If we find a way of making this nicer, we'll look into that, but no guarantees when that can happen)