I am building a serverless backend using type-graphql
and apollo-server-lambda
, but after around third request to graphql endpoint I get an error
Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
I have already downgraded type-graphql
because version 1.0.0
requires graphql@^15.3.0
while some other dependencies require graphql <= 14
Here are my dependencies
{
"dependencies": {
"@types/graphql": "14.5.0",
"apollo-server-lambda": "^2.17.0",
"aws-sdk": "^2.750.0",
"graphql": "^14.7.0",
"pg": "^8.3.3",
"reflect-metadata": "^0.1.13",
"source-map-support": "^0.5.19",
"type-graphql": "^0.17.0",
},
"devDependencies": {
"@types/aws-lambda": "^8.10.62",
"@types/aws-sdk": "^2.7.0",
"@types/jest": "^26.0.13",
"@types/node": "^14.10.0",
"apollo-server-testing": "^2.17.0",
"serverless": "^2.0.0",
"serverless-offline": "^6.7.0",
"serverless-webpack": "^5.3.4",
"ts-jest": "^26.3.0",
"ts-loader": "^8.0.3",
"typescript": "^4.0.2",
"webpack": "^4.44.1",
"webpack-node-externals": "^2.5.2"
},
"resolutions": {
"graphql": "^14.7.0",
},
npm ls graphql
result
[email protected]
├─┬ @types/[email protected]
│ └── [email protected] deduped
└── [email protected]
and index.ts
import "reflect-metadata"
import { buildSchemaSync } from "type-graphql"
import { HelloResolver } from "./src/resolvers/example"
const schema = buildSchemaSync({
resolvers: [HelloResolver],
validate: false,
})
const server = new ApolloServer({
schema,
playground: { endpoint: "/dev/graphql" },
})
export const handler = server.createHandler({
cors: {
origin: true,
credentials: true,
},
})
I had a similar problem and found an advice in typegraphql FAQ.
npm dedupe
solved it for me.
I've finally found the solution. I upgraded both apollo-server-lambda
to 3.0.0-alpha.3
and reverted type-graphql
back to ^1.0.0
. I also upgraded the resolution
to "graphql": "15.3.0"
also an important step when using type-graphql with lambda is to override the global object (name schema
matters) - reference issue
if (!global.schema) {
global.schema = buildSchemaSync({
resolvers: [UserResolver],
validate: false,
});
}
const schema = global.schema;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With