1

我有一个使用 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加入架构和解析器而不是使用 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代替?

4

1 回答 1

1

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

于 2017-01-31T12:37:36.397 回答