Skip to content

Commit

Permalink
chore: #1127 add categories data to preview (#1610)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trường An authored and duong-se committed Jun 8, 2020
1 parent 6cdf68f commit 3c6f482
Show file tree
Hide file tree
Showing 55 changed files with 367 additions and 464 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Install dependencies
run: |
yarn
yarn install
# Currently, actions/caches does not support multiple paths https://github.com/actions/cache/issues/16
# Looks like they're working on it, let's keep an eye on it
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: |
yarn
yarn install
- name: Fetch config
run: |
Expand Down
22 changes: 3 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"**/react-app-scaffolder",
"**/react-app-scaffolder/**",
"**/svelte",
"**/svelte/**"
"**/svelte/**",
"**/graphql-server",
"**/graphql-server/**"
]
},
"scripts": {
Expand Down Expand Up @@ -46,24 +48,6 @@
"git add"
]
},
"resolutions": {
"**/**/lodash": "^4.17.15",
"**/**/decompress": "^4.2.0",
"**/**/mem": "^6.0.1",
"**/**/apollo-client": "2.6.8",
"**/**/apollo-link": "1.2.13",
"**/**/apollo-utilities": "1.3.3",
"**/**/apollo-cache-inmemory": "1.6.5",
"**/**/react-is": "16.12.0",
"**/**/@storybook/source-loader": "5.3.13",
"**/**/prop-types": "^15.7.2",
"**/**/rollup": "1.31.1",
"**/**/styled-components": "4.4.1",
"**/**/react-test-renderer": "16.12.0",
"acorn": "^6.4.1",
"minimist": "^1.2.2",
"kind-of": "^6.0.3"
},
"dependencies": {
"@sentry/browser": "^5.11.1",
"@types/webpack": "^4.41.6",
Expand Down
6 changes: 0 additions & 6 deletions packages/graphql-server/nodemon.json

This file was deleted.

30 changes: 8 additions & 22 deletions packages/graphql-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-server",
"version": "0.0.0",
"version": "0.0.1",
"private": true,
"description": "GraphQL Server",
"repository": "https://github.com/reapit/foundations.git",
Expand All @@ -11,9 +11,10 @@
"build:schema": "node ./scripts/build-schema.js && prettier src/schema.graphql --write",
"lint": "concurrently \"tsc --noEmit\" \"eslint --cache --ext=ts,js src\"",
"lint:fix": "prettier \"src/**/*.{graphql,gql}\" --write && eslint --cache --ext=ts,js src --fix",
"release:dev": "yarn build:schema && serverless deploy --stage dev",
"release:development": "yarn build:schema && serverless deploy --stage dev",
"release:production": "yarn build:schema && serverless deploy --stage prod",
"start:dev": "yarn build:schema && cross-env NODE_ENV=development nodemon",
"start:dev": "yarn build:schema && serverless offline --stage local",
"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",
Expand All @@ -24,28 +25,19 @@
},
"dependencies": {
"@reapit/cognito-auth": "^2.0.19",
"apollo-server": "^2.9.16",
"apollo-server-lambda": "^2.10.1",
"async": "^3.2.0",
"axios": "^0.19.2",
"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",
"lodash.merge": "^4.6.2",
"merge-graphql-schemas": "^1.7.6",
"ms": "^2.1.2",
"readable-stream": "^3.6.0",
"stack-trace": "^0.0.10",
"query-string": "^6.13.0",
"snyk": "^1.335.0",
"uuid": "^3.3.3",
"winston": "^3.2.1",
"snyk": "^1.335.0"
"winston": "^3.2.1"
},
"devDependencies": {
"@babel/core": "^7.8.4",
Expand All @@ -57,16 +49,10 @@
"@types/uuid": "^3.4.6",
"babel-loader": "^8.0.6",
"babel-runtime": "^6.26.0",
"bufferutil": "^4.0.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",
"tsconfig-paths": "^3.9.0",
"tsdx": "^0.12.3",
"utf-8-validate": "^5.0.2",
"serverless-offline": "^6.1.4",
"serverless-webpack": "^5.3.2",
"webpack-node-externals": "^1.7.2"
},
"snyk": true
Expand Down
9 changes: 6 additions & 3 deletions packages/graphql-server/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
service: graphql-server
plugins:
- serverless-webpack
- serverless-offline
custom:
env: ${file(./config.json)}
webpack:
Expand All @@ -14,7 +15,7 @@ custom:

provider:
name: aws
runtime: nodejs10.x
runtime: nodejs12.x
stage: ${opt:stage, 'dev'}
region: eu-west-2
deploymentBucket:
Expand Down Expand Up @@ -45,12 +46,14 @@ package:
functions:
graphqlHandler:
handler: src/index.graphqlHandler
environment:
SLS_DEBUG: true
events:
- http:
path: v1/graphql
path: graphql
method: post
cors: true
- http:
path: v1/graphql
path: graphql
method: get
cors: true
2 changes: 1 addition & 1 deletion packages/graphql-server/src/__mocks__/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServerContext } from '../app'
import { ServerContext } from '../index'

