restql 学习三 查询语言简单说明

restql 中with 参数的内容在restql 中实际上就是http request 的请求内容。按照restql 的设计
method 分为 from (get) , to (post) ,into(put) ,update (patch), delete (delete), 实际开发中,好多
团队对于from (get) 的处理可能并不是按照 rest标准的处理,我们可能需要使用to 处理

to 格式说明

  • 格式
 
to users
  with
    id = "user.id"
    username = "user.name"
    password = "super.secret"

http post 格式

POST http://some.api/users/
BODY { "id": "user.id", "username": "user.name", "password": "super.secret"

wtih 参数的格式

with 支持简单数据类型以及复杂格式(json,数组扁平处理 )

  • 支持的数据类型
- 字符串,使用双引号
- 数字,可以是浮点类型 ,科学计数不支持
- 布尔类型,true,false
- 列表类型,使用中括号包含
- key/value 数据结构,类似json
- 从其他查询引用的值,使用引号以及`.`引用,类似json 字段引用

变量引用

我们可以方便的传递参数,

  • 格式如下
from superheroes
    with
        name = $heroName
        level = $heroLevel
        powers = $heroPowers
 

对应的http 请求为

localhost:9000/run-query?heroName="Superman"&heroLevel=99&heroPowers=["flight","heat vision","super strenght"]

展开以及扁平处理

使用展开以及扁平功能,我们我们方便的进行传递参数的处理

  • 参考扩展使用
from superheroes as party
    with
        id = [1, 2, 3]

http 请求格式

GET http://some.api/superhero?id=1
GET http://some.api/superhero?id=2
GET http://some.api/superhero?id=3
  • 参考扁平处理
    使用-> 会镜像进行转换
 
        from superheroes as fused
         with
          id = [1, 2, 3] -> flatten
 

http 格式

GET http://some.api/superhero?id=1&id=2&id=3

值编码

可以对于参数参数编码(比如base64,json)

  • 参考使用
from superheroes as hero
    with
        stats = {health: 100,
                 magic: 100} -> json // encode this value as a json string
from superheroes as hero
    with
        bag = {capacity: 10} -> base64
 
 

选择返回值

使用only 我们可能选择应用需要的数据

  • 参考格式
from superheroes as hero
    with
        id = 1
    only
        name
        items
        skills.id
        skills.name
        nicknames -> matches("^Super")

说明:
其中我们可以使用match 进行进一步的数据过滤

忽略错误

有时对于数据查询的错误我们可能不太感兴趣,我们可恶意使用忽略错误

  • 参考格式
 
from products as product
from ratings
  with
    productId = product.id
  ignore-errors

请求头处理

很多时候我们基于请求头进行api 的认证处理,restql 提供了请求头参数的处理

  • 参考格式
from superheroes as hero
headers
    Authorization = "Basic user:pass"
    Accept = "application/json"
with
    id = 1

超时控制

很多时候接口响应比较慢,我们可以通过设置超时时间

  • 参考格式
from superheroes as hero
headers
    Authorization = "Basic user:pass"
    Accept = "application/json"
timeout 200
with
    id = 1

cache 请求头控制

我们可以添加cache 请求头,方便proxy 对于请求资源的cache 处理

use max-age = 600
from products

请求头

参考资料

http://docs.restql.b2w.io/#/restql/query-language?id=expanding-and-flattening

原文地址:https://www.cnblogs.com/rongfengliang/p/11881426.html