Apollo Server Graphql within AWS Lambda



我有一个本地实例在 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 处理程序中,我有这个,但需要它来使用阿波罗快递服务器。

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-serverless-expressexpress-graphql相结合。这不是最直接的解决方案,因此它促使我为它创建一个模块:https://www.npmjs.com/package/lambda-graphql

最新更新