From 4195f9e0ee358518b2b7c8af0fd14d28ee708079 Mon Sep 17 00:00:00 2001 From: kamilzielinskidev Date: Mon, 12 Jun 2023 15:29:05 +0200 Subject: [PATCH] feat: graphql headers handling, extract http create server from awilix, middleware mocking enabled --- docs/14-graphql.md | 16 ++++++++++++++++ src/app/app.ts | 3 ++- src/container.ts | 10 ---------- src/container/common.ts | 2 ++ src/index.ts | 7 +++++-- 5 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 docs/14-graphql.md diff --git a/docs/14-graphql.md b/docs/14-graphql.md new file mode 100644 index 0000000..de56f4e --- /dev/null +++ b/docs/14-graphql.md @@ -0,0 +1,16 @@ +## GraphQL + +#### GraphQL headers +In order to use headers in GraphQl query, once you created GraphQL handler, you can use context param: + +for example +``` +export const meQuery = async (parent: any, args: any, context: QueryContext) => { + const token = ((context as any).req as Request).get("authorization")?.split(" ").at(1); // express Request + + const { result } = await context.queryBus.execute(new MeQuery({ token })); + return result; +}; +``` + +REMEMBER: req param must be added in ApolloServer context setup \ No newline at end of file diff --git a/src/app/app.ts b/src/app/app.ts index 8753007..3231264 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -36,7 +36,8 @@ async function createApp({ const apolloServer = new ApolloServer({ typeDefs, resolvers, - context: () => ({ + context: ({ req }) => ({ + req, commandBus, queryBus, }), diff --git a/src/container.ts b/src/container.ts index c94ef75..c23f7c4 100644 --- a/src/container.ts +++ b/src/container.ts @@ -37,15 +37,5 @@ export async function createContainer(dependencies?: ContainerDependencies): Pro await registerSubscribers(container); await registerDatabase(container, dependencies); - container.register({ - app: asFunction(createApp).singleton(), - }); - - const { app } = container.cradle; - - container.register({ - server: asValue(http.createServer(await app)), - }); - return container; } \ No newline at end of file diff --git a/src/container/common.ts b/src/container/common.ts index de91496..5e5feb5 100644 --- a/src/container/common.ts +++ b/src/container/common.ts @@ -6,6 +6,7 @@ import { createLogger, restrictFromProduction } from "@tshio/logger"; import { AppConfig } from "../config/app"; import { cacheClient } from "../tools/cache-client"; import { createRouter } from "../app/router"; +import { createApp } from "../app/app"; export async function registerCommonDependencies(appConfig: AppConfig, container: AwilixContainer) { await cacheClient.connect(); @@ -23,6 +24,7 @@ export async function registerCommonDependencies(appConfig: AppConfig, container .classic() .singleton() .inject(() => ({ throwOnFailure: false })), + app: asFunction(createApp).singleton(), }); return container; diff --git a/src/index.ts b/src/index.ts index bf5af18..f8bd8d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import "express-async-errors"; +import http from "http"; import { createContainer } from "./container"; (async () => { @@ -15,7 +16,9 @@ import { createContainer } from "./container"; process.exit(1); }); - const { server, port } = container.cradle; - server.listen(port); + const { port, app } = container.cradle; + + http.createServer(await app).listen(port); + container.cradle.logger.info(`listening on port: ${port}`); })();