Class: Ecoportal::API::GraphQL::Logic::Mutation

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

Defined Under Namespace

Classes: GenericInput, GenericPayload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Mutation.



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

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.



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

def base_path
  @base_path
end

#clientObject (readonly)

Returns the value of attribute client.



32
33
34
# File 'lib/ecoportal/api/graphql/logic/mutation.rb', line 32

def client
  @client
end

Class Method Details

.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



22
23
24
25
26
# File 'lib/ecoportal/api/graphql/logic/mutation.rb', line 22

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

Instance Method Details

#access_point(path = []) ⇒ Object



65
66
67
# File 'lib/ecoportal/api/graphql/logic/mutation.rb', line 65

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.



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

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(input:, path: self.path, &block) ⇒ Class

Query rely that manages the different blocks.

Returns:

  • (Class)

    an object of response_class with the results hanving from path.



51
52
53
54
55
56
57
58
59
# File 'lib/ecoportal/api/graphql/logic/mutation.rb', line 51

def query(input:, path: self.path, &block)
  request(*path) do
    client.query(input: as_input(input), &basic_block(&block))
  end
rescue Faraday::ParsingError => e
  puts "Internal Error with these input ('#{input.class}'):"
  pp input
  raise
end

#response_classObject



61
62
63
# File 'lib/ecoportal/api/graphql/logic/mutation.rb', line 61

def response_class
  payload_class
end