2017-01-10 73 views
0

我有一個本地實例,Apollo Graphql庫運行正常,但我想在AWS Lambda中使用相同的模式和解析器。AWS內的Apollo服務器Graphql 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方法。

我不知道如何,使用阿波羅工具和服務器工作在同一個工作示例老graphql代碼...

轉換在lambda處理我有這個,但需要使用阿波羅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而不是?

回答

1

我一直在爲此而苦苦掙扎。我發現最安全的解決方案是將aws-serverless-expressexpress-graphql結合起來。這不是最直接的解決方案,所以它導致我創建一個模塊:https://www.npmjs.com/package/lambda-graphql

+0

謝謝,正在尋找類似這樣的東西。沒有機會使用它,但看起來很簡單。 – aqwert