class GraphqlConnector::Formatters::BaseFormat
Class that returns in query or mutation string format
Public Class Methods
new(model, conditions, selected_fields)
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 7 def initialize(model, conditions, selected_fields) @model = model @conditions = conditions @selected_fields = selected_fields end
Public Instance Methods
create()
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 13 def create <<-STRING #{query_type} { #{@model}#{arguments} { #{parse_fields(@selected_fields)} } } STRING end
Private Instance Methods
arguments()
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 25 def arguments conditions = @conditions.each_with_object([]) do |(key, value), array| array << "#{key}: #{value_as_parameter(value)}" end return '' if conditions.empty? "(#{conditions.join(', ')})" end
handle_association(hash)
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 70 def handle_association(hash) hash.map do |key, fields| "#{key} { #{parse_fields(fields)} }" end end
parse_fields(selected_fields)
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 57 def parse_fields(selected_fields) results = selected_fields.map do |field| case field when Hash handle_association(field) else field end end results.join(' ') end
query_type()
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 76 def query_type raise 'query_type undefined' end
scalar_types(value)
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 48 def scalar_types(value) case value when TrueClass, FalseClass, Integer, Float value else # fallback to string '"' + value.to_s + '"' end end
value_as_parameter(value)
click to toggle source
# File lib/graphql_connector/formatters/base_format.rb, line 35 def value_as_parameter(value) case value when Array casted_values = value.map { |v| value_as_parameter(v) } "[#{casted_values.join(',')}]" when Hash casted_values = value.map { |k, v| "#{k}: #{value_as_parameter(v)}" } "{#{casted_values.join(',')}}" else scalar_types(value) end end