|
1 | 1 | # ShopifyAPI::GraphQL::Tiny |
2 | 2 |
|
3 | | -Lightweight, no-nonsense, Shopify GraphQL Admin API client with built-in pagination and retry. |
| 3 | +Lightweight, no-nonsense, Shopify GraphQL Admin API client with built-in pagination and retry |
4 | 4 |
|
5 | 5 | [](https://github.com/ScreenStaring/shopify_api-graphql-tiny/actions) |
6 | 6 |
|
|
74 | 74 | p result.dig("data", "customerUpdate", "userErrors") |
75 | 75 | ``` |
76 | 76 |
|
| 77 | +### Automatically Retrying Failed Requests |
| 78 | + |
| 79 | +There are 2 types of retries: 1) request is rate-limited by Shopify 2) request fails due to an exception or non-200 HTTP response. |
| 80 | + |
| 81 | +When a request is rate-limited by Shopify retry occurs according to [Shopify's `throttleStatus`](https://shopify.dev/docs/api/admin-graphql/unstable#rate-limits) |
| 82 | + |
| 83 | +When a request fails due to an exception or non-200 HTTP status a retry will be attempted after an exponential backoff waiting period. |
| 84 | +This is controlled by `ShopifyAPI::GraphQL::Tiny::DEFAULT_BACKOFF_OPTIONS`. It contains: |
| 85 | + |
| 86 | +* `:base_delay` - `0.5` |
| 87 | +* `:jitter` - `true` |
| 88 | +* `:max_attempts` - `10` |
| 89 | +* `:max_delay` - `60` |
| 90 | +* `:multiplier` - `2.0` |
| 91 | + |
| 92 | +`:max_attempts` dictates how many retry attempts will be made **for all** types of retries. |
| 93 | + |
| 94 | +These can be overridden globally (by assigning to the constant) or per instance: |
| 95 | + |
| 96 | +```rb |
| 97 | +gql = ShopifyAPI::GraphQL::Tiny.new(shop, token, :max_attempts => 20, :max_delay => 90) |
| 98 | +``` |
| 99 | + |
| 100 | +`ShopifyAPI::GraphQL::Tiny::DEFAULT_RETRY_ERRORS` determines what is retried. It contains and HTTP statuses codes, Shopify GraphQL errors codes, and exceptions. |
| 101 | +By default it contains: |
| 102 | + |
| 103 | +* `"5XX"` - Any HTTP 5XX status |
| 104 | +* `"INTERNAL_SERVER_ERROR"` - Shopify GraphQL error code |
| 105 | +* `"TIMEOUT"` - Shopify GraphQL error code |
| 106 | +* `EOFError` |
| 107 | +* `Errno::ECONNABORTED` |
| 108 | +* `Errno::ECONNREFUSED` |
| 109 | +* `Errno::ECONNRESET` |
| 110 | +* `Errno::EHOSTUNREACH` |
| 111 | +* `Errno::EINVAL` |
| 112 | +* `Errno::ENETUNREACH` |
| 113 | +* `Errno::ENOPROTOOPT` |
| 114 | +* `Errno::ENOTSOCK` |
| 115 | +* `Errno::EPIPE` |
| 116 | +* `Errno::ETIMEDOUT` |
| 117 | +* `Net::HTTPBadResponse` |
| 118 | +* `Net::HTTPHeaderSyntaxError` |
| 119 | +* `Net::ProtocolError` |
| 120 | +* `Net::ReadTimeout` |
| 121 | +* `OpenSSL::SSL::SSLError` |
| 122 | +* `SocketError` |
| 123 | +* `Timeout::Error` |
| 124 | + |
| 125 | +These can be overridden globally (by assigning to the constant) or per instance: |
| 126 | + |
| 127 | +```rb |
| 128 | +# Only retry on 2 errors |
| 129 | +gql = ShopifyAPI::GraphQL::Tiny.new(shop, token, :retry => [SystemCallError, "500"]) |
| 130 | +``` |
| 131 | + |
| 132 | +#### Disabling Automatic Retry |
| 133 | + |
| 134 | +To disable retries set the `:retry` option to `false`: |
| 135 | + |
| 136 | +```rb |
| 137 | +gql = ShopifyAPI::GraphQL::Tiny.new(shop, token, :retry => false) |
| 138 | +``` |
| 139 | + |
77 | 140 | ### Pagination |
78 | 141 |
|
79 | 142 | In addition to built-in request retry `ShopifyAPI::GraphQL::Tiny` also builds in support for pagination. |
@@ -196,19 +259,21 @@ pager.execute(query) { |page| } |
196 | 259 |
|
197 | 260 | The `"data"` and `"pageInfo"` keys are automatically added if not provided. |
198 | 261 |
|
199 | | -### Automatically Retrying Failed Requests |
200 | | - |
201 | | -See [the docs](https://rubydoc.info/gems/shopify_api-graphql-tiny) for more information. |
202 | | - |
203 | 262 | ## Testing |
204 | 263 |
|
205 | 264 | `cp env.template .env` and fill-in `.env` with the missing values. This requires a Shopify store. |
206 | 265 |
|
| 266 | +To elicit a request that will be rate-limited by Shopify run following Rake task: |
| 267 | + |
| 268 | +```sh |
| 269 | +bundle exec rake rate_limit SHOPIFY_DOMAIN=your-domain SHOPIFY_TOKEN=your-token |
| 270 | +``` |
| 271 | + |
207 | 272 | ## See Also |
208 | 273 |
|
209 | 274 | - [Shopify Dev Tools](https://github.com/ScreenStaring/shopify-dev-tools) - Command-line program to assist with the development and/or maintenance of Shopify apps and stores |
210 | | -- [Shopify ID Export](https://github.com/ScreenStaring/shopify_id_export/) Dump Shopify product and variant IDs —along with other identifiers— to a CSV or JSON file |
211 | | -- [ShopifyAPIRetry](https://github.com/ScreenStaring/shopify_api_retry) - Retry a ShopifyAPI request if rate-limited or other errors occur (REST and GraphQL APIs) |
| 275 | +- [Shopify ID Export](https://github.com/ScreenStaring/shopify_id_export/) - Dump Shopify product and variant IDs —along with other identifiers— to a CSV or JSON file |
| 276 | +- [`TinyGID`](https://github.com/sshaw/tiny_gid/) - Build Global ID (gid://) URI strings from scalar values |
212 | 277 |
|
213 | 278 | ## License |
214 | 279 |
|
|
0 commit comments