mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
have schema extern
have cors extern have plugins exter have context extern
This commit is contained in:
parent
4601c3df34
commit
255244fbec
@ -11,4 +11,4 @@ const schema = async (): Promise<GraphQLSchema> => {
|
||||
})
|
||||
}
|
||||
|
||||
export { schema }
|
||||
export default schema
|
||||
@ -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({
|
||||
|
||||
14
backend/src/server/context.ts
Normal file
14
backend/src/server/context.ts
Normal 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
|
||||
8
backend/src/server/cors.ts
Normal file
8
backend/src/server/cors.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import cors from 'cors'
|
||||
|
||||
const corsOptions = {
|
||||
origin: '*',
|
||||
exposedHeaders: ['token'],
|
||||
}
|
||||
|
||||
export default cors(corsOptions)
|
||||
17
backend/src/server/plugins.ts
Normal file
17
backend/src/server/plugins.ts
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user