2017-01-10 15 views
0

私はApollo Graphqlライブラリで正常に動作するローカルインスタンスを持っていますが、AWS Lambda内で同じスキーマとリゾルバを使用したいと思います。AWS Lambda内のApollo Server Graphql

私の現在のコードは次のとおりです。

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 makeSchema方法を使用するよりも、スキーマとリゾルバに参加するgraphql-toolsを使用しています。

私は私がこれを持っているが、それはアポロ特急サーバーを使用する必要があります...ラムダハンドラ内

アポロツールとサーバーを使用して同じ作業を例に働いていた古いgraphqlコードを変換する方法がわからないです。

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)); 
}; 

代わりにgraphqlExpressを使用するためにラムダ関数内で使用できるものは何ですか?

答えて

1

私はこれにも苦労しています。私は最も安全な解決策がaws-serverless-expressexpress-graphqlを組み合わせることであることを発見しました。それは最も簡単な解決策ではないので、モジュールを作成しました:https://www.npmjs.com/package/lambda-graphql

+0

ありがとう、このようなものを探していました。それを使用するチャンスはありませんでしたが、それは十分に簡単に見えます。 – aqwert

関連する問題