lib/ecoportal/api/graphql/logic/base_query.rb in ecoportal-api-graphql-0.3.13 vs lib/ecoportal/api/graphql/logic/base_query.rb in ecoportal-api-graphql-0.3.14

- old
+ new

@@ -17,11 +17,11 @@ keys.map(&:to_sym).uniq.each do |key| @accepted_params |= [key] param_defaults[key] = default unless default == :unused end - @accepted_params.push(*keys).tap {|ks| ks.uniq!} + @accepted_params.push(*keys).tap(&:uniq!) @accepted_params end def clear_accepted_params @param_defaults = {} @@ -39,10 +39,12 @@ @field_name = nil @field_name = str.to_s if str end end + include Ecoportal::API::Common::Concerns::Benchmarkable + attr_reader :client attr_reader :base_path def initialize(client, path: nil, base_path: []) @path = path @@ -51,19 +53,21 @@ end # Resolves the `path` by using `path` or `base_path` + `class.field_name`. def path(field_name = self.class.field_name) result = @path - result ||= default_path if self.respond_to?(:default_path, true) + result ||= default_path if respond_to?(:default_path, true) result ||= (base_path + [field_name]) if base_path && field_name result end # Query rely that manages the different blocks. # @return [Class] an object of `response_class` with the results hanging from `path`. def query(path: self.path, **kargs, &block) - graphql_query(path: path, **kargs, &basic_block(&block)) + benchmarking("#{self.class}##{__method__}", print: true) do + graphql_query(path: path, **kargs, &basic_block(&block)) + end end def response_class raise "You should override this method in #{self.class}" end @@ -82,11 +86,11 @@ default_params = self.class.param_defaults.dup query_params = self.class.slice_params(default_params.merge(kargs)) request(*path) do raw_response[:data] = client.query(query_params, &block) end - rescue Faraday::ParsingError, Graphlient::Errors::GraphQLError => e + rescue Faraday::ParsingError, Graphlient::Errors::GraphQLError => _err puts "Internal Error with these params:" pp kargs pp block raise end @@ -95,10 +99,13 @@ response = yield wrap_response(response, path) end def wrap_response(response, path = []) - raise "Complete failure on request. Path: #{path}\n#{response.to_h.pretty_inspect}" unless res = response.to_h.dig(*path.dup.unshift("data")) + unless (res = response.to_h.dig(*path.dup.unshift("data"))) + msg = "Complete failure on request. Path: #{path}\n#{response.to_h.pretty_inspect}" + raise msg + end data = Ecoportal::API::Common::GraphQL::HashHelpers.deep_dup(res) response_class.new(data) end end end