Skip to content

Commit

Permalink
docs(middleware): update middleware documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
xcfox committed Dec 3, 2024
1 parent 25c27ac commit 6965006
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
19 changes: 9 additions & 10 deletions website/docs/en/guide/fundamentals/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
11 changes: 5 additions & 6 deletions website/docs/zh/guide/fundamentals/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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()` 获取到当前解析函数的上下文以及更多信息。

一个最基础的中间件函数如下:
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6965006

Please sign in to comment.