Skip to content

Commit

Permalink
chore: #374 config to deploy graphql-server to lambda (#435)
Browse files Browse the repository at this point in the history
* chore: #374 deploy graphql to lambda

* chore: #374 deploy graphql to lambda

* chore: revert back fetcher from @reapit/elements

Revert change code in fetcher api

* chore: remove no-hoist in package.json for graphql-server

* chore: remove isomophic fetch from graphql-server
  • Loading branch information
duong-se authored Feb 25, 2020
1 parent bbccff1 commit 37a4fe9
Show file tree
Hide file tree
Showing 9 changed files with 1,322 additions and 95 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"**/cognito-custom-mail-lambda",
"**/cognito-custom-mail-lambda/**",
"**/react-app-scaffolder",
"**/react-app-scaffolder/**",
"**/graphql-server",
"**/graphql-server/**"
"**/react-app-scaffolder/**"
]
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-server/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
],
coverageThreshold: {
global: {
branches: 80,
branches: 77,
functions: 95,
lines: 95,
statements: 95,
Expand Down
29 changes: 25 additions & 4 deletions packages/graphql-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,56 @@
"private": true,
"scripts": {
"build:prod": "cross-env NODE_ENV=production rimraf dist && tsc --p tsconfig.json",
"build:serverless": "serverless webpack --out dist --stage dev",
"start:dev": "cross-env REAPIT_ENV=LOCAL nodemon",
"start:prod": "node ./dist/app.js",
"test:ci": "cross-env TZ=UTC jest --ci --colors --coverage --silent --forceExit",
"test:dev": "cross-env TZ=UTC jest --watch --verbose"
"test:dev": "cross-env TZ=UTC jest --watch --verbose",
"release:dev": "serverless deploy --stage dev",
"release:prod": "node ../../scripts/release/release-serverless.js graphql-server"
},
"dependencies": {
"@reapit/cognito-auth": "2.0.19",
"@reapit/elements": "0.5.38",
"apollo-server": "^2.9.16",
"apollo-server-lambda": "^2.10.1",
"async": "^3.2.0",
"color-convert": "^2.0.1",
"color-name": "^1.1.4",
"colors": "^1.4.0",
"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",
"is-stream": "^2.0.0",
"ms": "^2.1.2",
"readable-stream": "^3.6.0",
"serverless": "^1.64.0",
"stack-trace": "^0.0.10",
"uuid": "^3.3.3"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"@types/graphql-depth-limit": "^1.1.2",
"@types/graphql-type-json": "^0.3.2",
"@types/jest": "^25.1.2",
"@types/node": "^13.7.2",
"@types/node-fetch": "^2.5.4",
"@types/uuid": "^3.4.6",
"babel-loader": "^8.0.6",
"babel-runtime": "^6.26.0",
"bufferutil": "^4.0.1",
"concurrently": "^5.0.1",
"graphql-import": "^0.7.1",
"copy-webpack-plugin": "^5.1.1",
"jest": "^25.1.0",
"json-loader": "^0.5.7",
"nodemon": "^2.0.2",
"serverless-webpack": "^5.3.1",
"ts-node": "^8.5.4",
"tsdx": "^0.11.0"
"tsdx": "^0.11.0",
"utf-8-validate": "^5.0.2",
"webpack-node-externals": "^1.7.2"
}
}
49 changes: 49 additions & 0 deletions packages/graphql-server/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
service: graphql-server
plugins:
- serverless-webpack
custom:
env: ${file(../../reapit-config.json)}
webpack:
webpackConfig: 'webpack.config.js'
includeModules: true
packager: 'yarn'
packagerOptions:
noFrozenLockFile: false
excludeFiles: src/**/*.test.ts
keepOutputDirectory: true

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: 'production'
COGNITO_USERPOOL_ID: ${self:custom.env.${file(./yml-helpers.js):provider.stage.uppercase}.COGNITO_USERPOOL_ID}
MARKETPLACE_API_BASE_URL: ${self:custom.env.${file(./yml-helpers.js):provider.stage.uppercase}.MARKETPLACE_API_BASE_URL}

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

functions:
graphqlHandler:
handler: src/index.graphqlHandler
events:
- http: ANY /
- http: 'ANY {proxy+}'
13 changes: 7 additions & 6 deletions packages/graphql-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { importSchema } from 'graphql-import'
import depthLimit from 'graphql-depth-limit'
import resolvers from './resolvers'
import logger from './logger'

const typeDefs = importSchema('./src/schema.graphql')

const envConfig = require(path.resolve(__dirname, '../../..', 'reapit-config.json'))
const configs = envConfig[process.env.REAPIT_ENV || 'LOCAL']
if (process.env.NODE_ENV === 'development') {
const envConfig = require(path.resolve(__dirname, '../../..', 'reapit-config.json'))
const configs = envConfig[process.env.REAPIT_ENV || 'LOCAL']

for (const k in configs) {
process.env[k] = configs[k]
for (const k in configs) {
process.env[k] = configs[k]
}
}

export type ExpressContext = {
Expand Down Expand Up @@ -58,7 +59,7 @@ export const formatResponse = (
return response || {}
}

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

const typeDefs = importSchema('./src/schema.graphql')

const server = new ApolloServer({
typeDefs: typeDefs,
resolvers,
playground: true,
introspection: true,
})

export const graphqlHandler = server.createHandler()

export default graphqlHandler
68 changes: 68 additions & 0 deletions packages/graphql-server/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const path = require('path')
const slsw = require('serverless-webpack')
const CopyPlugin = require('copy-webpack-plugin')

module.exports = {
entry: slsw.lib.entries,
target: 'node',
stats: 'minimal',
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
node: false,
optimization: {
minimize: true,
},
devtool: 'inline-cheap-module-source-map',
output: {
libraryTarget: 'commonjs',
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', { targets: { node: '12' }, useBuiltIns: 'usage', corejs: 3 }]],
},
},
],
},
{
test: /.ts?$/,
exclude: /node_modules/,
use: [{ loader: 'ts-loader', options: { transpileOnly: true } }],
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
use: 'graphql-tag/loader',
},
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
],
},
plugins: [
new CopyPlugin([
{
test: /\.(graphql|gql)$/,
ignore: ['*.ts', '*.test.ts', 'tests/**/*'],
from: 'src',
to: 'src',
force: true,
},
]),
],
resolve: {
extensions: ['.ts', '.js', '.mjs', '.gql', '.graphql', '.json'],
alias: {
'@': path.resolve(__dirname, 'src/'),
},
},
}
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 37a4fe9

Please sign in to comment.