如何在GraphQL(Rails)中返回ActiveRecord关系?

How to return ActiveRecord Relation in GraphQL (Rails)?

GraphQL查询可以直接定义是否搜索记录,例如按其ID或确切值(在我的情况下为日期)。

我的查询应该返回的不是ActiveRecord对象,而是ActiveRecord Relation。如何通过GraphQL将其定义为可消耗的?

1
2
3
4
5
6
7
  field :file_data, !FileDataType," Returns records based on given date: format 'yyyy-mm-dd hh:mm:ss'" do
  argument :created_on, !types.String,"format 'yyyy-mm-dd hh:mm:ss'"

  resolve -> (obj, args, context) do
    FileData.where("created_on > ?", args["created_on"])
  end
end

它适用于一个对象:如果我添加到关系" last":

1
      FileData.where("created_on > ?", args["created_on"]).last

对于许多结果,我在Rails控制台中遇到错误:

NoMethodError(未定义的方法" attribute_name" [任何已定义的方法


使用列表类型返回多个对象:

1
2
3
field :file_data, types[FileDataType] do
  ...
end

types[...]定义列表类型。