Class: GraphitiGql::ActiveResource::Proxy
- Inherits:
-
Object
- Object
- GraphitiGql::ActiveResource::Proxy
- Defined in:
- lib/graphiti_gql/active_resource.rb
Instance Method Summary collapse
- #data ⇒ Object
- #edges ⇒ Object
- #error_messages ⇒ Object
- #errors ⇒ Object
-
#initialize(resource, params, ctx, query, options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #node(id = nil) ⇒ Object
- #nodes ⇒ Object (also: #to_a)
- #page_info ⇒ Object
-
#query ⇒ Object
barf.
- #response ⇒ Object
- #run!(symbolize_keys: true) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(resource, params, ctx, query, options = {}) ⇒ Proxy
Returns a new instance of Proxy.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/graphiti_gql/active_resource.rb', line 58 def initialize(resource, params, ctx, query, = {}) @query = query @resource = resource @ctx = ctx @params = params.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym } (@params[:sort] || []).each do |sort| sort[:att] = sort[:att].to_s.camelize(:lower) sort[:dir] = sort[:dir].to_s end @with_pagination = !![:with_pagination] end |
Instance Method Details
#data ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/graphiti_gql/active_resource.rb', line 101 def data if response.key?(:data) response[:data] else raise "Tried to access 'data', but these errors were returned instead: #{.join(". ")}." end end |
#edges ⇒ Object
117 118 119 |
# File 'lib/graphiti_gql/active_resource.rb', line 117 def edges data[data.keys.first][:edges] end |
#error_messages ⇒ Object
113 114 115 |
# File 'lib/graphiti_gql/active_resource.rb', line 113 def response[:errors].map { |e| e[:message] } end |
#errors ⇒ Object
109 110 111 |
# File 'lib/graphiti_gql/active_resource.rb', line 109 def errors response[:errors] end |
#node(id = nil) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/graphiti_gql/active_resource.rb', line 77 def node(id = nil) if @resource.singular data # fire query Node.new(underscore(data[data.keys.first]), @resource) else nodes.find { |n| n.id == id.to_s || n.id == @resource.gid(id) } end end |
#nodes ⇒ Object Also known as: to_a
86 87 88 89 90 91 92 93 94 |
# File 'lib/graphiti_gql/active_resource.rb', line 86 def nodes return [] unless data elements = if edges edges.map { |e| e[:node] } else data[data.keys.first][:nodes] || [] end elements.compact.map { |n| Node.new(underscore(n), @resource) } end |
#page_info ⇒ Object
125 126 127 |
# File 'lib/graphiti_gql/active_resource.rb', line 125 def page_info underscore(data[data.keys.first][:pageInfo]) end |
#query ⇒ Object
barf
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/graphiti_gql/active_resource.rb', line 130 def query return @query if @query name = Schema.registry.key_for(@resource) sortvar = "$sort: [#{name}Sort!]," if @resource.sorts.any? filter_bang = "!" if @resource.grouped_filters.any? || @resource.filters.values.any? { |f| f[:required] } filtervar = "$filter: #{name}Filter#{filter_bang}," if @resource.filters.any? if !(fields = @params[:fields]) fields = [] @resource.attributes.each_pair do |name, config| (fields << name) if config[:readable] end end q = %| query #{name} ( #{filtervar} #{sortvar} $first: Int, $last: Int, $before: String, $after: String, ) { #{@resource.graphql_entrypoint} ( #{ 'filter: $filter,' if filtervar } #{ 'sort: $sort,' if sortvar } first: $first, last: $last, before: $before, after: $after, ) { edges { node {| fields.each do |name| next if name.is_a?(Hash) q << %| #{name.to_s.camelize(:lower)}| end if @params[:include] includes = Array(@params[:include]) # NB HASH (?) includes.each do |inc| sideload = @resource.sideload(inc.to_sym) if sideload.nil? raise "Passed option \"include: #{inc}\" but #{@resource} does not have an association with that name!" end to_one = [:belongs_to, :has_one, :polymorphic_belongs_to].include?(sideload.type) indent = " " if !to_one edge_fields = [] runtime_sideload_fields = @params[:fields].find { |f| f.is_a?(Hash) && f.key?(inc.to_sym) } if runtime_sideload_fields runtime_sideload_fields = runtime_sideload_fields.values edge_fields = runtime_sideload_fields.find { |f| f.is_a?(Hash) && f.key?(:edge) } runtime_sideload_fields = runtime_sideload_fields.reject { |f| f.is_a?(Hash) } edge_fields = edge_fields[:edge] if edge_fields end q << %| #{inc.to_s.camelize(:lower)} {| if !to_one q << %| edges {| edge_fields.each do |ef| q << %| #{ef.to_s.camelize(:lower)}| end q << %| node {| end sideload_fields = runtime_sideload_fields if sideload_fields.blank? sideload_fields = @resource.sideload(inc.to_sym).resource.attributes.select { |_, config| config[:readable] }.map(&:first) end sideload_fields = sideload_fields.map { |sf| sf.to_s.camelize(:lower) } sideload_fields |= [:__typename] sideload_fields.each do |name| q << %| #{indent}#{name}| end if to_one q << %| }| else q << %| } } }| end end end q << %| } }| if @with_pagination q << %| pageInfo { startCursor endCursor hasNextPage hasPreviousPage }| end if @params[:stats] q << %| stats {| @params[:stats].each_pair do |name, calculations| q << %| #{name.to_s.camelize(:lower)} {| Array(calculations).each do |calc| q << %| #{calc.to_s.camelize(:lower)}| end q << %| }| end q << %| }| end q << %| } } | q end |
#response ⇒ Object
97 98 99 |
# File 'lib/graphiti_gql/active_resource.rb', line 97 def response @response ||= run! end |
#run!(symbolize_keys: true) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/graphiti_gql/active_resource.rb', line 70 def run!(symbolize_keys: true) result = GraphitiGql.run(query, @params, @ctx) result = result.deep_symbolize_keys if symbolize_keys @response = result result end |
#stats ⇒ Object
121 122 123 |
# File 'lib/graphiti_gql/active_resource.rb', line 121 def stats underscore(data[data.keys.first][:stats]) end |