Class: GraphQL::Client::QueryResult
- Inherits:
-
Object
- Object
- GraphQL::Client::QueryResult
- Defined in:
- lib/graphql/client/query_result.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .cast(obj) ⇒ Object
-
.define(name:, source_node:, fields: {}) ⇒ Object
Internal.
- .fields ⇒ Object
- .inspect ⇒ Object
- .name ⇒ Object
- .new(obj) ⇒ Object
- .source_node ⇒ Object
-
.spreads(node) ⇒ Object
Internal.
-
.wrap(node, name: nil) ⇒ Object
Internal: Get QueryResult class for result of query.
- .|(other) ⇒ Object
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
176 177 178 179 180 |
# File 'lib/graphql/client/query_result.rb', line 176 def method_missing(*args) super rescue NoMethodError => e raise NoMethodError, "undefined method `#{e.name}' for #{inspect}" end |
Instance Attribute Details
#data ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute data.
165 166 167 |
# File 'lib/graphql/client/query_result.rb', line 165 def data @data end |
Class Method Details
.cast(obj) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/graphql/client/query_result.rb', line 104 def self.cast(obj) case obj when Hash new(obj) when self return obj when QueryResult spreads = Set.new(self.spreads(obj.class.source_node).map(&:name)) if !spreads.include?(self.source_node.name) = "couldn't cast #{obj.inspect} to #{self.inspect}\n\n" suggestion = "\n ...#{name || "YourFragment"} # SUGGESTION" << GraphQL::Language::Generation.generate(obj.class.source_node).sub(/\n}$/, "#{suggestion}\n}") raise TypeError, end cast(obj.to_h) when Array obj.map { |e| cast(e) } when NilClass nil else raise TypeError, "#{obj.class}" end end |
.define(name:, source_node:, fields: {}) ⇒ Object
Internal
32 33 34 35 36 37 38 39 40 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 |
# File 'lib/graphql/client/query_result.rb', line 32 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) if field != field_alias send :alias_method, field_alias, field end class_eval <<-RUBY, __FILE__, __LINE__ def #{field_alias}? #{field_alias} ? true : false end RUBY if 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 end assigns = fields.map do |field, type| if type <<-RUBY @#{field} = self.class.fields[:#{field}].cast(@data["#{field}"]) RUBY else <<-RUBY @#{field} = @data["#{field}"] RUBY end end class_eval <<-RUBY, __FILE__, __LINE__ def initialize(data) @data = data #{assigns.join("\n")} freeze end RUBY end end |
.fields ⇒ Object
92 93 94 |
# File 'lib/graphql/client/query_result.rb', line 92 def self.fields @fields end |
.inspect ⇒ Object
100 101 102 |
# File 'lib/graphql/client/query_result.rb', line 100 def self.inspect "#<#{self.name} fields=#{@fields.keys.inspect}>" end |
.name ⇒ Object
96 97 98 |
# File 'lib/graphql/client/query_result.rb', line 96 def self.name @name || super || GraphQL::Client::QueryResult.name end |
.new(obj) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/graphql/client/query_result.rb', line 143 def self.new(obj) case obj when Hash super else cast(obj) end end |
.source_node ⇒ Object
88 89 90 |
# File 'lib/graphql/client/query_result.rb', line 88 def self.source_node @source_node end |
.spreads(node) ⇒ Object
Internal
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/graphql/client/query_result.rb', line 130 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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/graphql/client/query_result.rb', line 11 def self.wrap(node, name: nil) fields = {} node.selections.each do |selection| case selection when Language::Nodes::FragmentSpread 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
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/graphql/client/query_result.rb', line 152 def self.|(other) new_fields = self.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: self.source_node, fields: new_fields) end |
Instance Method Details
#inspect ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/graphql/client/query_result.rb', line 168 def inspect ivars = (self.class.fields.keys).map { |sym| "#{sym}=#{instance_variable_get("@#{sym}").inspect}" } buf = "#<#{self.class.name}" buf << " " << ivars.join(" ") if ivars.any? buf << ">" buf end |