sofa graphql 2 rest api webhook 试用

sofa 的webhook实际上就是将graphql 的subscription 进行了扩展,当接受到sub 请求的时候
再做一次http 的转发处理,方便rest api 的访问

环境准备

环境还是使用的docker-compose ,以前的代码,只是添加了一个webhook 的容器镜像
可以参考: https://www.cnblogs.com/rongfengliang/p/10326302.html

  • docker-compose 文件
version: "3"
services:
  api:
    build: ./
    image: dalongrong/sofa-graphql2rest
    ports:
    - "4000:4000"
  webhook:
    image: jeffail/benthos
    volumes:
    - "./configs/webhook.yaml:/benthos.yaml"
    ports:
    - "4195:4195"
  • webhook config
input:
  type: broker
  broker:
    inputs:
      - type: http_server
        http_server:
          path: /post/customer1
        processors:
          - type: text
            text:
              operator: prepend
              value: "Customer 1 received: "
      - type: http_server
        http_server:
          path: /post/customer2
        processors:
          - type: text
            text:
              operator: prepend
              value: "Customer 2 received: "
      - type: http_server
        http_server:
          path: /post/customer3
        processors:
          - type: text
            text:
              operator: prepend
              value: "Customer 3 received: "
output:
  type: stdout

启动&&测试

  • 启动
docker-compose up -d
  • 添加webhook
    请求的同时会启动webhook 的rest 请求
    请求的参数格式如下:
{
 "subscription":"sub schema name ",
 "variables":" some field just for filter data",
 "url": "the webhook address"
}

使用curl 请求

curl -X POST \
  http://localhost:4000/webhook \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: f2098fda-1108-4981-83b4-7eae3f3828c8' \
  -H 'cache-control: no-cache' \
  -d '{
 "subscription":"onBook",
 "url":"http://webhook:4195"
}'

返回数据
包含了webhook 的id

curl -X POST \
  http://localhost:4000/webhook \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: f2098fda-1108-4981-83b4-7eae3f3828c8' \
  -H 'cache-control: no-cache' \
  -d '{
        "subscription":"onBook",
        "url":"http://webhook:4195"
}'
{"id":"2de735c3-5698-4cdd-9986-1941d1e20c88"}
  • 修改数据进行测试
    可以通过graphql 或者rest api

    webhook 数据
    docker-compose logs -f webhook
webhook_1 | get webhook message: {"data":{"onBook":{"id":"23337599919478412","title":"dalongdemo"}}}
  • webhook 订阅的删除以及修改
    删除操作
    DELETE /webhook/:id - 删除webhook
    POST /webhook/:id- 更新webhook

说明

sofa 的设计还是很不错的,给与我们一个反向的操作,一般我们的方式是rest2graphql ,但是这个是graphql2rest 同时提供了很不错的
功能支持

参考资料

https://github.com/Urigo/sofa
https://github.com/rongfengliang/sofa-graphql2rest-docker-compose

posted on   荣锋亮  阅读(239)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示