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

docs: add latest content about GraphQL in APISIX #6449

Merged
merged 16 commits into from
Mar 4, 2022
38 changes: 35 additions & 3 deletions docs/en/latest/router-radixtree.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ We can define the following route:
```shell
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"methods": ["POST"],
"methods": ["POST", "GET"],
"uri": "/_post",
"vars": [
["post_arg_name", "==", "json"]
Expand All @@ -245,6 +245,8 @@ The route will be matched when the POST form contains `name=json`.

### How to filter route by GraphQL attributes

APISIX can handle HTTP GET and POST methods. At the same time, the request body can be a GraphQL query string or JSON-formatted content.

APISIX supports filtering route by some attributes of GraphQL. Currently we support:

* graphql_operation
Expand Down Expand Up @@ -273,8 +275,8 @@ We can filter such route out with:
```shell
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"methods": ["POST"],
"uri": "/_graphql",
"methods": ["POST", "GET"],
"uri": "/graphql",
"vars": [
["graphql_operation", "==", "query"],
["graphql_name", "==", "getRepo"],
Expand All @@ -289,6 +291,36 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
}'
```

We can verify GraphQL matches in the following three ways:

1. GraphQL query strings

```shell
$ curl -H 'content-type: application/graphql' -X POST http://127.0.0.1:9080/graphql -d '
Chever-John marked this conversation as resolved.
Show resolved Hide resolved
query getRepo {
owner {
name
}
repo {
created
}
}'
```

2. JSON format

```shell
$ curl -H 'content-type: application/json' -X POST \
http://127.0.0.1:9080/graphql --data '{"query": "query getRepo { owner {name } repo {created}}"}'
```

3. Try `GET` request match

```shell
$ curl -H 'content-type: application/graphql' -X GET \
"http://127.0.0.1:9080/graphql?query=query getRepo { owner {name } repo {created}}" -g
```

To prevent spending too much time reading invalid GraphQL request body, we only read the first 1 MiB
data from the request body. This limitation is configured via:

Expand Down
36 changes: 34 additions & 2 deletions docs/zh/latest/router-radixtree.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f

### 如何通过 GraphQL 属性过滤路由

目前,APISIX 可以处理 HTTP GET 和 POST 方法。请求体正文可以是 GraphQL 查询字符串,也可以是 JSON 格式的内容。

APISIX 支持通过 GraphQL 的一些属性过滤路由。 目前我们支持:

* graphql_operation
Expand Down Expand Up @@ -274,8 +276,8 @@ query getRepo {
```shell
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
Chever-John marked this conversation as resolved.
Show resolved Hide resolved
{
"methods": ["POST"],
"uri": "/_graphql",
"methods": ["POST", "GET"],
"uri": "/graphql",
"vars": [
["graphql_operation", "==", "query"],
["graphql_name", "==", "getRepo"],
Expand All @@ -290,6 +292,36 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
}'
```

我们可以通过以下三种方式分别去验证 GraphQL 匹配:

1. 使用 GraphQL 查询字符串

```shell
$ curl -H 'content-type: application/graphql' -X POST http://127.0.0.1:9080/graphql -d '
Chever-John marked this conversation as resolved.
Show resolved Hide resolved
query getRepo {
owner {
name
}
repo {
created
}
}'
```

2. 使用 JSON 格式

```shell
$ curl -H 'content-type: application/json' -X POST \
http://127.0.0.1:9080/graphql --data '{"query": "query getRepo { owner {name } repo {created}}"}'
```

3. 尝试 `GET` 请求

```shell
$ curl -H 'content-type: application/graphql' -X GET \
"http://127.0.0.1:9080/graphql?query=query getRepo { owner {name } repo {created}}" -g
```

为了防止花费太多时间读取无效的 `GraphQL` 请求正文,我们只读取前 `1 MiB`
来自请求体的数据。 此限制是通过以下方式配置的:

Expand Down