Class: GraphQL::Client::QueryResult

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/client/query_result.rb

Overview

A QueryResult struct wraps data returned from a GraphQL response.

Wrapping the JSON-like Hash allows access with nice Ruby accessor methods rather than using ‘obj` access.

Wrappers also limit field visibility to fragment definitions.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



195
196
197
198
199
# File 'lib/graphql/client/query_result.rb', line 195

def method_missing(*args)
  super
rescue NoMethodError => e
  raise NoMethodError, "undefined method `#{e.name}' for #{inspect}"
end

Class Attribute Details

.fieldsObject (readonly)

Returns the value of attribute fields.



99
100
101
# File 'lib/graphql/client/query_result.rb', line 99

def fields
  @fields
end

.source_nodeObject (readonly)

Returns the value of attribute source_node.



97
98
99
# File 'lib/graphql/client/query_result.rb', line 97

def source_node
  @source_node
end

Instance Attribute Details

#dataObject (readonly) Also known as: to_h

Returns the value of attribute data.



177
178
179
# File 'lib/graphql/client/query_result.rb', line 177

def data
  @data
end

#errorsObject (readonly)

Public: Return errors associated with data.

Returns Errors collection.



175
176
177
# File 'lib/graphql/client/query_result.rb', line 175

def errors
  @errors
end

Class Method Details

.[](name) ⇒ Object



101
102
103
# File 'lib/graphql/client/query_result.rb', line 101

def [](name)
  fields[name]
end

.cast(obj, errors = Errors.new) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/graphql/client/query_result.rb', line 114

def self.cast(obj, errors = Errors.new)
  case obj
  when Hash
    new(obj, errors)
  when self
    obj
  when QueryResult
    spreads = Set.new(self.spreads(obj.class.source_node).map(&:name))

    unless spreads.include?(source_node.name)
      raise TypeError, "couldn't cast #{obj.inspect} to #{inspect}"
    end
    cast(obj.to_h, obj.errors)
  when Array
    List.new(obj.each_with_index.map { |e, idx| cast(e, errors.filter_by_path(idx)) }, errors)
  when NilClass
    nil
  else
    raise TypeError, obj.class.to_s
  end
end

.define(name:, source_node:, fields: {}) ⇒ Object

Internal



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/graphql/client/query_result.rb', line 41

def self.define(name:, source_node:, fields: {})
  Class.new(self) do
    @name = name
    @source_node = source_node
    @fields = {}

    fields.each do |field, type|
      @fields[field.to_sym] = type

      send :attr_reader, field

      # Convert GraphQL camelcase to snake case: commitComments -> commit_comments
      field_alias = ActiveSupport::Inflector.underscore(field)
      send :alias_method, field_alias, field if field != field_alias

      class_eval <<-RUBY, __FILE__, __LINE__
        def #{field_alias}?
          #{field_alias} ? true : false
        end
      RUBY

      next unless field == "edges"
      class_eval <<-RUBY, __FILE__, __LINE__
        def each_node
          return enum_for(:each_node) unless block_given?
          edges.each { |edge| yield edge.node }
          self
        end
      RUBY
    end

    assigns = fields.map do |field, type|
      if type
        <<-RUBY
          @#{field} = self.class.fields[:#{field}].cast(@data["#{field}"], @errors.filter_by_path("#{field}"))
        RUBY
      else
        <<-RUBY
          @#{field} = @data["#{field}"]
        RUBY
      end
    end

    class_eval <<-RUBY, __FILE__, __LINE__
      def initialize(data, errors = Errors.new)
        @data = data
        @errors = errors

        #{assigns.join("\n")}
        freeze
      end
    RUBY
  end
end

.inspectObject



110
111
112
# File 'lib/graphql/client/query_result.rb', line 110

def self.inspect
  "#<#{name} fields=#{@fields.keys.inspect}>"
end

.nameObject



106
107
108
# File 'lib/graphql/client/query_result.rb', line 106

def self.name
  @name || super || GraphQL::Client::QueryResult.name
end

.new(obj, *args) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/graphql/client/query_result.rb', line 150

def self.new(obj, *args)
  case obj
  when Hash
    super
  else
    cast(obj, *args)
  end
end

.spreads(node) ⇒ Object

Internal



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/graphql/client/query_result.rb', line 137

def self.spreads(node)
  node.selections.flat_map do |selection|
    case selection
    when Language::Nodes::FragmentSpread
      selection
    when Language::Nodes::InlineFragment
      spreads(selection)
    else
      []
    end
  end
end

.wrap(node, name: nil) ⇒ Object

Internal: Get QueryResult class for result of query.

Returns subclass of QueryResult or nil.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/client/query_result.rb', line 19

def self.wrap(node, name: nil)
  fields = {}

  node.selections.each do |selection|
    case selection
    when Language::Nodes::FragmentSpread
      nil
    when Language::Nodes::Field
      field_name = selection.alias || selection.name
      field_klass = selection.selections.any? ? wrap(selection, name: "#{name}[:#{field_name}]") : nil
      fields[field_name] ? fields[field_name] |= field_klass : fields[field_name] = field_klass
    when Language::Nodes::InlineFragment
      wrap(selection, name: name).fields.each do |fragment_name, klass|
        fields[fragment_name.to_s] ? fields[fragment_name.to_s] |= klass : fields[fragment_name.to_s] = klass
      end
    end
  end

  define(name: name, source_node: node, fields: fields)
end

.|(other) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/graphql/client/query_result.rb', line 159

def self.|(other)
  new_fields = fields.dup
  other.fields.each do |name, value|
    if new_fields[name]
      new_fields[name] |= value
    else
      new_fields[name] = value
    end
  end
  # TODO: Picking first source node seems error prone
  define(name: self.name, source_node: source_node, fields: new_fields)
end

Instance Method Details

#inspectObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/graphql/client/query_result.rb', line 180

def inspect
  ivars = self.class.fields.keys.map do |sym|
    value = instance_variable_get("@#{sym}")
    if value.is_a?(QueryResult)
      "#{sym}=#<#{value.class.name}>"
    else
      "#{sym}=#{value.inspect}"
    end
  end
  buf = "#<#{self.class.name}"
  buf << " " << ivars.join(" ") if ivars.any?
  buf << ">"
  buf
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/graphql/client/query_result.rb', line 201

def respond_to_missing?(*args)
  super
end