我正在寻找与graphql教程中类似的方法:https ://graphql.org/learn/queries/#arguments
我想将英尺/米传递给定标器字段以转换返回的结果。
{
human(id: "1000") {
name
height(unit: FOOT)
}
}
我不知道如何使用graphql-ruby在ruby中做到这一点。
我目前有一个看起来像这样的类型:
class Types::Human < Types::BaseObject
field :id, ID, null: false
field :height, Int, null: true do
argument :unit, String, required: false
end
def height(unit: nil)
#what do I do here to access the current value of height so I can use unit to transform the result?
end
end
我发现对于返回的每个实例都调用了resolver方法(高度)...但是我不知道如何访问当前值。
谢谢
在类型定义中定义解析方法时,Ruby Graphql会假定该方法将解析该值。因此,在运行当前方法时def height(unit: nil)
,它不知道当前的高度值是什么,因为它期望您对其进行定义。
相反,您想要做的是将解析方法移至为该Human
类型返回的模型/对象。例如,在rails中,您可以这样做:
# app/graphql/types/human_type.rb
class Types::Human < Types::BaseObject
field :id, ID, null: false
field :height, Int, null: true do
argument :unit, String, required: false
end
end
# app/models/human.rb
class Human < ApplicationRecord
def height(unit: nil)
# access to height with self[:height].
# You must use self[:height] so as to access the height property
# and not call the method you defined. Note: This overrides the
# getter for height.
end
end
然后,GraphQL Ruby将调用.height
传递给它的人类实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句