1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
# frozen_string_literal: true
module GraphQL
module StaticValidation
module OneOfInputObjectsAreValid
def on_input_object(node, parent)
return super unless parent.is_a?(GraphQL::Language::Nodes::Argument)
parent_type = get_parent_type(context, parent)
return super unless parent_type && parent_type.kind.input_object? && parent_type.one_of?
validate_one_of_input_object(node, context, parent_type)
super
end
private
def validate_one_of_input_object(ast_node, context, parent_type)
present_fields = ast_node.arguments.map(&:name)
input_object_type = parent_type.to_type_signature
if present_fields.count != 1
add_error(
OneOfInputObjectsAreValidError.new(
"OneOf Input Object '#{input_object_type}' must specify exactly one key.",
path: context.path,
nodes: ast_node,
input_object_type: input_object_type
)
)
return
end
field = present_fields.first
value = ast_node.arguments.first.value
if value.is_a?(GraphQL::Language::Nodes::NullValue)
add_error(
OneOfInputObjectsAreValidError.new(
"Argument '#{input_object_type}.#{field}' must be non-null.",
path: [*context.path, field],
nodes: ast_node.arguments.first,
input_object_type: input_object_type
)
)
return
end
if value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)
variable_name = value.name
variable_type = @declared_variables[variable_name].type
unless variable_type.is_a?(GraphQL::Language::Nodes::NonNullType)
add_error(
OneOfInputObjectsAreValidError.new(
"Variable '#{variable_name}' must be non-nullable to be used for OneOf Input Object '#{input_object_type}'.",
path: [*context.path, field],
nodes: ast_node,
input_object_type: input_object_type
)
)
end
end
end
end
end
end
|