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: GraphQLError, HTTPError
Constant Summary collapse
- Error =
Class.new(StandardError)
- ConnectionError =
Class.new(Error)
- RateLimitError =
Class.new(GraphQLError)
- USER_AGENT =
"ShopifyAPI::GraphQL::Tiny v#{VERSION} (Ruby v#{RUBY_VERSION})"
- SHOPIFY_DOMAIN =
".myshopify.com"
- ACCESS_TOKEN_HEADER =
"X-Shopify-Access-Token"
- QUERY_COST_HEADER =
"X-GraphQL-Cost-Include-Fields"
- DEFAULT_HEADERS =
{ "Content-Type" => "application/json" }.freeze
- DEFAULT_RETRY_OPTIONS =
Retry rules to be used for all instances if no rules are specified via the
:retry
option when creating an instance { ConnectionError => { :wait => 3, :tries => 20 }, GraphQLError => { :wait => 3, :tries => 20 }, HTTPError => { :wait => 3, :tries => 20 } }
- 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.1.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.
-
#paginate(*options) ⇒ Object
Create a pager to execute a paginated query:.
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)
-
Hash
can 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|IO)
-
Output the HTTP request/response to
STDERR
or to its value if it’s anIO
. Defaults tofalse
.
Errors
ArgumentError if no shop
or token
is provided.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/shopify_api/graphql/tiny.rb', line 77 def initialize(shop, token, = nil) raise ArgumentError, "shop required" unless shop raise ArgumentError, "token required" unless token @domain = shopify_domain(shop) @options = || {} @headers = DEFAULT_HEADERS.dup @headers[ACCESS_TOKEN_HEADER] = token @headers[QUERY_COST_HEADER] = "true" if retry? @endpoint = URI(sprintf(ENDPOINT, @domain, !@options[: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
ArgumentError, ConnectionError, HTTPError, RateLimitError, GraphQLError
-
An ShopifyAPI::GraphQL::Tiny::HTTPError is raised of the response does not have 200 status code
-
A ShopifyAPI::GraphQL::Tiny::RateLimitError is raised if rate-limited and retries are disabled or if still rate-limited after the configured number of retry attempts
-
A ShopifyAPI::GraphQL::Tiny::GraphQLError is raised if the response contains an
errors
property that is not a rate-limit error
Returns
- Hash
-
The GraphQL response. Unmodified.
113 114 115 116 117 118 |
# File 'lib/shopify_api/graphql/tiny.rb', line 113 def execute(q, variables = nil) raise ArgumentError, "query required" if q.nil? || q.to_s.strip.empty? config = retry? ? @options[:retry] || DEFAULT_RETRY_OPTIONS : {} ShopifyAPIRetry::GraphQL.retry(config) { post(q, variables) } end |
#paginate(*options) ⇒ Object
Create a pager to execute a paginated query:
pager = gql.paginate # This is the same as gql.paginate(:after)
pager.execute(query, :id => id) do |page|
page.dig("data", "product", "title")
end
The block is called for each page.
Using pagination requires you to include the PageInfo object in your queries and wrap them in a function that accepts a page/cursor argument. See the README for more information.
Arguments
- direction (Symbol)
-
The direction to paginate, either
:after
or:before
. Optional, defaults to:after:
- options (Hash)
-
Pagination options. Optional.
Options
- :after (Array|Proc)
-
The location of PageInfo block.
An
Array
will be passed directly toHash#dig
. ATypeError
resulting from the#dig
call will be raised as anArgumentError
.The
"data"
and"pageInfo"
keys are automatically added if not provided.A
Proc
must accept the GraphQL responseHash
as its argument and must return thepageInfo
block to use for pagination. - :before (Array|Proc)
-
See the
:after
option - :variable (String)
-
Name of the GraphQL variable to use as the “page” argument. Defaults to
"before"
or"after"
, depending on the pagination direction.
Errors
ArgumentError
162 163 164 |
# File 'lib/shopify_api/graphql/tiny.rb', line 162 def paginate(*) Pager.new(self, ) end |