目前我正在使用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不是一个函数
正如doc 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);
};