have schema extern

have cors extern
have plugins exter
have context extern
This commit is contained in:
Ulf Gebhardt 2021-09-24 14:21:14 +02:00
parent 4601c3df34
commit 255244fbec
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
5 changed files with 48 additions and 38 deletions

View File

@ -11,4 +11,4 @@ const schema = async (): Promise<GraphQLSchema> => {
})
}
export { schema }
export default schema

View File

@ -2,7 +2,6 @@
import 'reflect-metadata'
import express from 'express'
import cors from 'cors'
import { ApolloServer } from 'apollo-server-express'
// config
@ -12,27 +11,19 @@ import CONFIG from './config'
import connection from './typeorm/connection'
import getDBVersion from './typeorm/getDBVersion'
// server
import cors from './server/cors'
import context from './server/context'
import plugins from './server/plugins'
// graphql
import { schema } from './graphql'
import schema from './graphql/schema'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
const DB_VERSION = '0001-init_db'
const context = (args: any) => {
const authorization = args.req.headers.authorization
let token = null
if (authorization) {
token = authorization.replace(/^Bearer /, '')
}
const context = {
token,
setHeaders: [],
}
return context
}
async function main() {
// open mysql connection
const con = await connection()
@ -53,28 +44,8 @@ async function main() {
// Express Server
const server = express()
const corsOptions = {
origin: '*',
exposedHeaders: ['token'],
}
server.use(cors(corsOptions))
const plugins = [
{
requestDidStart() {
return {
willSendResponse(requestContext: any) {
const { setHeaders = [] } = requestContext.context
setHeaders.forEach(({ key, value }: { [key: string]: string }) => {
requestContext.response.http.headers.append(key, value)
})
return requestContext
},
}
},
},
]
// cors
server.use(cors)
// Apollo Server
const apollo = new ApolloServer({

View File

@ -0,0 +1,14 @@
const context = (args: any) => {
const authorization = args.req.headers.authorization
let token = null
if (authorization) {
token = authorization.replace(/^Bearer /, '')
}
const context = {
token,
setHeaders: [],
}
return context
}
export default context

View File

@ -0,0 +1,8 @@
import cors from 'cors'
const corsOptions = {
origin: '*',
exposedHeaders: ['token'],
}
export default cors(corsOptions)

View File

@ -0,0 +1,17 @@
const plugins = [
{
requestDidStart() {
return {
willSendResponse(requestContext: any) {
const { setHeaders = [] } = requestContext.context
setHeaders.forEach(({ key, value }: { [key: string]: string }) => {
requestContext.response.http.headers.append(key, value)
})
return requestContext
},
}
},
},
]
export default plugins