class ObjectToGraphQL::ObjectParser

Attributes

object[R]
original_arguments[R]
original_variables[R]

Public Class Methods

new(object, arguments, variables) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 11
def initialize(object, arguments, variables)
  @object = object
  @original_arguments = arguments
  @original_variables = variables
end
parse(object, arguments = [], variables = []) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 3
def self.parse(object, arguments = [], variables = [])
  new(object, arguments, variables).parse
end

Public Instance Methods

argument_value(str) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 64
def argument_value(str)
  if str.start_with?("$")
    Nodes::VariableIdentifier.new(name: str.delete_prefix("$"))
  else
    str
  end
end
arguments() click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 54
def arguments
  @arguments ||= original_arguments.map do |(route, argument)|
    [
      route,
      Nodes::Argument.new(name: argument[:name],
                          value: argument_value(argument[:value]))
    ]
  end
end
extract_selections(object, path = []) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 28
def extract_selections(object, path = [])
  object.map do |key, value|
    lower_camelized_key = key.to_s.camelize(:lower)
    current_path = path + [key]

    case value
    when Hash
      Nodes::Field.new(name: lower_camelized_key,
                       selections: extract_selections(value, current_path),
                       arguments: select_arguments_for(current_path))
    when Array
      Nodes::Field.new(name: lower_camelized_key,
                       selections: extract_selections(value.first, current_path),
                       arguments: select_arguments_for(current_path))
    else
      Nodes::Field.new(name: lower_camelized_key,
                       value: value,
                       arguments: select_arguments_for(current_path))
    end
  end
end
parse() click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 17
def parse
  selections = extract_selections(object, [])
  operation_type = variables.empty? ? nil : "query"
  operation_definition = Nodes::OperationDefinition.new(name: nil,
                                                        selections: selections,
                                                        operation_type: operation_type,
                                                        variables: variables)

  Nodes::Document.new(definitions: [operation_definition])
end
select_arguments_for(path) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 50
def select_arguments_for(path)
  arguments.select { |(route, _)| route == path }.map(&:last)
end
variable_name(str) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 79
def variable_name(str)
  str.delete_prefix("$")
end
variable_type(str) click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 83
def variable_type(str)
  not_null = str.end_with?("!")
  name = str.delete_suffix("!")

  type_name = ObjectToGraphQL::Nodes::TypeName.new(name: name)

  if not_null
    ObjectToGraphQL::Nodes::NonNullType.new(of_type: type_name)
  else
    type_name
  end
end
variables() click to toggle source
# File lib/object_to_graphql/object_parser.rb, line 72
def variables
  @variables ||= original_variables.map do |variable|
    Nodes::VariableDefinition.new(name: variable_name(variable[:name]),
                                  type: variable_type(variable[:type]))
  end
end