graphql-ruby订阅无法正常播放

问题描述

我有一个准系统设置,可以通过Action Cable用graphql-ruby更新广播。

  • 客户端将收到初始的有效载荷,用于订阅
  • 客户端未收到任何更新,并且未调用订阅类型的update方法。后端没有错误。触发器调用似乎被静忽略。这就是我要解决的问题/正确理解它应该如何工作。

我正在查看的文档:

订阅类型:

module Types
  class SubscriptionType < Types::BaSEObject
    field :server_info,subscription: Subscriptions::ServerInfoSubscription,null: false
  end
end
module Subscriptions
  class ServerInfoSubscription < Subscriptions::BaseSubscription
    field :date_time,GraphQL::Types::ISO8601DateTime,null: false

    def subscribe
      { date_time: DateTime.Now }
    end

    def update
      puts 'UPDATE CALLED' # nope,it's not being called
      super
    end
  end
end

我尝试触发更新的方式:

MySchema.subscriptions.trigger(:server_info,{},{ date_time: DateTime.Now })

该模式定义为:

class MySchema < GraphQL::Schema
  use GraphQL::Execution::Interpreter
  use GraphQL::Subscriptions::ActionCableSubscriptions

  subscription(Types::SubscriptionType)
end

我在config / cable.yml中将redis适配器用于开发环境:

development:
  adapter: redis
  url: <%= ENV.fetch("REdis_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: test-app_development

我想念什么?有任何疑难解答提示吗?

更新

一些诊断:

为初始(成功的)订阅呼叫跟踪日志:

GraphqlChannel#execute({"query"=>"subscription ServerInfoSubscription {\n  serverInfo {\n    dateTime\n    __typename\n  }\n}\n","variables"=>{},"operationName"=>"ServerInfoSubscription"})
GraphqlChannel transmitting {"data"=>{"serverInfo"=>{"dateTime"=>"2020-10-08T10:47:08-04:00","__typename"=>"ServerInfoSubscriptionPayload"}}}
GraphqlChannel is streaming from graphql-subscription:9087735b-9b99-4d66-8b77-ff3622c8efe7
GraphqlChannel is streaming from graphql-event::dateTime:
Started POST "/graphql" for 192.168.64.210 at 2020-10-08 10:47:08 -0400

检查活动的Redis PubSub频道:

$ redis-cli -h 192.168.64.210 -p 6379 pubsub channels
1) "test-app_development:graphql-subscription:9087735b-9b99-4d66-8b77-ff3622c8efe7"
2) "test-app_development:graphql-event::dateTime:"
3) "_action_cable_internal"

在Rails控制台中运行触发调用的结果:

irb(main):001:0> MySchema.subscriptions.trigger(:server_info,{ date_time: DateTime.Now })
[ActionCable] broadcasting to graphql-event::serverInfo:: "{\"date_time\":\"2020-10-08T10:54:18-04:00\",\"__sym_keys__\":[\"date_time\"]}"

最明显的是,Redis PubSub通道称为“ graphql-event :: dateTime:”,而触发调用通过不存在的“ graphql-event :: serverInfo:”通道进行广播。我不明白为什么订阅查询为顶级“ ServerInfo”类型时,实际的Redis通道为何称为“ graphql-event :: dateTime”。

解决方法

我终于让它工作了,但无法指出为什么它不起作用:

MySchema.subscriptions.trigger(:server_info,{},{ date_time: DateTime.now })

请注意,我一直在传递一个空的哈希({})作为第二个参数。

我可以使用的方法是在查询中添加argument并使用匹配的参数(与原始订阅查询匹配)。像这样:

module Subscriptions
  class EventReceivedSubscription < Subscriptions::BaseSubscription
    argument :event_name,String,required: true

    field :event_name,null: false
    field :payload,null: true
  end
end
MySchema.subscriptions.trigger(
  :event_received,{ event_name: 'greeting' },{ payload: 'Hello' }
)

我现在看到的Redis频道是:

$ redis-cli -h 192.168.64.210 -p 6379 pubsub channels
1) "test-app_development:graphql-event::eventReceived:eventName:greeting"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...