Apollo-server: graphql-server-lambda timeouts

Created on 21 May 2017  路  3Comments  路  Source: apollographql/apollo-server

Hi,

I've deployed a graphql to lamba by following https://github.com/apollographql/graphql-server/tree/master/packages/graphql-server-lambda.

My handler.js looks like

import schema from './api/graphql/schema';
import {graphqlLambda} from 'graphql-server-lambda'

export const graphql = graphqlLambda({ schema: schema });

I've deployed it using serverless, webpack and babel. It seems to deploy ok but when i hit the API it gives me a timeout.

Looking at the logs I get timeouts.
2017-05-21T12:28:06.013Z 3d93c802-3e20-11e7-8c9a-e78f22160234 Task timed out after 300.00 seconds

At first i thought it was an issue with network/database but i've confirmed the connectivity. It seems graphqlLambda is not calling the callback to finish the lambda execution?

Any tips how i can t-shoot ?

Most helpful comment

For anyone else who comes across this post, I had the same problem, but you don't have to close your connection to MongoDB after each request. In fact, you probably shouldn't. Instead, set a special property on your context object to tell lambda to return even though there are open items on the event loop.

exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  // ...
}

This problem isn't specific to graphqlLambda but rather lambda functions in general.

More info here:
https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs

All 3 comments

Solved this. IT was related to mongoose preventing it from closing. Hope this helps someone else.

```exports.graphqlHandler = (event, context, cb) => {
mongoose.connect(process.env.MONGODB_URI, {
server: {
socketOptions: {connectTimeoutMS: 10000}
}
})
mongoose.connection.on('error', () => {
cb(new Error('Error connecting to MongoDB.'))
process.exit(1)
})

const cbCloseDB = function (error, output) {
mongoose.disconnect()
cb(error, output)
}
graphqlHandler(event, context, cbCloseDB)
}
```

For anyone else who comes across this post, I had the same problem, but you don't have to close your connection to MongoDB after each request. In fact, you probably shouldn't. Instead, set a special property on your context object to tell lambda to return even though there are open items on the event loop.

exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  // ...
}

This problem isn't specific to graphqlLambda but rather lambda functions in general.

More info here:
https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs

Thanks @michaelcox for this hint. Got the same issue with my mysql db and now it just works. Awesome!

Was this page helpful?
0 / 5 - 0 ratings