sofa graphql 2 rest api webhook 試用

sofa 的webhook實際上就是將graphql 的subscription 進行了擴展,當接受到sub 請求的時候
再作一次http 的轉發處理,方便rest api 的訪問html

環境準備

環境仍是使用的docker-compose ,之前的代碼,只是添加了一個webhook 的容器鏡像
能夠參考: http://www.javashuo.com/article/p-vysuzxim-my.htmlgit

  • 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 請求github

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 的idweb

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 同時提供了很不錯的
功能支持docker

參考資料

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

相關文章
相關標籤/搜索