【问题标题】:How to define a type holding an array of arrays?如何定义一个包含数组数组的类型?
【发布时间】:2021-07-02 07:24:08
【问题描述】:

使用 GraphQL-ruby,我想为一个字段定义一个类型,该字段在以下结构中保存数据:

[
  ["2016-06-07", 14134.84],
  ["2016-06-08", 14134.84],
  # ...
]

我该怎么做? 我试过了

module Types
  class PerformanceDataType < Types::BaseObject
    field :assets, [[Types::AssetType]], null: false
    # ....
  end
end

module Types
  class AssetType < Types::BaseObject
    field :date, String, null: false
    field :value, Float, null: false
  end
end

我还没有使用这些数据,所以我不能说它是否有效,但感觉太不具体了。生成架构没有引发任何错误。

【问题讨论】:

    标签: graphql-ruby


    【解决方案1】:

    我们最终做了如下:

    # innerArrayType.rb
    
    module Types
      class InnerArrayType < Types::BaseObject
        field :first_value, GraphQL::Types::ISO8601Date, null: false
        field :second_value, Types::DecimalType, null: false
      end
    end
    
    # someOtherType.rb
    
    module Types
      class SomeDataType < Types::BaseObject
        field :desiredType, [Types::InnerArrayType], null: false
      end
    end
    

    我想得出的结论是,在最低级别上,必须使用 Types::BaseObject 定义一个数组。

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2017-03-08
      • 2020-01-02
      • 1970-01-01
      • 2018-05-09
      • 2018-09-19
      • 2018-03-06
      • 2021-11-04
      • 2023-02-05
      相关资源
      最近更新 更多