Class: ShopifyApiBruv::Resources::Graphql::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/shopify_api_bruv/resources/graphql/resource.rb

Constant Summary collapse

QUERY =
nil
MAX_TRIES =
ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_MAX_TRIES', 3).to_i
SLEEP_TIMER =
ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_SLEEP_TIMER', 4).to_i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

call

Constructor Details

#initialize(config:, variables: nil) ⇒ Resource

Returns a new instance of Resource.



14
15
16
17
18
# File 'lib/shopify_api_bruv/resources/graphql/resource.rb', line 14

def initialize(config:, variables: nil)
  @client = Clients::Graphql::Client.new(config:)
  @variables = variables
  @query = self.class::QUERY unless self.class::QUERY.nil?
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/shopify_api_bruv/resources/graphql/resource.rb', line 7

def client
  @client
end

#queryObject

Returns the value of attribute query.



8
9
10
# File 'lib/shopify_api_bruv/resources/graphql/resource.rb', line 8

def query
  @query
end

#variablesObject

Returns the value of attribute variables.



8
9
10
# File 'lib/shopify_api_bruv/resources/graphql/resource.rb', line 8

def variables
  @variables
end

Instance Method Details

#call(tries: 0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shopify_api_bruv/resources/graphql/resource.rb', line 20

def call(tries: 0)
  if query.nil?
    raise Errors::ResourceError, "Please set attribute 'query' or define constant 'QUERY' in derived class"
  end

  response = client.request(query:, variables:)

  handle_response_errors(body: response.body, query:, variables:, tries:)

  ResourceResponse.new(body: response.body, query:)
end