From 8bb325511b8c88f4dd8c30e15cb060bef014e928 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 22 Jan 2024 19:48:53 +0100 Subject: [PATCH] feat: added driveId as param to new graphql route --- api/src/graphql/server.ts | 59 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/api/src/graphql/server.ts b/api/src/graphql/server.ts index 5cb99908..b44aa157 100644 --- a/api/src/graphql/server.ts +++ b/api/src/graphql/server.ts @@ -1,18 +1,18 @@ -import type { Server } from 'http'; -import { createServer as createHttpServer } from 'http'; -import type express from 'express'; -import { ApolloServerPlugin, ApolloServer } from '@apollo/server'; -import { expressMiddleware } from '@apollo/server/express4'; -import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled'; -import bodyParser from 'body-parser'; -import cookierParser from 'cookie-parser'; -import cors from 'cors'; -import { PORT } from '../env'; -import { schemaWithMiddleware } from './schema'; -import { Context, createContext } from './context'; -import { getChildLogger } from '../logger'; +import type { Server } from "http"; +import { createServer as createHttpServer } from "http"; +import type express from "express"; +import { ApolloServerPlugin, ApolloServer } from "@apollo/server"; +import { expressMiddleware } from "@apollo/server/express4"; +import { ApolloServerPluginLandingPageDisabled } from "@apollo/server/plugin/disabled"; +import bodyParser from "body-parser"; +import cookierParser from "cookie-parser"; +import cors from "cors"; +import { PORT } from "../env"; +import { schemaWithMiddleware } from "./schema"; +import { Context, createContext } from "./context"; +import { getChildLogger } from "../logger"; -const logger = getChildLogger({ msgPrefix: 'SERVER' }); +const logger = getChildLogger({ msgPrefix: "SERVER" }); function loggerPlugin(): ApolloServerPlugin { return { @@ -28,28 +28,39 @@ function loggerPlugin(): ApolloServerPlugin { }; } -const createApolloServer = (): ApolloServer => new ApolloServer({ - schema: schemaWithMiddleware, - introspection: true, - plugins: [ApolloServerPluginLandingPageDisabled(), loggerPlugin()], -}); +const createApolloServer = (): ApolloServer => + new ApolloServer({ + schema: schemaWithMiddleware, + introspection: true, + plugins: [ApolloServerPluginLandingPageDisabled(), loggerPlugin()], + }); export const startServer = async ( - app: express.Application, + app: express.Application ): Promise => { - logger.debug('Starting server'); + logger.debug("Starting server"); const httpServer = createHttpServer(app); const apollo = createApolloServer(); await apollo.start(); app.use( - '/graphql', + "/graphql", + cors(), + cookierParser(undefined, { decode: (value: string) => value }), + bodyParser.json(), + expressMiddleware(apollo, { + context: async (params) => createContext(params), + }) + ); + + app.use( + "/:driveId/graphql", cors(), cookierParser(undefined, { decode: (value: string) => value }), bodyParser.json(), expressMiddleware(apollo, { - context: async (params) => (createContext(params)), - }), + context: async (params) => createContext(params), + }) ); return httpServer.listen({ port: PORT }, () => {