Skip to content

Commit

Permalink
chore: #374 deploy graphql to lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Duong Pham committed Feb 25, 2020
1 parent d63f431 commit c50b261
Show file tree
Hide file tree
Showing 6 changed files with 1,040 additions and 69 deletions.
5 changes: 4 additions & 1 deletion packages/graphql-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
"@reapit/cognito-auth": "2.0.19",
"@reapit/elements": "0.5.38",
"apollo-server": "^2.9.16",
"aws-lambda": "^1.0.5",
"dataloader": "^2.0.0",
"express": "^4.17.1",
"graphql": "^14.5.8",
"graphql-depth-limit": "^1.1.0",
"graphql-import": "^0.7.1",
"graphql-type-json": "^0.3.1",
"isomorphic-fetch": "^2.2.1",
"serverless": "^1.64.0",
"serverless-http": "^2.3.1",
"uuid": "^3.3.3"
},
"devDependencies": {
Expand All @@ -34,7 +38,6 @@
"@types/node-fetch": "^2.5.4",
"@types/uuid": "^3.4.6",
"concurrently": "^5.0.1",
"graphql-import": "^0.7.1",
"jest": "^25.1.0",
"nodemon": "^2.0.2",
"ts-node": "^8.5.4",
Expand Down
39 changes: 39 additions & 0 deletions packages/graphql-server/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
service: graphql-server
plugins:
- serverless-webpack
custom:
env: ${file(../../reapit-config.json)}

provider:
name: aws
runtime: nodejs10.x
stage: ${opt:stage, 'dev'}
region: eu-west-2
deploymentBucket:
name: graphql-server.${self:provider.stage}
blockPublicAccess: false
environment:
NODE_ENV: ${self:custom.env.${self:provider.stage}.NODE_ENV}
COGNITO_USERPOOL_ID: ${self:custom.${file(./yml-helpers.js):provider.stage.uppercase}.COGNITO_USERPOOL_ID}
MARKET_PLACE_URL: ${self:custom.${file(./yml-helpers.js):provider.stage.uppercase}.MARKET_PLACE_URL}

package:
include:
- dist/**
- node_modules/**
- package.json
exclude:
- error.log
- info.log
- jest.config.js
- nodemon.json
- serverless.yml
- src/**
- tsconfig.json

functions:
graphqlHandler:
handler: dist/index.graphqlHandler
events:
- http: ANY /
- http: 'ANY {proxy+}'
2 changes: 1 addition & 1 deletion packages/graphql-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const formatResponse = (
return response || {}
}

const server = new ApolloServer({
export const server = new ApolloServer({
typeDefs: typeDefs,
resolvers,
formatError,
Expand Down
7 changes: 7 additions & 0 deletions packages/graphql-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import serverless from 'serverless-http'
import { Context, Handler } from 'aws-lambda'
import { server } from './app'

const app = serverless(server as any)

export const graphqlHandler: Handler<any, any> = async (event: any, context: Context) => app(event, context)
19 changes: 19 additions & 0 deletions packages/graphql-server/yml-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://stackoverflow.com/questions/48226686/serverless-yml-touppercase#48249468
module.exports.provider = serverless => {
// The `serverless` argument containers all the information in the .yml file
const provider = serverless.service.provider

return Object.entries(provider).reduce(
(accumulator, [key, value]) => ({
...accumulator,
[key]:
typeof value === 'string'
? {
lowercase: value.toLowerCase(),
uppercase: value.toUpperCase(),
}
: value,
}),
{},
)
}
Loading

0 comments on commit c50b261

Please sign in to comment.