Ruby GraphQL actioncable 订阅:出现异常 - RuntimeError(无法实施 ListWasCreated.list,已尝试
Ruby GraphQL actioncable subscription : There was an exception - RuntimeError( Failed to implement ListWasCreated.list, tried
我正在尝试实时收听从我的 Vue.js 应用程序创建的新列表。
我已关注 graphql-ruby documentation 进行订阅。
当我从 rails console
创建新列表时,我在日志中收到一条错误消息:
日志
GraphqlChannel is transmitting the subscription confirmation
GraphqlChannel#execute({"query"=>"subscription listWasCreated {\n listWasCreated {\n list {\n title\n __typename\n }\n __typename\n }\n}\n", "variables"=>{}, "operationName"=>"listWasCreated"})
GraphqlChannel transmitting {:result=>{"data"=>{}}, :more=>true}
GraphqlChannel is streaming from graphql-subscription:c56670f6-d59c-438a-896c-ba8c2d91d375
GraphqlChannel is streaming from graphql-event::listWasCreated:
List Load (0.2ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = LIMIT [["id", "0946b254-c86c-4a53-b6fc-b1b29e54fd70"], ["LIMIT", 1]]
There was an exception - RuntimeError( Failed to implement ListWasCreated.list, tried:
- `#<Class:0x00007fa0af083ed0>#list`, which did not exist
- `List#list`, which did not exist
- Looking up hash key `:list` or `"list"` on `#<List:0x00007fa0dfbdc748>`, but it wasn't a Hash
To implement this field, define one of the methods above (and check for typos)
)
Ruby代码
# app/graphql/types/subscription_type.rb
class Types::SubscriptionType < Types::BaseObject
field :list_was_created, subscription: Subscriptions::ListWasCreated
end
# app/graphql/subscriptions/list_was_created.rb
module Subscriptions
class ListWasCreated < Subscriptions::BaseSubscription
field :list, Types::ListType, null: false
def list
List.last
end
end
end
# app/graphql/subscriptions/base_subscription.rb
module Subscriptions
class BaseSubscription < GraphQL::Schema::Subscription
# Hook up base classes
object_class Types::BaseObject
field_class Types::BaseField
argument_class Types::BaseArgument
end
end
# app/graphql/types/list_type.rb
module Types
class ListType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
field :status, String, null: false
end
end
# app/models/list.rb
class List < ApplicationRecord
# Callbacks
after_save :notify_subscriber
def notify_subscriber
ListouSchema.subscriptions.trigger(:list_was_created, {}, self)
end
end
GQL查询订阅
subscription listWasCreated {
listWasCreated {
list {
title
}
}
}
我的非理想解决方案(多个订阅时太乱了)
# app/graphql/types/subscription_type.rb
class Types::SubscriptionType < Types::BaseObject
field :list_was_created, Types::ListType, null: false
def list_was_created
List.last
end
end
subscription listWasCreated {
listWasCreated {
status
title
}
}
GraphqlChannel is transmitting the subscription confirmation
GraphqlChannel#execute({"query"=>"subscription listWasCreated {\n listWasCreated {\n status\n title\n __typename\n }\n}\n", "variables"=>{}, "operationName"=>"listWasCreated"})
List Load (0.3ms) SELECT "lists".* FROM "lists" ORDER BY "lists"."created_at" DESC LIMIT [["LIMIT", 1]]
↳ app/graphql/types/subscription_type.rb:12:in `list_was_created'
GraphqlChannel transmitting {:result=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:29:44+01:00", "__typename"=>"List"}}}, :more=>true}
GraphqlChannel is streaming from graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be
GraphqlChannel is streaming from graphql-event::listWasCreated:
List Load (0.2ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = LIMIT [["id", "e6561161-c15b-4cf3-8979-79085a1d1a94"], ["LIMIT", 1]]
List Load (0.2ms) SELECT "lists".* FROM "lists" ORDER BY "lists"."created_at" DESC LIMIT [["LIMIT", 1]]
↳ app/graphql/types/subscription_type.rb:12:in `list_was_created'
[ActionCable] Broadcasting to graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be: {:result=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:30:21+01:00", "__typename"=>"List"}}}, :more=>true}
GraphqlChannel transmitting {"result"=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:30:21+01:00", "__typename"=>"List"}}}, "more"=>true} (via streamed from graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be)
我想要什么
在app/graphql/types/subscription_type.rb
中使用field :list_was_created, subscription: Subscriptions::ListWasCreated
因为它不那么混乱而且我可以添加很多字段
我找到了解决方案...
我必须添加方法 subscribe
和 update
# app/graphql/subscriptions/list_was_created.rb
module Subscriptions
class ListWasCreated < Subscriptions::BaseSubscription
field :list, Types::ListType, null: false
def list
List.last
end
def subscribe
{
list: list
}
end
def update
{
list: list
}
end
end
end
我正在尝试实时收听从我的 Vue.js 应用程序创建的新列表。
我已关注 graphql-ruby documentation 进行订阅。
当我从 rails console
创建新列表时,我在日志中收到一条错误消息:
日志
GraphqlChannel is transmitting the subscription confirmation
GraphqlChannel#execute({"query"=>"subscription listWasCreated {\n listWasCreated {\n list {\n title\n __typename\n }\n __typename\n }\n}\n", "variables"=>{}, "operationName"=>"listWasCreated"})
GraphqlChannel transmitting {:result=>{"data"=>{}}, :more=>true}
GraphqlChannel is streaming from graphql-subscription:c56670f6-d59c-438a-896c-ba8c2d91d375
GraphqlChannel is streaming from graphql-event::listWasCreated:
List Load (0.2ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = LIMIT [["id", "0946b254-c86c-4a53-b6fc-b1b29e54fd70"], ["LIMIT", 1]]
There was an exception - RuntimeError( Failed to implement ListWasCreated.list, tried:
- `#<Class:0x00007fa0af083ed0>#list`, which did not exist
- `List#list`, which did not exist
- Looking up hash key `:list` or `"list"` on `#<List:0x00007fa0dfbdc748>`, but it wasn't a Hash
To implement this field, define one of the methods above (and check for typos)
)
Ruby代码
# app/graphql/types/subscription_type.rb
class Types::SubscriptionType < Types::BaseObject
field :list_was_created, subscription: Subscriptions::ListWasCreated
end
# app/graphql/subscriptions/list_was_created.rb
module Subscriptions
class ListWasCreated < Subscriptions::BaseSubscription
field :list, Types::ListType, null: false
def list
List.last
end
end
end
# app/graphql/subscriptions/base_subscription.rb
module Subscriptions
class BaseSubscription < GraphQL::Schema::Subscription
# Hook up base classes
object_class Types::BaseObject
field_class Types::BaseField
argument_class Types::BaseArgument
end
end
# app/graphql/types/list_type.rb
module Types
class ListType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
field :status, String, null: false
end
end
# app/models/list.rb
class List < ApplicationRecord
# Callbacks
after_save :notify_subscriber
def notify_subscriber
ListouSchema.subscriptions.trigger(:list_was_created, {}, self)
end
end
GQL查询订阅
subscription listWasCreated {
listWasCreated {
list {
title
}
}
}
我的非理想解决方案(多个订阅时太乱了)
# app/graphql/types/subscription_type.rb
class Types::SubscriptionType < Types::BaseObject
field :list_was_created, Types::ListType, null: false
def list_was_created
List.last
end
end
subscription listWasCreated {
listWasCreated {
status
title
}
}
GraphqlChannel is transmitting the subscription confirmation
GraphqlChannel#execute({"query"=>"subscription listWasCreated {\n listWasCreated {\n status\n title\n __typename\n }\n}\n", "variables"=>{}, "operationName"=>"listWasCreated"})
List Load (0.3ms) SELECT "lists".* FROM "lists" ORDER BY "lists"."created_at" DESC LIMIT [["LIMIT", 1]]
↳ app/graphql/types/subscription_type.rb:12:in `list_was_created'
GraphqlChannel transmitting {:result=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:29:44+01:00", "__typename"=>"List"}}}, :more=>true}
GraphqlChannel is streaming from graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be
GraphqlChannel is streaming from graphql-event::listWasCreated:
List Load (0.2ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = LIMIT [["id", "e6561161-c15b-4cf3-8979-79085a1d1a94"], ["LIMIT", 1]]
List Load (0.2ms) SELECT "lists".* FROM "lists" ORDER BY "lists"."created_at" DESC LIMIT [["LIMIT", 1]]
↳ app/graphql/types/subscription_type.rb:12:in `list_was_created'
[ActionCable] Broadcasting to graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be: {:result=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:30:21+01:00", "__typename"=>"List"}}}, :more=>true}
GraphqlChannel transmitting {"result"=>{"data"=>{"listWasCreated"=>{"status"=>"online", "title"=>"2021-01-31T18:30:21+01:00", "__typename"=>"List"}}}, "more"=>true} (via streamed from graphql-subscription:edf5ce43-8838-4212-a9d8-8e660bafc4be)
我想要什么
在app/graphql/types/subscription_type.rb
中使用field :list_was_created, subscription: Subscriptions::ListWasCreated
因为它不那么混乱而且我可以添加很多字段
我找到了解决方案...
我必须添加方法 subscribe
和 update
# app/graphql/subscriptions/list_was_created.rb
module Subscriptions
class ListWasCreated < Subscriptions::BaseSubscription
field :list, Types::ListType, null: false
def list
List.last
end
def subscribe
{
list: list
}
end
def update
{
list: list
}
end
end
end