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

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

Direct Known Subclasses

Mutation, Query

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path: nil, base_path: []) ⇒ BaseQuery

Returns a new instance of BaseQuery.



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

def initialize(client, path: nil, base_path: [])
  @path      = path
  @base_path = base_path
  @client    = client
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#clientObject (readonly)

Returns the value of attribute client.



33
34
35
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 33

def client
  @client
end

Class Method Details

.accepted_params(*keys) ⇒ Object



10
11
12
13
14
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 10

def accepted_params(*keys)
  @accepted_params ||= []
  return @accepted_params if keys.empty?
  @accepted_params.push(*keys).tap {|ks| ks.uniq!}
end

.clear_accepted_paramsObject



16
17
18
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 16

def clear_accepted_params
  @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



26
27
28
29
30
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 26

def field_name(str = nil)
  return @field_name unless str
  @field_name = nil
  @field_name = str.to_s if str
end

.slice_params(kargs) ⇒ Object



20
21
22
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 20

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

Instance Method Details

#access_point(path = []) ⇒ Object



60
61
62
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 60

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.



43
44
45
46
47
48
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 43

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

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

Query rely that manages the different blocks.

Returns:

  • (Class)

    an object of response_class with the results hanging from path.



52
53
54
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 52

def query(path: self.path, **kargs, &block)
  graphql_query(path: path, **kargs, &basic_block(&block))
end

#response_classObject



56
57
58
# File 'lib/ecoportal/api/graphql/logic/base_query.rb', line 56

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