Class: ActiveGraphQL::Fetcher

Inherits:
Support::Fancy show all
Defined in:
lib/activegraphql/fetcher.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Fetcher

Returns a new instance of Fetcher.



10
11
12
# File 'lib/activegraphql/fetcher.rb', line 10

def initialize(attrs)
  super(attrs)
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



6
7
8
# File 'lib/activegraphql/fetcher.rb', line 6

def action
  @action
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/activegraphql/fetcher.rb', line 6

def config
  @config
end

#klassObject

Returns the value of attribute klass.



6
7
8
# File 'lib/activegraphql/fetcher.rb', line 6

def klass
  @klass
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/activegraphql/fetcher.rb', line 6

def params
  @params
end

#queryObject

Returns the value of attribute query.



6
7
8
# File 'lib/activegraphql/fetcher.rb', line 6

def query
  @query
end

Instance Method Details

#default_retriable_optionsObject

Defaults are:

- { tries: 1 } if there's no retriable config.
- {} if the config is enabled but with no hash (it will use the defaults from Retriable)


55
56
57
58
# File 'lib/activegraphql/fetcher.rb', line 55

def default_retriable_options
  @default_retriable_options ||=
    config[:retriable].blank? ? { tries: 1 } : {}
end

#fetch(*graph) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/activegraphql/fetcher.rb', line 23

def fetch(*graph)
  response = query_get(*graph)

  return if response.blank?

  case response
  when Hash
    klass.new(response)
  when Array
    response.map { |h| klass.new(h) }
  else
    raise Error, "Unexpected response for query: #{response}"
  end
end

#in_locale(locale) ⇒ Object



18
19
20
21
# File 'lib/activegraphql/fetcher.rb', line 18

def in_locale(locale)
  query.locale = locale if locale.present?
  self
end

#query_get(*graph) ⇒ Object



38
39
40
# File 'lib/activegraphql/fetcher.rb', line 38

def query_get(*graph)
  Retriable.retriable(retriable_config) { query.get(*graph) }
end

#retriable_configObject



42
43
44
45
46
47
48
49
50
# File 'lib/activegraphql/fetcher.rb', line 42

def retriable_config
  # use defaults if retriable config is not a hash (ie. retriable: true | false)
  @retriable_config ||=
    if config[:retriable].is_a?(Hash)
      default_retriable_options.merge(config[:retriable])
    else
      default_retriable_options
    end
end