Class: ShopifyAPI::GraphQL::Tiny
- Inherits:
-
Object
- Object
- ShopifyAPI::GraphQL::Tiny
- Defined in:
- lib/shopify_api/graphql/tiny.rb,
lib/shopify_api/graphql/tiny/version.rb
Overview
Client to make Shopify GraphQL Admin API requests with built-in retries.
Defined Under Namespace
Classes: HTTPError, RateLimitError
Constant Summary collapse
- Error =
Class.new(StandardError)
- ConnectionError =
Class.new(Error)
- GraphQLError =
Class.new(Error)
- SHOPIFY_DOMAIN =
".myshopify.com"- ACCESS_TOKEN_HEADER =
"X-Shopify-Access-Token"- QUERY_COST_HEADER =
"X-GraphQL-Cost-Include-Fields"- DEFAULT_RETRY_OPTIONS =
{ ConnectionError => { :wait => 3, :tries => 20 }, HTTPError => { :wait => 3, :tries => 20 } }
- DEFAULT_HEADERS =
{ "Content-Type" => "application/json" }.freeze
- ENDPOINT =
Note that we omit the “/” after API for the case where there’s no version.
"https://%s/admin/api%s/graphql.json"- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#execute(q, variables = nil) ⇒ Object
Execute a GraphQL query or mutation.
-
#initialize(shop, token, options = nil) ⇒ Tiny
constructor
Create a new GraphQL client.
Constructor Details
#initialize(shop, token, options = nil) ⇒ Tiny
Create a new GraphQL client.
Arguments
- shop (String)
-
Shopify domain to make requests against
- token (String)
-
Shopify Admin API GraphQL token
- options (Hash)
-
Client options. Optional.
Options
- :retry (Boolean|Hash)
-
Hashcan be retry config options. For the format see ShopifyAPIRetry. Defaults totrue - :version (String)
-
Shopify API version to use. Defaults to the latest version.
- :debug (Boolean)
-
Output the HTTP request/response to
STDERR. Defaults tofalse.
Errors
ArgumentError if no shop or token is provided.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/shopify_api/graphql/tiny.rb', line 65 def initialize(shop, token, = nil) raise ArgumentError, "shop required" unless shop raise ArgumentError, "token required" unless token @domain = shopify_domain(shop) = || {} @headers = DEFAULT_HEADERS.dup @headers[ACCESS_TOKEN_HEADER] = token @headers[QUERY_COST_HEADER] = "true" if retry? @endpoint = URI(sprintf(ENDPOINT, @domain, ![:version].to_s.strip.empty? ? "/#{@options[:version]}" : "")) end |
Instance Method Details
#execute(q, variables = nil) ⇒ Object
Execute a GraphQL query or mutation.
Arguments
- q (String)
-
Query or mutation to execute
- variables (Hash)
-
Optional. Variable to pass to the query or mutation given by
q
Errors
ConnectionError, HTTPError, RateLimitError, GraphQLError
-
An HTTPError is raised of the response does not have 200 status code
-
A RateLimitError is raised if rate-limited and retries are disabled or if still rate-limited after the configured number of retry attempts
-
A GraphQLError is raised if the response contains an
errorsproperty that is not a rate-limit error
Returns
- Hash
-
The GraphQL response. Unmodified.
99 100 101 102 |
# File 'lib/shopify_api/graphql/tiny.rb', line 99 def execute(q, variables = nil) config = retry? ? [:retry] || DEFAULT_RETRY_OPTIONS : {} ShopifyAPIRetry::GraphQL.retry(config) { post(q, variables) } end |