From 255244fbecdc7f9173381e36c36b6c66f2149f0b Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 24 Sep 2021 14:21:14 +0200 Subject: [PATCH] have schema extern have cors extern have plugins exter have context extern --- backend/src/graphql/{index.ts => schema.ts} | 2 +- backend/src/index.ts | 45 ++++----------------- backend/src/server/context.ts | 14 +++++++ backend/src/server/cors.ts | 8 ++++ backend/src/server/plugins.ts | 17 ++++++++ 5 files changed, 48 insertions(+), 38 deletions(-) rename backend/src/graphql/{index.ts => schema.ts} (93%) create mode 100644 backend/src/server/context.ts create mode 100644 backend/src/server/cors.ts create mode 100644 backend/src/server/plugins.ts diff --git a/backend/src/graphql/index.ts b/backend/src/graphql/schema.ts similarity index 93% rename from backend/src/graphql/index.ts rename to backend/src/graphql/schema.ts index a36d1dcdf..f18a3bea6 100644 --- a/backend/src/graphql/index.ts +++ b/backend/src/graphql/schema.ts @@ -11,4 +11,4 @@ const schema = async (): Promise => { }) } -export { schema } +export default schema diff --git a/backend/src/index.ts b/backend/src/index.ts index c6121aeee..c138deba7 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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({ diff --git a/backend/src/server/context.ts b/backend/src/server/context.ts new file mode 100644 index 000000000..2ad4b520d --- /dev/null +++ b/backend/src/server/context.ts @@ -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 diff --git a/backend/src/server/cors.ts b/backend/src/server/cors.ts new file mode 100644 index 000000000..e76ed1591 --- /dev/null +++ b/backend/src/server/cors.ts @@ -0,0 +1,8 @@ +import cors from 'cors' + +const corsOptions = { + origin: '*', + exposedHeaders: ['token'], +} + +export default cors(corsOptions) diff --git a/backend/src/server/plugins.ts b/backend/src/server/plugins.ts new file mode 100644 index 000000000..6b27d19ea --- /dev/null +++ b/backend/src/server/plugins.ts @@ -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