我有一个运行Apollo Graphql库的本地实例,但我想在AWS Lambda中使用相同的模式和解析器。
我目前的代码是:
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-tools
加入架构和解析器,而不是使用graphql makeSchema
方法。
我不知道如何使用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
?
答案 0 :(得分:1)
我也一直在努力解决这个问题。我发现最安全的解决方案是将aws-serverless-express
与express-graphql
结合起来。这不是最直接的解决方案,因此它促使我为它创建了一个模块:https://www.npmjs.com/package/lambda-graphql