AWS Lambda graphql handler
npm install --save lambda-graphql
-
create Lambda function in AWS console
-
install
lambda-graphql
andgraphql
modules
npm install --save lambda-graphql graphql
- create
index.js
...for example:
'use strict'
const graphql = require('graphql')
const lambdaGraphql = require('lambda-graphql')
const schema = graphql.buildSchema(`
type Query {
hello: String
}
`)
const root = {
hello: function(params) {
return 'world'
}
}
// options passed to lambdaGraphql() are the same as options for express-graphql module
let app = lambdaGraphql({
schema: schema,
rootValue: root
})
// pass handler as `index.handler` for aws lambda
exports.handler = app.handler
// shutdown gracefully (process in container is stopped before going to "sleep")
process.on('exit',() => {
app.close()
})
process.on('SIGINT',process.exit)
-
deploy function by uploading to AWS Lambda
-
setup AWS Gateway API with proxy resource (for example /graphql) and forward requests (
GET
andPOST
methods) to created lambda function