2021-02-22 17:27:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Graphql
|
|
|
|
class Arguments
|
|
|
|
delegate :blank?, :empty?, to: :to_h
|
|
|
|
|
|
|
|
def initialize(values)
|
2022-08-13 15:12:31 +05:30
|
|
|
@values = values
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
@values
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
to_h == other&.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :eql, :==
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return '' if empty?
|
|
|
|
|
|
|
|
@values.map do |name, value|
|
|
|
|
value_str = as_graphql_literal(value)
|
|
|
|
|
|
|
|
"#{GraphqlHelpers.fieldnamerize(name.to_s)}: #{value_str}"
|
|
|
|
end.join(", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_graphql_literal(value)
|
|
|
|
self.class.as_graphql_literal(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Transform values to GraphQL literal arguments.
|
|
|
|
# Use symbol for Enum values
|
|
|
|
def self.as_graphql_literal(value)
|
|
|
|
case value
|
|
|
|
when ::Graphql::Arguments then "{#{value}}"
|
|
|
|
when Array then "[#{value.map { |v| as_graphql_literal(v) }.join(',')}]"
|
|
|
|
when Hash then "{#{new(value)}}"
|
|
|
|
when Integer, Float, Symbol then value.to_s
|
2022-07-16 23:28:13 +05:30
|
|
|
when String, GlobalID then "\"#{value.to_s.gsub(/"/, '\\"')}\""
|
2022-04-04 11:22:00 +05:30
|
|
|
when Time, Date then "\"#{value.iso8601}\""
|
2022-08-13 15:12:31 +05:30
|
|
|
when NilClass then 'null'
|
2021-02-22 17:27:13 +05:30
|
|
|
when true then 'true'
|
|
|
|
when false then 'false'
|
|
|
|
else
|
|
|
|
value.to_graphql_value
|
|
|
|
end
|
|
|
|
rescue NoMethodError
|
2022-07-16 23:28:13 +05:30
|
|
|
raise ArgumentError, "Cannot represent #{value} (instance of #{value.class}) as GraphQL literal"
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge(other)
|
|
|
|
self.class.new(@values.merge(other.to_h))
|
|
|
|
end
|
|
|
|
|
|
|
|
def +(other)
|
|
|
|
if blank?
|
|
|
|
other
|
|
|
|
elsif other.blank?
|
|
|
|
self
|
|
|
|
elsif other.is_a?(String)
|
|
|
|
[to_s, other].compact.join(', ')
|
|
|
|
else
|
|
|
|
merge(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|