Class: ObjectToGraphQL::ObjectParser

Inherits:
Object
  • Object
show all
Defined in:
lib/object_to_graphql/object_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, arguments, variables) ⇒ ObjectParser

Returns a new instance of ObjectParser.



11
12
13
14
15
# File 'lib/object_to_graphql/object_parser.rb', line 11

def initialize(object, arguments, variables)
  @object = object
  @original_arguments = arguments
  @original_variables = variables
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/object_to_graphql/object_parser.rb', line 7

def object
  @object
end

#original_argumentsObject (readonly)

Returns the value of attribute original_arguments.



8
9
10
# File 'lib/object_to_graphql/object_parser.rb', line 8

def original_arguments
  @original_arguments
end

#original_variablesObject (readonly)

Returns the value of attribute original_variables.



9
10
11
# File 'lib/object_to_graphql/object_parser.rb', line 9

def original_variables
  @original_variables
end

Class Method Details

.parse(object, arguments = [], variables = []) ⇒ Object



3
4
5
# File 'lib/object_to_graphql/object_parser.rb', line 3

def self.parse(object, arguments = [], variables = [])
  new(object, arguments, variables).parse
end

Instance Method Details

#argument_value(str) ⇒ Object



64
65
66
67
68
69
70
# 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

#argumentsObject



54
55
56
57
58
59
60
61
62
# 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 = []) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#parseObject



17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



50
51
52
# 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) ⇒ Object



79
80
81
# File 'lib/object_to_graphql/object_parser.rb', line 79

def variable_name(str)
  str.delete_prefix("$")
end

#variable_type(str) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# 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

#variablesObject



72
73
74
75
76
77
# 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