export const mockContext = {
traceId: 'mockTraceId',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GraphQLResponse, GraphQLRequestContext } from 'apollo-server-core'
import { formatResponse, handleContext, ExpressContext, formatError, listenCallback } from '../app'
import { formatResponse, formatError, handleContext } from '../index'
import { GraphQLError } from 'graphql'
import { ServerInfo } from 'apollo-server'

jest.mock('../logger')
jest.mock('uuid/v4', (): (() => string) => {
Expand Down Expand Up @@ -69,18 +68,23 @@ describe('app.js', () => {

describe('handleContext', () => {
it('should run correctly', () => {
const mockPrams = {
req: {
const mockParams = {
event: {
headers: {
authorization: 'mock Authorization',
Authorization: 'Mock Authorization',
},
},
res: {},
} as ExpressContext
const result = handleContext(mockPrams)
context: {
functionName: 'Mock Function Name',
},
} as any
const result = handleContext(mockParams)
expect(result).toEqual({
traceId: 'mockUUID',
authorization: 'mock Authorization',
authorization: 'Mock Authorization',
functionName: 'Mock Function Name',
event: mockParams.event,
context: mockParams.context,
})
})
})
Expand Down Expand Up @@ -133,14 +137,4 @@ describe('app.js', () => {
})
})
})

describe('listenCallback', () => {
it('should run correctly', () => {
const mockServerInfo = {
url: 'http://localhost:4000',
} as ServerInfo
const result = listenCallback(mockServerInfo)
expect(result).not.toBeDefined()
})
})
})
83 changes: 0 additions & 83 deletions packages/graphql-server/src/app.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
generateInternalServerError,
generateNotFoundError,
} from '../errors'
import { AuthenticationError, UserInputError, ApolloError, ForbiddenError, ValidationError } from 'apollo-server'
import { AuthenticationError, UserInputError, ApolloError, ForbiddenError, ValidationError } from 'apollo-server-lambda'

describe('generateAuthenticationError', () => {
it('should return AuthenticationError', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-server/src/errors/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthenticationError, UserInputError, ForbiddenError, ValidationError, ApolloError } from 'apollo-server'
import { AuthenticationError, UserInputError, ForbiddenError, ValidationError, ApolloError } from 'apollo-server-lambda'
import logger from '../logger'

export const errorMessages = {
Expand Down
29 changes: 26 additions & 3 deletions packages/graphql-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
import { ApolloServer } from 'apollo-server-lambda'
import { importSchema } from 'graphql-import'
import { Context, GraphQLResponse, GraphQLRequestContext } from 'apollo-server-core'
import { GraphQLError, GraphQLFormattedError } from 'graphql'
import uuidv4 from 'uuid/v4'
import { formatError, formatResponse } from './app'
import resolvers from './resolvers'
import depthLimit from 'graphql-depth-limit'
import logger from './logger'

export type ServerContext = Context<{ traceId: string; authorization: string }>

const typeDefs = importSchema('./src/schema.graphql')
const handleContext = ({ event, context }) => {
export const handleContext = ({ event, context }) => {
const traceId = uuidv4()
const isProductionEnv = process.env.NODE_ENV === 'production'
if (isProductionEnv) {
logger.info('handleContext', { traceId, event })
}
return {
traceId: traceId,
headers: event.headers,
authorization: event?.headers?.Authorization || '',
functionName: context.functionName,
event,
context,
}
}

export const formatResponse = (
response: GraphQLResponse | null,
requestContext: GraphQLRequestContext<{ traceId?: string }>,
): GraphQLResponse => {
const traceId = requestContext.context?.traceId
const isProductionEnv = process.env.NODE_ENV === 'production'
if (isProductionEnv) {
logger.info('formatResponse', { traceId, requestContext, response })
}
return response || {}
}

export const formatError = (error: GraphQLError): GraphQLFormattedError => {
if (process.env.NODE_ENV === 'production') {
return { message: error.message, extensions: { code: error.extensions?.code } }
}
return error
}

const server = new ApolloServer({
typeDefs: typeDefs,
typeDefs,
resolvers,
playground: true,
introspection: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-server/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const levels = {

export const logger = winston.createLogger({
format: winston.format.combine(winston.format.json()),
defaultMeta: { service: 'apollo-server' },
defaultMeta: { service: 'graphql-server' },
exitOnError: false,
transports: [
new winston.transports.Console({
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-server/src/resolvers/appointments/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import qs from 'query-string'
import logger from '../../logger'
import { ServerContext } from '../../app'
import { ServerContext } from '../../index'
import {
GetAppointmentByIdArgs,
CreateAppointmentArgs,
Expand Down
Loading

0 comments on commit 3c6f482

Please sign in to comment.