目的:像使用 restful api 一样使用 graphql api
基于纯 graphql 的后端服务,提供的 Open API 也是一个 graphql 接口。 对于不熟悉的人来说,可读性不高,使用不便,与现有广泛存在的 restful API 差异明显。
将 restful API 转发到 graphql API,大致分为两种方案
第一种:
优点
缺点
主要是因为 graphql 功能多、复杂,当做 restful 使用时,难免丢失一些功能。
第二种:
基于第一种,仍然需要生成graphql query,只是在请求接收到后,通过GraphQL对象,调用本地的graphql fetcher。
优点
缺点
难点:
这里不是说 restful 不能实现选择字段返回数据,而是这么做不如用 graphql。 总的来说,使用方案一二,都比较复杂,依赖东西多,且不是可靠的操作,更好的方法是采用封装 client 提供 sdk 的方式,可以提供类型安全的请求。
示意图
无论是第一种和第二种,需要关注如何将 restful 的接口对应到 graphql 接口上。
我们知道,在 graphql 中只有一个请求URI,而 restful 是每个资源都有一个以上的路径,且使用 HTTP 方法代表资源的操作。 fetcher name = 操作+资源,如创建用户变量:createUserVariable,查询所有用户变量:userVariables(查询比较特殊,前缀没有拼接操作),删除用户变量:deleteUserVariable
约定对应关系如下: 针对通用 restful 接口,其中 requestBody 是可选,每个资源有以下八个独立接口,分四种HTTP方法类型:
实际 URI 只有两种格式,requestBody:json,resources均采用后缀加s的复数,并且单词使用横线分割,如:user-variables
查询单个、更新单个、删除单个必须有 resource_id 参数,其他批量操作使用 requestBody。
注册实现
/v1/projects/([\w]+), /v1/projects/([\w]+)/([\w]+)
目前只支持,标准 result api 的 crud 转发到 graphql 的 mutation 和 query
第二种方案提供了 trait 封装,但没有提供接口,也没有测试。
首先利用前端代码生成 graphql query 语句,每个 gql 对应服务端的一个 data fetcher
这里自动生成的语句实际会有很多的多余字段,需要排除掉,可以在application.conf中配置
使用 restful 完成 crud
userVariables: [UserVariable]
[
{
"name": "test-restful9",
"description": "132",
"id": "3mpx7MQO",
"key": "test_restful9"
},
{
"name": "test-restful8",
"description": "132",
"id": "qVDgN1Gk",
"key": "test_restful8"
}
]
createUserVariable(userVariable: VariableInput!): UserVariable!
{
"userVariable": {
"name": "测试graphql",
"key": "test_132",
"valueType": "int",
"description": "132"
}
}
如果使用 graphql ,那么这个请求体长这样:
{
"operationName": "createUserVariable",
"variables": {
"userVariable": {
"name": "测试graphql",
"key": "test_132",
"valueType": "int",
"description": "132"
}
},
"query": "mutation createUserVariable($userVariable: VariableInput!) {\n createUserVariable(userVariable: $userVariable) {\n name\n __typename\n }\n}\n"
}
对用户来说,query 的拼写是痛苦的。
deleteUserVariable(id: HashId!): Boolean!
updateUserVariable(id: HashId!, userVariable: VariableInput!): UserVariable!
{
"userVariable": {
"name": "测试graphql",
"key": "test_132",
"valueType": "int",
"description": "132"
}
}
配置使用查看 development.conf
graphql {
schema.path = "src/main/resources/all.graphql"
gql.folder = "gql"
url = "http://gdp-dev.growingio.com/graphql"
auth {
key = "Cookie"
}
}
dryad {
enabled = false
namespace = "gio-graphql-forward"
group = "k8s-datatest"
provider = "io.growing.dryad.consul.provider.ConsulConfigProvider"
registry = "io.growing.dryad.consul.registry.ConsulServiceRegistry"
service {
http {
prefix = "/v1/projects/:project_id"
port = 8080
pattern = "/.*"
check {
url = "/healthy-check"
interval = 5s
}
}
}
consul {
host = "ci-consul.infra.growingio.com"
port = 80
username = "x"
password = "x"
}
}
Sign in for post a comment
Comments ( 0 )