Class: ShopifyAPI::GraphQL::Request
- Inherits:
-
Object
- Object
- ShopifyAPI::GraphQL::Request
- Defined in:
- lib/shopify_api/graphql/request.rb,
lib/shopify_api/graphql/request/version.rb
Overview
Small class to simplify the writing and handling of GraphQL queries and mutations for the Shopify Admin API. Comes with built-in retry, pagination, error handling, and more!
Defined Under Namespace
Classes: NotFoundError, UserError
Constant Summary collapse
- Error =
Class.new(StandardError)
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#execute(query, variables = nil, options = nil) ⇒ Object
Executes a query or mutation.
-
#initialize(shop, token, options = nil) ⇒ Request
constructor
Create a new GraphQL client to connect to
shop. -
#paginate(query, variables = nil, options = nil) ⇒ Object
Executes a query using pagination.
Constructor Details
#initialize(shop, token, options = nil) ⇒ Request
Create a new GraphQL client to connect to shop
Arguments
- shop (String)
-
Shopify domain to make requests against
- token (String)
-
Shopify API token
- options (Hash)
-
Client options. Optional.
Options
- :raise_if_not_found (Boolean)
-
If
trueraise a NotFoundError if the requested record is not found. Defaults totrue. - :raise_if_user_errors (Boolean)
-
If
trueraise a UserError if the mutation resulted in user errors. Defaults totrue. - :snake_case (Boolean)
-
Convert response
Hashkeys tosnake_casesymbols. Defaults totrue.
Additional options: those accepted by ShopifyAPI::GraphQL::Tiny
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/shopify_api/graphql/request.rb', line 66 def initialize(shop, token, = nil) = ( || {}).dup [:snake_case, :raise_if_not_found, :raise_if_user_errors].each do |name| [name] = true unless .include?(name) end @gql = ShopifyAPI::GraphQL::Tiny.new(shop, token, ) @gid = TinyGID.new("shopify") end |
Instance Method Details
#execute(query, variables = nil, options = nil) ⇒ Object
Executes a query or mutation
Arguments
- query (String)
-
Query or mutation to execute
- token (String)
-
Optional variables accepted by
query - options (Hash)
-
Optional
Options
These override the instance's defaults for a single query or mutation.
- :raise_if_not_found (Boolean)
- :raise_if_user_errors (Boolean)
- :snake_case (Boolean)
-
Returns
The GraphQL response
HashErrors
-
ArgumentError
-
ShopifyAPI::GraphQL::Request::UserError - the a mutation contains user errors
-
ShopifyAPI::GraphQL::Request::NotFoundError - if the query cannot be find the given object
-
ShopifyAPI::GraphQL::Tiny::ConnectionError
-
ShopifyAPI::GraphQL::Tiny::HTTPError
-
ShopifyAPI::GraphQL::Tiny::RateLimitError - if the retry attempts have been exceeded
-
ShopifyAPI::GraphQL::Tiny::GraphQLError
-
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/shopify_api/graphql/request.rb', line 110 def execute(query, variables = nil, = nil) = .merge( || {}) variables = camelize_keys(variables) if [:snake_case] data = gql.execute(query, variables) raise_if_not_found(data, variables) if [:raise_if_not_found] raise_if_user_errors(data) if [:raise_if_user_errors] data = snake_case_keys(data) if [:snake_case] data end |
#paginate(query, variables = nil, options = nil) ⇒ Object
Executes a query using pagination.
Using pagination requires you to include the PageInfo in your query.
Arguments
Same as #execute but also accepts a block that will be called with each page. If a block not given returns an instance of Enumerator::Lazy that will fetch the next page on each iteration
Errors
See #execute
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/shopify_api/graphql/request.rb', line 142 def paginate(query, variables = nil, = nil) = .merge( || {}) variables = camelize_keys(variables) if [:snake_case] pager = gql.paginate # execute() returns a lazy enumerator so we're not loading everything now. pages = pager.execute(query, variables).map do |page| raise_if_not_found(page, variables) if [:raise_if_not_found] raise_if_user_errors(page) if [:raise_if_user_errors] page = snake_case_keys(page) if [:snake_case] page end return pages unless block_given? pages.each { |page| yield page } nil end |