class GraphQL::Schema::Validator::RequiredValidator

Use this validator to require one of the named arguments to be present. Or, use Arrays of symbols to name a valid set of arguments.

(This is for specifying mutually exclusive sets of arguments.)

If you use GraphQL::Schema::Visibility to hide all the arguments in a one_of: [..] set, then a developer-facing GraphQL::Error will be raised during execution. Pass allow_all_hidden: true to skip validation in this case instead.

This validator also implements argument ... required: :nullable. If an argument has required: :nullable but it’s hidden with GraphQL::Schema::Visibility, then this validator doesn’t run.

Examples

Example: Require exactly one of these arguments

field :update_amount, IngredientAmount, null: false do
  argument :ingredient_id, ID, required: true
  argument :cups, Integer, required: false
  argument :tablespoons, Integer, required: false
  argument :teaspoons, Integer, required: false
  validates required: { one_of: [:cups, :tablespoons, :teaspoons] }
end

Example: Require one of these sets of arguments

field :find_object, Node, null: true do
  argument :node_id, ID, required: false
  argument :object_type, String, required: false
  argument :object_id, Integer, required: false
  # either a global `node_id` or an `object_type`/`object_id` pair is required:
  validates required: { one_of: [:node_id, [:object_type, :object_id]] }
end

Example: require some value for an argument, even if it’s null

field :update_settings, AccountSettings do
  # `required: :nullable` means this argument must be given, but may be `null`
  argument :age, Integer, required: :nullable
end