class GraphQL::Schema::Validator::RequiredValidator


end
argument :age, Integer, required: :nullable
# ‘required: :nullable` means this argument must be given, but may be `null`
field :update_settings, AccountSettings do
@example require some value for an argument, even if it’s null
end
validates required: { one_of: [:node_id, [:object_type, :object_id]] }
# either a global ‘node_id` or an `object_type`/`object_id` pair is required:
argument :object_id, Integer, required: false
argument :object_type, String, required: false
argument :node_id, ID, required: false
field :find_object, Node, null: true do
@example Require one of these sets of arguments
end
validates required: { one_of: [:cups, :tablespoons, :teaspoons] }
argument :teaspoons, Integer, required: false
argument :tablespoons, Integer, required: false
argument :cups, Integer, required: false
argument :ingredient_id, ID, required: true
field :update_amount, IngredientAmount, null: false do
@example Require exactly one of these arguments
(This is for specifying mutually exclusive sets of arguments.)
Or, use Arrays of symbols to name a valid set of arguments.
Use this validator to require one of the named arguments to be present.

def arg_keyword_to_grapqhl_name(argument_definitions, arg_keyword)

def arg_keyword_to_grapqhl_name(argument_definitions, arg_keyword)
  argument_definition = argument_definitions.find { |defn| defn.keyword == arg_keyword }
  argument_definition.graphql_name
end

def build_message(context)

def build_message(context)
  argument_definitions = @validated.arguments(context).values
  required_names = @one_of.map do |arg_keyword|
    if arg_keyword.is_a?(Array)
      names = arg_keyword.map { |arg| arg_keyword_to_grapqhl_name(argument_definitions, arg) }
      "(" + names.join(" and ") + ")"
    else
      arg_keyword_to_grapqhl_name(argument_definitions, arg_keyword)
    end
  end
  if required_names.size == 1
    "%{validated} must include the following argument: #{required_names.first}."
  else
    "%{validated} must include exactly one of the following arguments: #{required_names.join(", ")}."
  end
end

def initialize(one_of: nil, argument: nil, message: nil, **default_options)

Parameters:
  • message (String) --
  • argument (Symbol) -- An argument that is required for this field
  • one_of (Array) -- A list of arguments, exactly one of which is required for this field
def initialize(one_of: nil, argument: nil, message: nil, **default_options)
  @one_of = if one_of
    one_of
  elsif argument
    [argument]
  else
    raise ArgumentError, "`one_of:` or `argument:` must be given in `validates required: {...}`"
  end
  @message = message
  super(**default_options)
end

def validate(_object, context, value)

def validate(_object, context, value)
  matched_conditions = 0
  if !value.nil?
    @one_of.each do |one_of_condition|
      case one_of_condition
      when Symbol
        if value.key?(one_of_condition)
          matched_conditions += 1
        end
      when Array
        if one_of_condition.all? { |k| value.key?(k) }
          matched_conditions += 1
          break
        end
      else
        raise ArgumentError, "Unknown one_of condition: #{one_of_condition.inspect}"
      end
    end
  end
  if matched_conditions == 1
    nil # OK
  else
    @message || build_message(context)
  end
end