I couldn't find a way to do that using apollo-server-lambda, so what a I did was use apollo-server-express and serverless-http in conjuction. The code below is using import/export because I am using typescript.
serverless-http accepts a variety of express-like frameworks.
import express from 'express'; // <-- IMPORTANT
import serverlessHttp from 'serverless-http'; // <-- IMPORTANT
import { ApolloServer } from 'apollo-server-express'; // <-- IMPORTANT
import typeDef from './typeDef';
import resolvers from './resolvers';
export const server = new ApolloServer({
typeDef,
resolvers,
context: async ({ req, res }) => {
/**
* you can do anything here like check if req has a session,
* check if the session is valid, etc...
*/
return {
// things that it'll be available to the resolvers
req,
res,
};
},
});
const app = express(); // <-- IMPORTANT
server.applyMiddleware({ app }); // <-- IMPORTANT
// IMPORTANT
// by the way, you can name the handler whatever you want
export const graphqlHandler = serverlessHttp(app, {
/**
* **** IMPORTANT ****
* this request() function is important because
* it adds the lambda's event and context object
* into the express's req object so you can access
* inside the resolvers or routes if your not using apollo
*/
request(req, event, context) {
req.event = event;
req.context = context;
},
});
Now for instance you can use res.cookie() inside the resolver
import uuidv4 from 'uuid/v4';
export default async (parent, args, context) => {
// ... function code
const sessionID = uuidv4();
// a example of setting the cookie
context.res.cookie('session', sessionID, {
httpOnly: true,
secure: true,
path: '/',
maxAge: 1000 * 60 * 60 * 24 * 7,
});
}
another useful resource