Skip to content

Commit

Permalink
feat: add support in buildContext for updating context based on augme…
Browse files Browse the repository at this point in the history
…ntation from other plugins (#173)
  • Loading branch information
Nabin99 committed Feb 17, 2023
1 parent 5f30db3 commit 5e013d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/mercurius/src/buildContext.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import type { ApiConfig } from "@dzangolab/fastify-config";
import type { Database } from "@dzangolab/fastify-slonik";
import type { FastifyRequest } from "fastify";

const buildContext = async (request: FastifyRequest) => {
return {
config: request.config as ApiConfig,
database: request.slonik as Database,
};
import type { FastifyRequest, FastifyReply } from "fastify";
import type { MercuriusContext } from "mercurius";

const buildContext = async (request: FastifyRequest, reply: FastifyReply) => {
const plugins = request.config.mercurius.plugins;

const context = {
config: request.config,
database: request.slonik,
} as MercuriusContext;

if (plugins) {
for (const plugin of plugins) {
await plugin.updateContext(context, request, reply);
}
}

return context;
};

export default buildContext;
4 changes: 4 additions & 0 deletions packages/mercurius/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MercuriusEnabledPlugin } from "./types";
import type { ApiConfig } from "@dzangolab/fastify-config";
import type { Database } from "@dzangolab/fastify-slonik";
import type { MercuriusOptions } from "mercurius";
Expand All @@ -13,8 +14,11 @@ declare module "@dzangolab/fastify-config" {
interface ApiConfig {
mercurius: MercuriusOptions & {
enabled?: boolean;
plugins?: MercuriusEnabledPlugin[];
};
}
}

export { default } from "./plugin";

export { type MercuriusEnabledPlugin } from "./types";
17 changes: 17 additions & 0 deletions packages/mercurius/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type {
FastifyPluginCallback,
FastifyPluginAsync,
FastifyRequest,
FastifyReply,
} from "fastify";
import type { MercuriusContext } from "mercurius";

export interface MercuriusEnabledPlugin
extends FastifyPluginAsync,
FastifyPluginCallback {
updateContext: (
context: MercuriusContext,
request: FastifyRequest,
reply: FastifyReply
) => Promise<void>;
}

0 comments on commit 5e013d2

Please sign in to comment.