常见参数 graphQL

Common arguments graphQL

我正在尝试在 graphql-ruby 中实现两个变更 - 一个用于创建资源,一个用于编辑。在大多数情况下,它们都从客户端获取完全相同的参数,因此我想避免重复突变并尝试在可重用的 class/module.

中指定参数

我正在使用 graphql-ruby 1.8 和新的基于 class 的 API,并从这个开始:

class Mutations::ApplicationMutation < GraphQL::Schema::Mutation
  ... common to every mutation ...
end

class Mutations::CreateResourceMutation < Mutations::ApplicationMutation
  argument :name, String, required: true
  argument :description, String
  argument :create_only_field, String
end

class Mutations::UpdateResourceMutation < Mutations::ApplicationMutation
  argument :name, String, required: true
  argument :description, String
  argument :update_only_field, String
end

在这个基本示例中,namedescription 属性在两个突变中是相同的。我已将解析器提取到另一个 class 中,以便可以重复使用,但我不确定处理参数的最佳方法。

我想 ActiveSupport::Concern 会起作用,但它感觉不对或我认为它应该起作用的方式,但我对 GraphQL 总体来说还很陌生,所以不知道。

我可以建议一个很好的解决方案,创建一个复杂的参数并在两个突变中使用。 这是代码

# graphql/inputs/resource_input.rb

Inputs::ResourceInput = GraphQL::InputObjectType.define do
  name 'ResourceInput'
  description 'An input object representing arguments for a user'

  argument :name, String, required: true
  argument :description, String
  argument :create_only_field, String
  argument :update_only_field, String
end

并且在突变中你可以只使用

 argument :resourceInput, Inputs::ResourceInput, "the resource complex input"

希望这能帮助您避免上当受骗