Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Apollo GraphQL with new _middleware API #31694

Closed
ncphillips opened this issue Nov 22, 2021 · 9 comments
Closed

Cannot use Apollo GraphQL with new _middleware API #31694

ncphillips opened this issue Nov 22, 2021 · 9 comments
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.

Comments

@ncphillips
Copy link
Contributor

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

16.9.0

What browser are you using?

n/a

What operating system are you using?

macOS

How are you deploying your application?

n/a

Describe the Bug

Inside of pages/_middleware I initialize a new instance of ApolloClient with createHttpLink in @apollo/client.

When I try to run next build it fails:

./node_modules/ts-invariant/lib/invariant.esm.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/_middleware

Import trace for requested module:
./node_modules/apollo-link/lib/bundle.esm.js
./node_modules/apollo-link-context/lib/bundle.esm.js
./pages/_middleware.ts

Expected Behavior

Build succeeds

To Reproduce

I will provide a reproduction if necessary.

@ncphillips ncphillips added the bug Issue was opened via the bug report template. label Nov 22, 2021
@ncphillips
Copy link
Contributor Author

@ncphillips ncphillips changed the title Cannot use apollo-link with new _middleware API Cannot use Apollo with new _middleware API Nov 22, 2021
@ncphillips ncphillips changed the title Cannot use Apollo with new _middleware API Cannot use Apollo GraphQL with new _middleware API Nov 22, 2021
@timneutkens
Copy link
Member

This seems to be expected as the library is using eval or new Function. They'll have to remove that to be compatible.

@timneutkens timneutkens added the Middleware Related to Next.js Middleware. label Nov 26, 2021
@ncphillips
Copy link
Contributor Author

big oof

@ncphillips
Copy link
Contributor Author

Would there by any way to disable that requirement?

It's unclear to me why it's needed for _middleware but not other server-side APIs i.e. getServerSideProps, getInitialprops

@timneutkens
Copy link
Member

_middleware runs in a new environment that does not allow for eval. This ensures that any new code written is compatible with the standard web apis which will allow executing them in Workers and even in the browser in development, it's a preparation for future work to improve more of the Next.js rendering cycle and where code can run.

@ncphillips
Copy link
Contributor Author

Very interesting. I'll open a ticket with apollo later. Thanks Tim

@ncphillips
Copy link
Contributor Author

ncphillips commented Dec 6, 2021

@timneutkens I opened a ticket in Apollo (apollographql/apollo-client#9128) and they're wondering if this is an issue with how the check happening in Next.js.

I went and looked at one of the files that Next.js was complaining about and there's no instances of eval or new Function happening, but there was some places where variable names contain the words Function.

Edit: I've pulled down the apollo-client source code. I cannot find any references to new Function or eval in their codebase. This adds to my suspicion that the problem might be in how Next is checking for those calls.

@ncphillips
Copy link
Contributor Author

Woo! The issue has been discovered and the fixes are incoming on the apollo side!

apollographql/apollo-client#9128

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.
Projects
None yet
Development

No branches or pull requests

3 participants