2017-09-25 53 views
0

我在使用graphQL进行操作,并且在拉取数据集时出现问题,这是一个has_many :trough连接。Rails Grapql has_many连接错误

错误即时得到在康寿是: 开始

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

:items字段是一个集合。因此,声明应该是这样的:

Types::CollectionType = GraphQL::ObjectType.define do 
    name 'Collection' 

    field :items, !types[Types::ItemType] 
    field :collectionItems, !types[Types::CollectionItemCrossType] 
end 

!types[]代表任何其他类型的GraphQL::ListType