我有一个基本的设置,更新广播与图形for红宝石行动电缆.
update方法。后端没有错误。触发呼叫似乎被默默地忽略了。这就是我想要解决的问题/正确理解它应该如何工作。我在看的医生:
订阅类型:
module Types
class SubscriptionType < Types::BaseObject
field :server_info, subscription: Subscriptions::ServerInfoSubscription, null: false
end
endmodule 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我遗漏了什么?有什么故障排除提示吗?
更新:
一些诊断:
Rails记录初始(成功)订阅调用的日志:
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:“通道上广播。我不明白为什么实际的Redis通道被称为"graphql-event::dateTime“,而订阅查询位于顶级"ServerInfo”类型。
发布于 2020-11-02 13:24:49
我终于让它起作用了,但我找不出为什么它不起作用:
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, String, null: false
field :payload, String, null: true
end
endMySchema.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"发布于 2022-01-19 13:50:34
这是因为如果您查看GraphQL::Schema::Subscription,您试图覆盖的方法是期望参数
def subscribe( attrs = {})
...
end
def update( attrs = {} )
...
end您可以安全地删除:argument并使用参数定义您的更新方法,这将解决这个问题。
def update( _attrs = {} )
# your logic here
endhttps://stackoverflow.com/questions/64197327
复制相似问题