Ad

Serverless Lambda Doesn't Work After Updating Apollo-server-lambda Npm

Currently I'm using apollo-server-lambda npm version 1.4.0. After updating the npm to version 2.4.8 it doesn't work.

Here is my serverless lambda code

var {graphqlLambda} = require("apollo-server-lambda");
var {makeExecutableSchema} = require('graphql-tools');
var {schema} = require('./schema');
var {resolvers} = require('./resolvers');
const vandium = require('vandium' );
const myGraphQLSchema = makeExecutableSchema({
typeDefs: schema, resolvers,
logger: console,
});
module.exports.graphqlHandler = (event, context, callback) => {  
context.callbackWaitsForEmptyEventLoop = false;
function callbackFilter(error, output) {
  output.headers['Access-Control-Allow-Origin'] = '*';
  callback(error, output);
}
  const handler = graphqlLambda({ schema: myGraphQLSchema });
  return handler(event, context, callbackFilter);   
};

It throws an error: Failure: graphqlLambda is not a function

Ad

Answer

As suggested in doc here you can upgrade to v2.4.8 by following below 3 steps,

  1. use ApolloServer for creating server
  2. use createHandler method to create graphqlHandler
  3. use graphqlHandler for handling request.

You can refer below code,

const { ApolloServer } = require('apollo-server-lambda');

//1
const server = new ApolloServer({
  typeDefs,
  resolvers
});
//2
const graphqlHandler = server.createHandler();

module.exports.graphqlHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  function callbackFilter(error, output) {
    output.headers['Access-Control-Allow-Origin'] = '*';
    callback(error, output);
  }
//3
  graphqlHandler(event, context, callbackFilter);
};
Ad
source: stackoverflow.com
Ad