vercel / next.js

The React Framework
https://nextjs.org
MIT License
123.33k stars 26.32k forks source link

RSC graphql duplicate import error #59056

Open mjfwebb opened 7 months ago

mjfwebb commented 7 months ago

Link to the code that reproduces this issue

https://github.com/mjfwebb/nextjs-neo4j-graphql-error

To Reproduce

To reproduce the error just run the repository code with npm run dev and open http://localhost:3000/api/graphql in your browser. You will encounter an error thrown by graphql:

Error: Cannot use GraphQLNonNull "String!" from another module or realm.

To circumvent this error, we have to add the graphql package to serverComponentsExternalPackages in next.config.js (this is currently commented out).

Current vs. Expected behavior

I would expect this not to occur. This error can be circumvented using serverComponentsExternalPackages but it seems that graphql is a large enough library that it warrants being added to https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json

Verify canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #50-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 10 18:24:29 UTC 2023
Binaries:
  Node: 20.0.0
  npm: 9.6.4
  Yarn: 1.22.18
  pnpm: N/A
Relevant Packages:
  next: 14.0.4-canary.25
  eslint-config-next: 14.0.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router, Middleware / Edge (API routes, runtime)

Additional context

No response

luchillo17 commented 5 months ago

I just ran into this, came from the neo4j/graphql issue mentioned, the workaround is still valid but its a very obscure error, took me 3 days of searching until I found the GH issue...

/**
 * @type {import('@nx/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  ...
  experimental: {
    serverComponentsExternalPackages: ['graphql'],
  },
};
luchillo17 commented 4 months ago

Hi there, I found this error again, this time in a different context, it was in the API when creating the Apollo server instance, and for this case, the workaround worked, and still does.

However, now I have an issue with the client, the idea here is to use a SchemaLink in the Apollo client wrapper, so the SSR part can query the DB directly, while the client part when the application has been Hydrated in the browser happens, it should use the HttpLink.

If you need a reproduction repo of my specific issue where the error happens, here you go, just be aware you'll need a Neo4j database: https://github.com/luchillo17/graph-meister/tree/feature/schema-link-wrapper