Skip to content

Commit

Permalink
feat: graphql headers handling, extract http create server from awili…
Browse files Browse the repository at this point in the history
…x, middleware mocking enabled
  • Loading branch information
kamilzielinskidev committed Jun 12, 2023
1 parent 90576ac commit 4195f9e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
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:

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
10 changes: 0 additions & 10 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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}`);
})();

0 comments on commit 4195f9e

Please sign in to comment.