-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support in buildContext for updating context based on augme…
…ntation from other plugins (#173)
- Loading branch information
Showing
3 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |