Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/14-graphql.md
Original file line number Diff line number Diff line change
@@ -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:

Comment on lines +4 to +5

Copilot AI Mar 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in 'GraphQl': please change it to 'GraphQL' to ensure consistent capitalization.

Suggested change
In order to use headers in GraphQl query, once you created GraphQL handler, you can use context param:
In order to use headers in GraphQL query, once you created GraphQL handler, you can use context param:

Copilot uses AI. Check for mistakes.
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
3 changes: 2 additions & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function createApp({
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
context: () => ({
context: ({ req }) => ({
req,
commandBus,
queryBus,
}),
Expand Down
17 changes: 2 additions & 15 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { asFunction, asValue, AwilixContainer, createContainer as createAwilixContainer, InjectionMode } from "awilix";
import http from "http";
import { AwilixContainer, createContainer as createAwilixContainer, InjectionMode } from "awilix";
import { DataSource } from "typeorm";
import { createApp } from "./app/app";
import { AppConfig, appConfigFactory } from "./config/app";

import { registerCommonDependencies } from "./container/common";
import { registerDatabase } from "./container/database";
import { loadEnvs } from "./config/env";
Expand Down Expand Up @@ -37,15 +34,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;
}
}
2 changes: 2 additions & 0 deletions src/container/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -23,6 +24,7 @@ export async function registerCommonDependencies(appConfig: AppConfig, container
.classic()
.singleton()
.inject(() => ({ throwOnFailure: false })),
app: asFunction(createApp).singleton(),
});

return container;
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "express-async-errors";
import http from "http";
import { createContainer } from "./container";

(async () => {
Expand All @@ -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}`);
})();