当前,我正在使用apollo-server-lambda npm版本1.4.0。将npm更新到2.4.8版本后,它不起作用。
这是我的无服务器lambda代码
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);
};
它抛出一个错误: 失败:graphqlLambda不是函数
答案 0 :(得分:0)
按照文档here中的建议,您可以按照以下3个步骤升级到v2.4.8,
ApolloServer
创建服务器createHandler
方法创建graphqlHandler
graphqlHandler
处理请求。您可以参考以下代码,
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);
};