apollo-server-lambda v2: expose gateway event and context
See original GitHub issueFeature Request
Problem
There’re a few usecases I can think of when developing an AWS Lambda with apollo-server where the developer needs access to the original APIGatewayEvent and Context. The current way to develop a lambda in apollo-server v2 is as follows:
exports.graphqlHandler = server.createHandler({
cors: {
origin: '*',
credentials: true
}
})
create handler takes care of creating the incoming handler that uses the original ApiGatewayEvent and Context. While we can use the context
property of ApolloServer for authroization logic to have access to the incoming gateway event and context, there’re some cases where we need the original gateway event before that on the handler level, some of the usecases I can think of is
Usecases
-
Database connection caching To be more specific with Mongoose the recommended way to cache the db connection is by checking it on the handler level as explained here https://mongoosejs.com/docs/lambda.html
-
Warming up the lambda to avoid cold starts and skipping execution if its a warmup check To be more specific using the serverless-plugin-warmup to avoid executing the handler if its a warmup request and do an early return.
-
setting
context.callbackWaitsForEmptyEventLoop = false
, I believe the created handler by apollo-server already adds this? -
other initialisation logic like defining a mongoose/database schema, models, etc…
Current Version
apollo-server-lambda v2.9.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:8 (1 by maintainers)
Top GitHub Comments
@khaledosman thank you for
context.callbackWaitsForEmptyEventLoop = false
hint. My lambda function was hanging in browser GET request when I was training to load the playground page. And I think that was because of the asynchronous MongoDB connection timeI avoided to create the handler on every request and to reconnect to MongoDB so I have done something similar to:
@FlorinDavid Right in mycase I also wanted to share the db connection before hitting graphql’s context to check if the user is authenticated and share him across the resolvers, so I had to do something like this
then use the db connection in apollo server’s context to have the user or the authentication error accessible from resolvers