Skip to content

Commit faae4dd

Browse files
feat: graphql headers handling, extract http create server from awilix, middleware mocking enabled
1 parent 90576ac commit faae4dd

5 files changed

Lines changed: 27 additions & 18 deletions

File tree

docs/14-graphql.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## GraphQL
2+
3+
#### GraphQL headers
4+
In order to use headers in GraphQl query, once you created GraphQL handler, you can use context param:
5+
6+
for example
7+
```
8+
export const meQuery = async (parent: any, args: any, context: QueryContext) => {
9+
const token = ((context as any).req as Request).get("authorization")?.split(" ").at(1); // express Request
10+
11+
const { result } = await context.queryBus.execute(new MeQuery({ token }));
12+
return result;
13+
};
14+
```
15+
16+
REMEMBER: req param must be added in ApolloServer context setup

src/app/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ async function createApp({
3636
const apolloServer = new ApolloServer({
3737
typeDefs,
3838
resolvers,
39-
context: () => ({
39+
context: ({ req }) => ({
40+
req,
4041
commandBus,
4142
queryBus,
4243
}),

src/container.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { asFunction, asValue, AwilixContainer, createContainer as createAwilixContainer, InjectionMode } from "awilix";
2-
import http from "http";
1+
import { AwilixContainer, createContainer as createAwilixContainer, InjectionMode } from "awilix";
32
import { DataSource } from "typeorm";
4-
import { createApp } from "./app/app";
53
import { AppConfig, appConfigFactory } from "./config/app";
6-
74
import { registerCommonDependencies } from "./container/common";
85
import { registerDatabase } from "./container/database";
96
import { loadEnvs } from "./config/env";
@@ -37,15 +34,5 @@ export async function createContainer(dependencies?: ContainerDependencies): Pro
3734
await registerSubscribers(container);
3835
await registerDatabase(container, dependencies);
3936

40-
container.register({
41-
app: asFunction(createApp).singleton(),
42-
});
43-
44-
const { app } = container.cradle;
45-
46-
container.register({
47-
server: asValue(http.createServer(await app)),
48-
});
49-
5037
return container;
51-
}
38+
}

src/container/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createLogger, restrictFromProduction } from "@tshio/logger";
66
import { AppConfig } from "../config/app";
77
import { cacheClient } from "../tools/cache-client";
88
import { createRouter } from "../app/router";
9+
import { createApp } from "../app/app";
910

1011
export async function registerCommonDependencies(appConfig: AppConfig, container: AwilixContainer) {
1112
await cacheClient.connect();
@@ -23,6 +24,7 @@ export async function registerCommonDependencies(appConfig: AppConfig, container
2324
.classic()
2425
.singleton()
2526
.inject(() => ({ throwOnFailure: false })),
27+
app: asFunction(createApp).singleton(),
2628
});
2729

2830
return container;

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "express-async-errors";
2+
import http from "http";
23
import { createContainer } from "./container";
34

45
(async () => {
@@ -15,7 +16,9 @@ import { createContainer } from "./container";
1516
process.exit(1);
1617
});
1718

18-
const { server, port } = container.cradle;
19-
server.listen(port);
19+
const { port, app } = container.cradle;
20+
21+
http.createServer(await app).listen(port);
22+
2023
container.cradle.logger.info(`listening on port: ${port}`);
2124
})();

0 commit comments

Comments
 (0)