Class: Ecoportal::API::GraphQL::Base::Mutation

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

Direct Known Subclasses

Mutation::CreateContractorEntity

Defined Under Namespace

Classes: 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.



28
29
30
31
32
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 28

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.



26
27
28
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 26

def base_path
  @base_path
end

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 25

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



17
18
19
20
21
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 17

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



58
59
60
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 58

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.



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

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.



44
45
46
47
48
49
50
51
52
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 44

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



54
55
56
# File 'lib/ecoportal/api/graphql/base/mutation.rb', line 54

def response_class
  payload_class
end