Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support in buildContext for updating context based on augme… #173

Merged
merged 13 commits into from
Feb 17, 2023
Merged
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>;
}