Aws lambda AWS Lambda中的阿波罗服务器图形QL
我有一个本地实例在Apollo Graphql库中运行良好,但我希望在AWS Lambda中使用相同的模式和解析器 我目前的代码是:Aws lambda AWS Lambda中的阿波罗服务器图形QL,aws-lambda,graphql,apollo-server,Aws Lambda,Graphql,Apollo Server,我有一个本地实例在Apollo Graphql库中运行良好,但我希望在AWS Lambda中使用相同的模式和解析器 我目前的代码是: var makeExecutableSchema = require('graphql-tools').makeExecutableSchema; var resolvers = require('./resolvers/root').root; var schema = require('./schema/graphSchema').schema; impo
var makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
var resolvers = require('./resolvers/root').root;
var schema = require('./schema/graphSchema').schema;
import graphqlHTTP from 'express-graphql';
import express from 'express';
//graphql express
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import path from 'path';
const config = require('./config/main.json');
const port = (!global.process.env.PORT) ? 1234 : global.process.env.PORT;
const server = global.server = express();
const allowCrossDomain = function (req, res, next) {
//slow down the requests to mimic latency
setTimeout(() => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Connection, Host, Origin, Referer, Access-Control-Request-Method, Access-Control-Request-Headers, User-Agent, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, Accept-Encoding, Accept-Language');
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
}, 1000);
}
var executableSchema = makeExecutableSchema({
typeDefs: schema,
resolvers: resolvers,
});
exports.executableSchema = executableSchema;
server.set('port', port);
server.use(express.static(path.join(__dirname, 'public')));
server.use(allowCrossDomain);
server.use('/graphql',
bodyParser.json(),
graphqlExpress({
schema: executableSchema
}));
server.use('/graphiql',
graphiqlExpress({
endpointURL: '/graphql'
}));
server.listen(server.get('port'), () => {
console.log('The server is running at http://localhost:' + server.get('port'));
});
我使用graphql工具
连接模式和解析器,而不是使用graphqlmakeSchema
方法
我不知道如何使用apollo工具和服务器将工作的旧graphql代码转换为相同的工作示例
在lambda处理程序中,我有这个,但需要它来使用apollo express服务器
var graphql = require('graphql').graphql;
var resolvers = require('./src/resolvers/root').root;
var schema = require('./src/schema/graphSchema').schema;
module.exports.graphql = (event, context, callback) => {
graphql(schema, event.body.query, resolvers, {}, event.body.variables)
.then((response) => callback(null, response))
.catch((error) => callback(error));
};
在lambda函数中可以使用什么来代替
graphqlExpress
?我也一直在努力解决这个问题。我发现最安全的解决方案是将aws无服务器express
与express graphql
相结合。这不是最简单的解决方案,因此我为它创建了一个模块:谢谢,我们正在寻找类似的解决方案。我没有机会使用它,但它看起来很简单。