diff --git a/website/docs/en/guide/fundamentals/middleware.mdx b/website/docs/en/guide/fundamentals/middleware.mdx index 7f68017..be70e74 100644 --- a/website/docs/en/guide/fundamentals/middleware.mdx +++ b/website/docs/en/guide/fundamentals/middleware.mdx @@ -5,19 +5,18 @@ import { Tab, Tabs, PackageManagerTabs } from 'rspress/theme'; Middleware is a function that intervenes in the processing flow of a parsed function. It provides a way to insert logic into the request and response flow to execute code before a response is sent or before a request is processed. `GQLoom`'s middleware follows the onion middleware pattern of [Koa](https://koajs.com/#application). -## Defining middleware +## Define Middleware -A middleware is a function that takes two arguments: `next` and `payload`. +Middleware is a function that will be injected with an `options` object as a parameter when called. The `options` object contains the following fields: + - `outputSilk`: output silk, which includes the output type of the field currently being parsed; + - `parent`: the parent node of the current field, equivalent to `useResolverPayload().root`; + - `parseInput`: a function used to obtain the input of the current field; + - `type`: the type of the current field, whose value can be `query`, `mutation`, `subscription`, or `field`; + - `next`: a function used to call the next middleware; -- `next` is a function that represents the next middleware. When `next` is called, the next middleware will be executed. +The `options` object can also be directly used as the `next` function. -- `payload` is an object that contains information about the field currently being parsed, specifically the following fields: - - `outputSilk`: output silk containing the output type of the field currently being parsed; - - `parent`: the parent of the current field, equivalent to `useResolverPayload().root`; - - `parseInput`: the function used to get the input of the current field; - - `type`: the type of the current field, with a value of `query`, `mutation`, `subscription` or `field`; - -Additionally, we can get the context of the current parser function and more with `useContext()` and `useResolverPayload()`. +Additionally, we can use `useContext()` and `useResolverPayload()` to get the context and more information of the current resolver function. A minimal middleware function is as follows: diff --git a/website/docs/zh/guide/fundamentals/middleware.mdx b/website/docs/zh/guide/fundamentals/middleware.mdx index 768fa11..5a15235 100644 --- a/website/docs/zh/guide/fundamentals/middleware.mdx +++ b/website/docs/zh/guide/fundamentals/middleware.mdx @@ -7,16 +7,15 @@ import { Tab, Tabs, PackageManagerTabs } from 'rspress/theme'; ## 定义中间件 -中间件是一个函数,它接收两个参数:`next` 和 `payload`。 - -- `next` 是一个函数,它代表着下一个中间件。当调用 `next` 时,将执行下一个中间件。 - -- `payload` 是一个对象,它包含了当前正在解析字段的信息,具体为以下字段: +中间件是一个函数,它将在调用时被注入 `options` 对象作为参数,`options` 包含以下字段: - `outputSilk`: 输出丝线,包含了当前正在解析字段的输出类型; - `parent`: 当前字段的父节点,相当于 `useResolverPayload().root`; - `parseInput`: 用于获取当前字段输入的函数; - `type`: 当前字段的类型,其值为 `query`, `mutation`, `subscription` 或 `field`; + - `next`: 用于调用下一个中间件的函数; +`options` 还可以直接作为 `next` 函数使用。 + 另外,我们还可以通过 `useContext()` 和 `useResolverPayload()` 获取到当前解析函数的上下文以及更多信息。 一个最基础的中间件函数如下: @@ -83,7 +82,7 @@ export const ZodExceptionFilter: Middleware = async (next) => { ```ts import { silk, type Middleware } from "@gqloom/core" -export const outputValidator: Middleware = async (next, { outputSilk }) => { +export const outputValidator: Middleware = async ({ next, outputSilk }) => { const output = await next() return await silk.parse(outputSilk, output) }