prisma 服务器端订阅试用

graphql 协议是支持数据的实时订阅功能的(一般基于websocket 进行实现)
prisma 支持客户端订阅以及服务器端订阅(类似webhook),可以方便将
数据推送后端服务

目的

prisma 实现这个功能是方便集成当前的serverless 基础设施,当前支持的是通过webhook
的模式进行时间传递

参考配置

endpoint: ${env:PRISMA_ENDPOINT}
secret: ${env:PRISMA_SECRET}
datamodel: database/datamodel.graphql
subscriptions:
  userChangedEmail:
    webhook:
      url: http://example.org/sendSlackMessage
      headers:
        Content-Type: application/json
        Authorization: Bearer cha2eiheiphesash3shoofo7eceexaequeebuyaequ1reishiujuu6weisao7ohc
    query: |
      subscription {
        user(where: {
          mutation_in: [UPDATED]
        }) {
          node {
            name
            email
          }
        }
      }

触发条件

mutation {
    createUser(data:{
    name:"appdemo",
    age:444,
    version:"v1"
  }){
    id
    name
    age
    version
  }
}

参考项目

项目使用wiremock 进行webhook 模拟

  • 项目初始化

    使用prisma cli

prisma init 
  • 项目结构
├── datamodel.graphql
├── docker-compose.yml
├── prisma.yml
├── stubs
│ └── web.json
└── subscription.graphql
  • 代码说明
datamodel.graphql: graphql  schema 定义
type User {
id: ID! @unique
name: String!
age: Int
version: String!
}
docker-compose.yml  docker-compose 服务配置
version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.14
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        # uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security
        # managementApiSecret: my-secret
        databases:
          default:
            connector: mysql
            host: mysql
            port: 3306
            user: root
            password: prisma
            migrations: true
  mysql:
    image: mysql:5.7
    ports:
    - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: prisma
    volumes:
      - ./data/mysql:/var/lib/mysql
  mock:
    image: rodolpheche/wiremock
    ports:
      - "9089:8080"
    command: "--local-response-templating --verbose"
    volumes:
      - ./stubs:/home/wiremock/mappings

prisma.yml
endpoint: http://localhost:4466
datamodel: datamodel.graphql
subscriptions:
  welcomeNewUser:
    webhook: http://mock:8080/webhook
    query: subscription.graphql

stubs/web.json  (webhook mock service)
{
"request": {
"method": "POST",
"url": "/webhook"
},
"response": {
"status": 200,
"body":"{{{request.body}}}",
"headers": {
"TOKEN": "{{request.headers.TOKEN}}",
"URL":"{{request.url}}",
"content-type":"application/json"
},
"transformers": ["response-template"]
}
}
subscription.graphql 服务端订阅配置
subscription {
user(where:
{
mutation_in:CREATED
}){
node {
id
name
age
}
}
}

启动

因为当前的prisma cli 有一个处理上的bug,直接运行提示的错误如下:

Subscription
✖ The provided query doesn't include any known model name. Please check for the latest subscriptions API.

当前的解决办法是先注释 sss ,先prisma deploy 然后取消注释,prsima deploy --force

  • 启动docker-compose
docker-compose up -d
  • 部署graphql 服务
prisma deploy &&  prisma deploy --force  如果有错,参考上边的说明
  • 效果

测试功能

  • 创建用户请求
mutation {
  createUser(data:{
    name:"rong",
    age:444,
    version:"v1"
  }){
    id
    name
    age
    version
  }
}
  • mock webhook service 日志
mock_1 | Content-Length: [85]
mock_1 | Content-Type: [application/json]
mock_1 | {"data":{"user":{"node":{"id":"cjld7u53j000n0a50ph4mezug","name":"rong","age":444}}}}
mock_1 |
mock_1 |
mock_1 | Matched response definition:
mock_1 | {
mock_1 | "status" : 200,
mock_1 | "body" : "{\"data\":{\"user\":{\"node\":{\"id\":\"cjld7u53j000n0a50ph4mezug\",\"name\":\"rong\",\"age\":444}}}}",
mock_1 | "headers" : {
mock_1 | "TOKEN" : "",
mock_1 | "URL" : "/webhook",
mock_1 | "content-type" : "application/json"
mock_1 | },
mock_1 | "transformers" : [ "response-template" ]
mock_1 | }

参考资料

https://github.com/rongfengliang/prisma-sss-demo
https://www.prisma.io/docs/reference/server_side-subscriptions/overview-to1ahf0ob6
https://github.com/prisma/prisma/issues/2967

posted on 2018-08-28 12:51  荣锋亮  阅读(689)  评论(0编辑  收藏  举报

导航