Class: Ecoportal::API::GraphQL::Logic::BaseQuery

Inherits:
Object
  • Object
show all
Includes:
Common::Concerns::Benchmarkable, Common::GraphQL::ClassHelpers
Defined in:
lib/ecoportal/api/graphql/logic/base_query.rb

Direct Known Subclasses

Mutation, Query

Constant Summary collapse

DETAIL_ERRORS =
[
  Faraday::ParsingError,
  Graphlient::Errors::FaradayServerError,
  Graphlient::Errors::GraphQLError,
  Graphlient::Errors::ClientError
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path: nil, base_path: self.class.base_path) ⇒ BaseQuery

Returns a new instance of BaseQuery.



69
70
71
72
73
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 69

def initialize(client, path: nil, base_path: self.class.base_path)
  @path      = path
  @base_path = base_path
  @client    = client
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



67
68
69
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 67

def base_path
  @base_path
end

#clientObject (readonly)

Returns the value of attribute client.



67
68
69
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 67

def client
  @client
end

Class Method Details

.accepted_params(*keys, default: :unused) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 22

def accepted_params(*keys, default: :unused)
  @accepted_params ||= []
  return @accepted_params if keys.empty?

  keys.map(&:to_sym).uniq.each do |key|
    @accepted_params   |= [key]
    param_defaults[key] = default unless default == :unused
  end

  @accepted_params.push(*keys).tap(&:uniq!)
  @accepted_params
end

.base_path(path = :unused) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 54

def base_path(path = :unused)
  return @base_path if path == :unused

  path ||= []
  path   = path.to_s.split('.') if path.is_a?(String)
  path   = path.map(&:to_s).compact

  @base_path = path
end

.clear_accepted_paramsObject



35
36
37
38
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 35

def clear_accepted_params
  @param_defaults  = {}
  @accepted_params = []
end

.field_name(str = nil) ⇒ Object

Note:

it is meant for reusability of queries from different end-points

Used to obtain the full path in the GraphQL query by using base_path



46
47
48
49
50
51
52
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 46

def field_name(str = nil)
  return @field_name unless str

  @field_name = nil
  @field_name = str.to_s if str
  @field_name
end

.param_defaultsObject



18
19
20
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 18

def param_defaults
  @param_defaults ||= {}
end

.slice_params(kargs) ⇒ Object



40
41
42
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 40

def slice_params(kargs)
  kargs.slice(*accepted_params)
end

Instance Method Details

#access_point(path = []) ⇒ Object



103
104
105
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 103

def access_point(path = [])
  path.last
end

#path(field_name = self.class.field_name) ⇒ Object

Resolves the path by using path or base_path + class.field_name.



76
77
78
79
80
81
82
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 76

def path(field_name = self.class.field_name)
  result   = @path
  result ||= default_path if respond_to?(:default_path, true)
  result ||= (base_path + [field_name]) if base_path && field_name
  result ||= [field_name]
  result
end

#query(path: self.path, raw_response: {}, **kargs, &block) ⇒ Class

Query rely that manages the different blocks.

Returns:

  • (Class)

    an object of response_class with the results hanging from path.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 86

def query(path: self.path, raw_response: {}, **kargs, &block)
  print = !is_a?(Mutation)

  benchmarking("#{self.class}##{__method__}", print: print) do
    graphql_query(
      path:         path,
      raw_response: raw_response,
      **kargs,
      &basic_block(&block)
    )
  end
end

#response_classObject



99
100
101
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 99

def response_class
  raise "You should override this method in #{self.class}"
end