我正在使用graphQL,并且在解决一个has_many :trough
连接的数据集时出现问题。
进入控制台的错误是: 开始
POST "/graphql" for 127.0.0.1 at 2017-09-25 19:28:24 -0400
Processing by GraphqlController#execute as */*
Parameters: {"query"=>"query{\n items{\n id\n title\n collections{\n id\n }\n }\n}", "variables"=>nil, "graphql"=>{"query"=>"query{\n items{\n id\n title\n collections{\n id\n }\n }\n}", "variables"=>nil}}
Item Load (0.7ms) SELECT `items`.* FROM `items`
Collection Load (0.6ms) SELECT `collections`.* FROM `collections` INNER JOIN `collection_item_crosses` ON `collections`.`id` = `collection_item_crosses`.`collection_id` WHERE `collection_item_crosses`.`item_id` = 1 LIMIT 11
Completed 500 Internal Server Error in 79ms (ActiveRecord: 4.0ms)
CACHE Collection Load (0.0ms) SELECT `collections`.* FROM `collections` INNER JOIN `collection_item_crosses` ON `collections`.`id` = `collection_item_crosses`.`collection_id` WHERE `collection_item_crosses`.`item_id` = 1 LIMIT 11 [["item_id", 1], ["LIMIT", 11]]
NoMethodError (undefined method `id' for #<Collection::ActiveRecord_Associations_CollectionProxy:0x007f6a38317d98>
Did you mean? ids):
app/controllers/graphql_controller.rb:12:in `execute'
我的查询如下:
query{
items{
id
title
collections{
id
}
}
}
我可以确认查询确实按照我的预期返回数据,但我不知道未定义的方法错误是指什么。
收藏类型:
Types::CollectionType = GraphQL::ObjectType.define do
name 'Collection'
field :id, !types.Int
field :collection_name, !types.String
field :collection_description, types.String
field :items, -> { Types::ItemType }
field :collectionItems, -> { Types::CollectionItemCrossType }
end
答案 0 :(得分:0)
:items
字段是一个集合。声明应该如下:
Types::CollectionType = GraphQL::ObjectType.define do
name 'Collection'
field :items, !types[Types::ItemType]
field :collectionItems, !types[Types::CollectionItemCrossType]
end
!types[]
代表任何其他类型的GraphQL::ListType
。