首页 > 解决方案 > 带有数组字段的 GraphQL ObjectType 返回 null

问题描述

我有一个字段是我想在解析器中定义的数组。问题是,我的结果不返回作为数组的字段,而只是一个具有空值的对象。


  field :pies do 
    type types[Types::OutputType]
    resolve -> (_obj, _args, ctx){
      pies = []
      CookBook.pages.each do |page|
        labels = []
        labels << OpenStruct.new(
          id: 1,
          name: "blueberry"
        )
        pie = OpenStruct.new(
          id: 1,
          labels: labels
        )
        pies << pie
      end
      pies
    }
  end
Types::OutputType = GraphQL::ObjectType.define do
    name 'OutputType'

    field :id, types.ID
    field :labels, types[Types::LabelType]
end
Types::LabelType = GraphQL::ObjectType.define do
    name 'LabelType'
    field :id, types.ID!
    field :name, types.String
end

这是查询:

# Write your query or mutation here
{
  pies{
    id,
    labels{
        name
    }
  }
}

这是结果:

{
  "data": {
    "pies": [
      {
        "id": "9",
        "labels": {
          "name": null
        }
      },
      {
        "id": "3",
        "labels": {
          "name": null
        }
      },
      ...
    ]
  }
}

为什么我的标签字段不返回数组而只返回带有空值的对象?

标签: ruby-on-railsrubygraphqlgraphql-ruby

解决方案


推荐阅读