You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The client call to the db works, but the server then "hangs" after the resolver, consistently making the lambda last 6 seconds before returning a 502 internal server error.
How to reproduce
1.) Setup a quick sample graphql yoga server and deploy it to aws lambda behind an http endpoint.
2.) Create a simple mutation resolver that uses the prisma client.
3.) Access the graphql playground by putting the http endpoint into your browser.
4.) Try the mutation and note the lag and 502 response.
Expected behavior
The function should return quickly and without a 502 internal server error. The only workaround was to run prisma.disconnect() during or right after every resolver. I tried to create a middleware but the "hang" happened before that could be run.
We already had a function that wrapped around each resolver, so I was able to add the disconnect there. I have no idea if that is helpful, but it's working for now:
catch.ts:
exportconstcatchResult=fn=>{returnfunction(...args){// hook prisma instance here so we can use to disconnect belowconstprisma=args[2].prisma;returnfn(...args).then((result)=>{// resolver was successful, disconnect from prisma and return resultprisma.disconnect();console.log("Prisma client successfully disconnected.");returnresult;}).catch((err)=>{// resolver threw an error, disconnect from prisma and throw onprisma.disconnect();console.log("Prisma client successfully disconnected.");throwerr;});};};
Then in the resolver, we import and wrap our function like this:
signupUser: catchResult(async(_,{ firstName, lastName, email, password },ctx)=>{constvalidEmail=validateEmail(email);if(!validEmail){thrownewError('Invalid email.');}constvalidPassword=validatePassword(password);if(!validPassword){thrownewError('The password is too short.');}constduplicateEmail=awaitctx.prisma.user.findMany({where: { email }});if(duplicateEmail.length>0){thrownewError('There is already an account with this email address.');}constverificationToken=generateResetToken();constuser=awaitctx.prisma.user.create({data: {
firstName,
lastName,
email,
verificationToken,password: hashPassword(password),supportToken: uuid()}});awaitnewRegistrationEmail(user,verificationToken);consttoken: Token=jwt.sign({id: user.id,role: user.role},process.env.JWT_SECRET,{expiresIn: '1y'});return{ token, user };})
Basically, you will need to set callBackWaitForEmptyEventLoop option to false because event loop can't be empty if we have a db connection from a previous lambda execution.
Export the server like so in your code:
exports.server=(event,context,cb)=>{// Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty.context.callbackWaitsForEmptyEventLoop=false;returnlambda.graphqlHandler(event,context,cb);};
Bug description
We're using graphql yoga 1.18.3 and deploying via the most recent serverless framework to aws lambda:
serverless.yml:
server.ts:
context.ts:
Using this generator block in schema.prisma:
We're connecting to a large aurora mysql 5.7 db.
The client call to the db works, but the server then "hangs" after the resolver, consistently making the lambda last 6 seconds before returning a 502 internal server error.
How to reproduce
1.) Setup a quick sample graphql yoga server and deploy it to aws lambda behind an http endpoint.
2.) Create a simple mutation resolver that uses the prisma client.
3.) Access the graphql playground by putting the http endpoint into your browser.
4.) Try the mutation and note the lag and 502 response.
Expected behavior
The function should return quickly and without a 502 internal server error. The only workaround was to run
prisma.disconnect()
during or right after every resolver. I tried to create a middleware but the "hang" happened before that could be run.We already had a function that wrapped around each resolver, so I was able to add the disconnect there. I have no idea if that is helpful, but it's working for now:
catch.ts:
Then in the resolver, we import and wrap our function like this:
Prisma information
Schema has simple user model:
First noticed the error when trying to check if a user with the given email was already in our db:
const duplicateEmail = await ctx.prisma.user.findMany({ where: { email } });
The "hang" happened whether the resolver threw an error or returned successfully.
Environment & setup
Lambda is set to use
nodejs12.x
in serverless.yml."@prisma/client": "^2.0.0-beta.2"
in package.json.The text was updated successfully, but these errors were encountered: