-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Getting multiple instances of graphql error despite only having a single version #2801
Comments
hi @link2cory I'll check what is going on |
@link2cory I was afraid it was the bundler that was making the mess and on runtime despite having a single version
and changing the plugins config on
and now it works great and it has also reduced the bundle size from 32MB to a few Kbs, which is great for your lambda performance. I'll continue investigate further on how to improve the version collision detection. |
Wow thank you so much for your help! |
@onhate Thanks for detailed investigation 🕵️ |
Sorry for the delay here, but I do have some updates. It turns out that the parcel solution didn't actually work in deployment. I think because the dependencies weren't actually being bundled which while it circumvented the problem, doesn't really solve it for the serverless deployment that I am trying to achieve. So I kept trying different webpack configurations until I came across this issue #1174 So I tried setting the NODE_ENV to 'production' and that seems to work. So I believe the real issue comes down to false positives introduced by the minification process. Not sure if this can be improved within graphql-js or just means I need to adjust my development setup for use with serverless-offline. I will report back if I come up with something. |
Well it looks like my NODE_ENV=production workaround led me right back to a dead end. All but the first request I make to my offline lambda result in an error saying that we expected a GraphQLSchema. I have traced this error back to assertSchema in src/type/schema.js, which calls the util function instanceOf Which is exactly the function that was edited in the issue I posted in my previous comment. I added some console logging to the assertSchema function in src/type/schema.js so that it looks like as follows:
So now when I run my lambda in production but offline i get the following output from 2 identical requests to the local endpoint:
I find this all very odd. It would seem that the schema I am providing is sometimes (all but the first request) being replaced by a similar one with a different prototype of the same name: GraphQLSchema. Since everything after my lambda handler creation is handled by Apollo-server-lambda, I suppose it is worth digging through their source as well to see if I can figure out what is going on. |
hi @link2cory , thanks for the detailed investigation, when I had run the lambda offline bundled with parcel it has worked fine, but indeed there's something else in the equation, I'll take some time to go deep in the scenarios you provided and see how to improve the false positive cases. |
@link2cory I think I finally discovered the root cause, it happens that serverless-offline uses worker threads to run the lambdas by default. Well, what that means, it means that the the lambda is instantiated in one worker thread and run in a different execution context so the Nexus schemas that are generated in the setup time uses one instance of graphql while the runtime checks after the lambda is setup uses a different instance of graphql. By setting the following on
|
Of course, the instanceOf check is kind of complex.... what the user is trying to do is to make sure that the object is a schema. But instead what the user does is check that the object is a schema instantiated with the same instance of the constructor for a schema. but what we want the user to do is to check that the object is a schema instantiated with the same version of the constructor for a schema regardless of instance. Maybe it makes sense instead to store metadata within the object that can use duck typing to easily check for whether it is a schema or not. Same goes for the other type system objects... And metadata can be added to determine what version of GraphQL the schema / type system object was created with... Just thinking out loud... |
@yaacovCR Fully agree that current approach is problematic however there are two problems with checking metadata:
|
Not sure why there would be production slowdown, in production we are doing:
Could do
Is that really slower if use symbol? In development could do:
|
@onhate thanks so much for diving deep into this. I have verified your solution in my dev environment, and now I wish I had taken a closer look at my deployed lambda because it works just fine (using stock serverless bundler) as your comment lead me to believe it would. this seems to be quite an edge-case, so I imagine a true solution wont be super fast in the making. In the meantime I will post a link to the commit where I have this working for those who stumble upon this issue before that happens. Thanks again for your help! |
@onhate THANK YOU so much, you just saved my day! :D :D :D |
This comment has been minimized.
This comment has been minimized.
@onhate -- just trying to make sure that i understand where the error comes from and if it is immune to switching the instanceof check to a Symbol.for check -- from my quick look through the serverless-offline code, i see that it can use workers to run the lambdas, but it seems to do so when configured on every execution -- why is the first execution successful and the others not? Are you able to point me in the right direction in the codebase in terms of where the setup/initial run veers off from the follow-up runs? Appreciate your time -- just a bit puzzled. |
I got the same error, anyone can help? |
I'm getting the same error, even with We've seen similar problems in other modules. As soon as they're put through webpack, I'll work on a reproduction. |
Here's the reproduction. Very simple setup, using apollo-server-lambda with webpack causes this error: https://github.com/thekevinbrown/graphqljs-instanceof-reproduction |
Found a workaround, seems like an Apollo Server can fix it, but also it'd be nice if this package could make it a bit easier for people using @yaacovCR's suggestion. |
@onhate thank you! |
I hit the same issue. my setup is a little different since i'm using graphql-codegen instead of apollo, but it's the same deal. there's only one copy of graphql:
and yarn only knows about one version of graphql:
my package.json includes
setting NODE_ENV to production changes the error but doesn't fix it - it still fails due to the instanceOf check not succeeding. and yeah it's probably because of some bundling, and since i'm creating a web app i kind of want to keep that. so my hacky workaround is to create a second node project with just the graphql-codegen stuff. in my "real project" i added a "prebuild" command that runs a script (yes i'm cool and running on windows):
hopefully that helps someone else :) |
I'm having the same issue as @scottdallamura Here is a commit where it happens. to trigger the issue, run |
Maybe for anyone who's encountering this issue where you're seemingly getting this error for no reason. I've managed to track down why it was failing for me, and I've fixed it. I got these errors because for some reason Webpack was importing both *.mjs files from graphql, and *.js files. This causes graphql to be instantiated twice, which results in this error. My error was due to the fact that the /***/ "../../../node_modules/graphql-parse-resolve-info/build-turbo/index.js":
/*!*****************************************************************************!*\
!*** ../../../node_modules/graphql-parse-resolve-info/build-turbo/index.js ***!
\*****************************************************************************/
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getAlias = exports.simplify = exports.parse = exports.simplifyParsedResolveInfoFragmentWithType = exports.parseResolveInfo = exports.getAliasFromResolveInfo = void 0;
const assert = __webpack_require__(/*! assert */ "assert");
const graphql_1 = __webpack_require__(/*! graphql */ "../../../node_modules/graphql/index.mjs");
const values_1 = __webpack_require__(/*! graphql/execution/values */ "../../../node_modules/graphql/execution/values.js");
const debugFactory = __webpack_require__(/*! debug */ "../../../node_modules/debug/src/index.js");
const debug = debugFactory("graphql-parse-resolve-info");
// <SNIP MORE CODE IRRELEVANT> As can be seen above it was importing the plugins: [
// other plugins maybe,
new NormalModuleReplacementPlugin(
/graphql\/execution\/values$/,
'graphql/execution/values.mjs',
)
], |
In our case, we put resolve: {
extensions: [".ts", ".mjs", ".json", ".js"], // IMPORTANT: .mjs has to be BEFORE .js
plugins: [PnpWebpackPlugin],
}, And it worked. That's black magic. |
I thought I'd share my own solution here, so it may save someone else hours of headdesking. My error message was 'Expected <my schema> to be a GraphQL Schema' which was traced to the 'instanceof' check in I'd previously added an alias to my webpack config in line with this comment to work around the 'multiple instances of GraphQL' issue. If you add this line, make sure the file extension is resolve: {
// https://github.com/apollographql/apollo-server/issues/4637#issuecomment-830767224
extensions: ['.ts', '.mjs', '.js'],
alias: {
// other aliases ...
// https://github.com/apollographql/apollo-server/issues/4637#issuecomment-706813287
graphql$: join(
__dirname,
'node_modules/graphql/index.mjs', // NOT index.js !!!
)
},
} EDIT: In fact, once you put |
see #3616 which attempts to replace |
Looking through the root cause of this issue I don't think we can ever make @yaacovCR Can you please test it with your solution? |
Yes looks impossible, unfortunately. Thanks for catching that! |
= add time expectations = remove references to cross-realm GraphQL (sadly), see graphql/graphql-js#2801 (comment)
= add time expectations = remove references to cross-realm GraphQL (sadly), see graphql/graphql-js#2801 (comment)
This also happens with Deno, see esm-dev/esm.sh#547 (comment) |
If experiencing with vitest, the workaround can be found here vitejs/vite#7879. The config needs to be updated to: // vite.config.ts
export default defineConfig({
test: {
server: {
deps: {
fallbackCJS: true,
},
},
},
}) |
i'm experiencing this issue and have posted the issue ardatan/graphql-tools#5732. the workaround suggested by @davidroeca by way of vitejs/vite#7879 doesn't solve the issue for me |
@jkepps It's been a while since I've thought about this but here are a few additional bits of info from what I remember. We also had to add the following workaround to the vite config: resolve: {
alias: {
graphql: 'graphql/index.js',
},
}, See this discussion thread. Could be worth considering alternatives within that thread as well. Another issue we ran into was name shadowing. We had a directory |
@davidroeca looks like the alias by itself did the trick. many thanks! 🙏🏽 |
It's great that everyone keeps posting workarounds here! We are tracking cjs/esm interactions in other issues (e.g. #4062), and the root issue within the serverless setup above seems to be out of scope for our project, so I am going to close this particular issue. |
I wasn't sure whether or not to make a new issue or comment on #594 but that one is so old I figured it would be preferable to start anew
In my project I get the "ensure that there are not multiple versions of GraphQL installed in your node_modules directory" error despite having done so.
yarn list output:
and for posterity:
My tech stack includes apollo-server-lambda and nexus-schema. Not sure if/how one of them could be behind this, but the issue only cropped up after switching from apollo-server-express to apollo-server-lambda. Nevertheless, it would seem that graphql-js is reporting this error erroneously as I have proven there is only one instance of graphql in my node_modules directory.
exact error message:
Link to my project repo: https://github.com/link2cory/portfolio-backend/tree/serverless
a quick note: although it is not in the current version of my repo, I have tried making use of the resolutions yarn with exactly the same results. Plus the evidence I have provided above suggests to me that it should not be necessary based on my project dependencies.
Please correct me if I am wrong in any of these assumptions!
The text was updated successfully, but these errors were encountered: