【问题标题】:Apollo Server Graphql within AWS LambdaAWS Lambda 中的 Apollo Server Graphql
【发布时间】:2017-05-24 11:23:48
【问题描述】:

我有一个使用 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

【问题讨论】:

    标签: aws-lambda graphql apollo-server


    【解决方案1】:

    我也一直在为此苦苦挣扎。我发现最安全的解决方案是将aws-serverless-expressexpress-graphql 结合使用。这不是最直接的解决方案,所以我为它创建了一个模块:https://www.npmjs.com/package/lambda-graphql

    【讨论】:

    • 谢谢,正在寻找这样的东西。没有机会使用它,但它看起来很简单。
    猜你喜欢
    • 2018-12-13
    • 2019-07-22
    • 2019-09-01
    • 2019-07-26
    • 2021-04-24
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多