如何使用传递给Graphql ruby字段的参数来转换结果?

RMK

我正在寻找与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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将参数传递给graphql片段?

如何将参数传递给graphql查询?

如何使用 | 将多个参数传递给 Ruby 方法?

将参数传递给graphQL查询

GraphQL将参数传递给子解析

将复杂的参数传递给GraphQL突变

将多个参数传递给GraphQL查询

如何将具有多个对象的状态数组作为参数传递给graphql突变?

如何使用 Flask 将 SQL 结果传递给 HTML

如何使用重复字段将动态值传递给JSON

如何使用空格将值传递给输入字段的值

如何使用withColumn将额外的参数传递给UDF

如何使用__new__将多个参数传递给类?

如何使用ninject将参数传递给客户提供程序

如何使用AndroidJavaObject.Call将参数传递给Java?

如何将通用参数传递给使用对象的方法?

如何使用angularjs服务将表单参数传递给Rest?

如何使用 Pester 验证传递给模拟函数的参数的值

如何使用JavaScript XMLHttpRequest将多个参数传递给@RequestBody

如何使用Gradle将参数传递给main方法?

如何使用“ -c”选项将参数传递给EXPECT?

如何使用form_for将参数正确传递给对象?

如何使用气流的 DockerOperator 将参数传递给 docker 容器

如何使用 HTMX 和 Django 将参数传递给视图?

如何使用Guice将参数传递给提供程序?

如何将参数传递给使用... mapActions(...)映射的函数?

如何使用Mockito匹配传递给Class <T>的参数的null

如何使用ctest将参数传递给memcheck?

如何使用get JSON将参数传递给php