shopify_api 9.0.0 → 9.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +85 -80
- data/README.md +398 -214
- data/lib/active_resource/detailed_log_subscriber.rb +13 -3
- data/lib/shopify_api/message_enricher.rb +7 -1
- data/lib/shopify_api/version.rb +1 -1
- data/test/detailed_log_subscriber_test.rb +22 -0
- data/test/message_enricher_test.rb +13 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -5,134 +5,61 @@ Shopify API
|
|
5
5
|
[gem]: https://img.shields.io/gem/v/shopify_api.svg
|
6
6
|
[gem_url]: https://rubygems.org/gems/shopify_api
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
*
|
17
|
-
|
18
|
-
*
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
Note `extras` is still optional the other arguments are required.
|
55
|
-
|
56
|
-
```ruby
|
57
|
-
ShopifyAPI::Session.temp(domain, token, extras) do
|
58
|
-
...
|
59
|
-
end
|
60
|
-
```
|
61
|
-
is now
|
62
|
-
```ruby
|
63
|
-
ShopifyAPI::Session.temp(domain: domain, token: token, api_version: api_version) do
|
64
|
-
...
|
65
|
-
end
|
66
|
-
```
|
67
|
-
|
68
|
-
For example if you want to use the `2019-04` version you would create a session like this:
|
69
|
-
```ruby
|
70
|
-
session = ShopifyAPI::Session.new(domain: domain, token: token, api_version: '2019-04')
|
71
|
-
```
|
72
|
-
if you want to use the `unstable` version you would create a session like this:
|
73
|
-
```ruby
|
74
|
-
session = ShopifyAPI::Session.new(domain: domain, token: token, api_version: :unstable)
|
75
|
-
```
|
76
|
-
|
77
|
-
### Changes to how to define resources
|
78
|
-
|
79
|
-
If you have defined or customized Resources, classes that extend `ShopifyAPI::Base`:
|
80
|
-
The use of `self.prefix =` has been deprecated you should now use `self.resource =` and not include `/admin`.
|
81
|
-
For example if you specified a prefix like this before:
|
82
|
-
```ruby
|
83
|
-
class MyResource < ShopifyAPI::Base
|
84
|
-
self.prefix = '/admin/shop/'
|
85
|
-
end
|
86
|
-
```
|
87
|
-
You will update this to:
|
88
|
-
```ruby
|
89
|
-
class MyResource < ShopifyAPI::Base
|
90
|
-
self.resource_prefix = 'shop/'
|
91
|
-
end
|
92
|
-
```
|
93
|
-
|
94
|
-
### URL construction
|
95
|
-
|
96
|
-
If you have specifed any full paths for API calls in find
|
97
|
-
```ruby
|
98
|
-
def self.current(options={})
|
99
|
-
find(:one, options.merge(from: "/admin/shop.#{format.extension}"))
|
100
|
-
end
|
101
|
-
```
|
102
|
-
would be changed to
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
def self.current(options = {})
|
106
|
-
find(:one, options.merge(
|
107
|
-
from: api_version.construct_api_path("shop.#{format.extension}")
|
108
|
-
))
|
109
|
-
end
|
110
|
-
```
|
111
|
-
|
112
|
-
### URLs that have not changed
|
113
|
-
|
114
|
-
- OAuth URLs for `authorize`, getting the `access_token` from a code, `access_scopes`, and using a `refresh_token` have _not_ changed.
|
115
|
-
- get: `/admin/oauth/authorize`
|
116
|
-
- post: `/admin/oauth/access_token`
|
117
|
-
- get: `/admin/oauth/access_scopes`
|
118
|
-
- URLs for the merchant’s web admin have _not_ changed. For example: to send the merchant to the product page the url is still `/admin/product/<id>`
|
119
|
-
|
120
|
-
## Usage
|
121
|
-
|
122
|
-
### Requirements
|
123
|
-
|
124
|
-
All API usage happens through Shopify applications, created by either shop owners for their own shops, or by Shopify Partners for use by other shop owners:
|
125
|
-
|
126
|
-
* Shop owners can create applications for themselves through their own admin: https://docs.shopify.com/api/authentication/creating-a-private-app
|
8
|
+
The Shopify API gem allows Ruby developers to access the admin section of Shopify stores programmatically.
|
9
|
+
|
10
|
+
The best way to consume the Shopify API is through GraphQL, which enables high volume mutations, bulk operations, and access to all new features.
|
11
|
+
|
12
|
+
The REST API is implemented as JSON over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has a distinct URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
|
13
|
+
|
14
|
+
- [Shopify API](#shopify-api)
|
15
|
+
- [Usage](#usage)
|
16
|
+
* [Requirements](#requirements)
|
17
|
+
+ [Ruby version](#ruby-version)
|
18
|
+
* [Installation](#installation)
|
19
|
+
* [Getting Started](#getting-started)
|
20
|
+
+ [1) Create an app](#1-create-an-app)
|
21
|
+
+ [2A) Private Apps](#2a-private-apps)
|
22
|
+
+ [2B) Public and Custom Apps](#2b-public-and-custom-apps)
|
23
|
+
+ [3) Requesting access from a shop](#3-requesting-access-from-a-shop)
|
24
|
+
+ [4) Trading your `code` for an access token.](#4-trading-your--code--for-an-access-token)
|
25
|
+
+ [5) Activating the session](#5-activating-the-session)
|
26
|
+
+ [6A) Making requests to the GraphQL API](#6a-making-requests-to-the-graphql-api)
|
27
|
+
+ [6B) Making requests to the REST API](#6b-making-requests-to-the-rest-api)
|
28
|
+
* [Console](#console)
|
29
|
+
* [Thread safety](#thread-safety)
|
30
|
+
* [Bulk Operations](#bulk-operations)
|
31
|
+
+ [Example](#example)
|
32
|
+
- [1) Start the bulk operation](#1-start-the-bulk-operation)
|
33
|
+
- [2) Poll the status of the bulk operation](#2-poll-the-status-of-the-bulk-operation)
|
34
|
+
- [3) Retrieve your data](#3-retrieve-your-data)
|
35
|
+
* [Pagination](#pagination)
|
36
|
+
- [Breaking Change Notices](#breaking-change-notices)
|
37
|
+
* [Breaking change notice for version 8.0.0](#breaking-change-notice-for-version-800)
|
38
|
+
* [Breaking change notice for version 7.0.0](#breaking-change-notice-for-version-700)
|
39
|
+
+ [Changes to ShopifyAPI::Session](#changes-to-shopifyapi--session)
|
40
|
+
+ [Changes to how to define resources](#changes-to-how-to-define-resources)
|
41
|
+
+ [URL construction](#url-construction)
|
42
|
+
+ [URLs that have not changed](#urls-that-have-not-changed)
|
43
|
+
- [Using Development Version](#using-development-version)
|
44
|
+
- [Additional Resources](#additional-resources)
|
45
|
+
- [Copyright](#copyright)
|
46
|
+
|
47
|
+
# Usage
|
48
|
+
|
49
|
+
## Requirements
|
50
|
+
|
51
|
+
All API usage happens through Shopify applications, created by either shop owners for their shops, or by Shopify Partners for use by other shop owners:
|
52
|
+
|
53
|
+
* Shop owners can create applications for themselves through their admin: https://shopify.dev/tutorials/authenticate-a-private-app-with-shopify-admin#generate-private-app-credentials
|
127
54
|
* Shopify Partners create applications through their admin: http://app.shopify.com/services/partners
|
128
55
|
|
129
56
|
For more information and detailed documentation about the API visit https://developers.shopify.com/
|
130
57
|
|
131
|
-
|
58
|
+
### Ruby version
|
132
59
|
|
133
60
|
This gem requires Ruby 2.4 as of version 7.0.
|
134
61
|
|
135
|
-
|
62
|
+
## Installation
|
136
63
|
|
137
64
|
Add `shopify_api` to your `Gemfile`:
|
138
65
|
|
@@ -146,33 +73,63 @@ Or install via [gem](http://rubygems.org/)
|
|
146
73
|
gem install shopify_api
|
147
74
|
```
|
148
75
|
|
149
|
-
|
76
|
+
## Getting Started
|
77
|
+
|
78
|
+
ShopifyAPI sessions need to be configured with a fully authorized URL of a particular store before they can start making API calls. To obtain that URL, you can follow these steps:
|
79
|
+
|
80
|
+
### 1) Create an app
|
81
|
+
|
82
|
+
First, create a new application in either the partners admin or your store admin.
|
150
83
|
|
151
|
-
|
84
|
+
**Private apps** are used for merchant-owned scripts and apps that run silently in the background on a single shop. Private apps aren't able to render any content in the admin. Private apps are created through the store admin.
|
152
85
|
|
153
|
-
|
86
|
+
**Custom apps** are also used for a single shop, but they have access to [app extensions](https://shopify.dev/docs/app-extensions) that allow the app to render content in the admin and are managed and created through the partners dashboard.
|
154
87
|
|
155
|
-
|
88
|
+
**Public apps** can be installed on many stores, and can be added to the Shopify App Store to generate revenue for the developer.
|
156
89
|
|
157
|
-
|
90
|
+
For a private app, you'll need the API_KEY and the PASSWORD; otherwise, you'll need the API_KEY and SHARED_SECRET.
|
91
|
+
|
92
|
+
If you're not sure how to create a new application in the partner admin, visit the [tutorial in our documentation](https://shopify.dev/tutorials/authenticate-a-public-app-with-oauth#generate-credentials-from-your-partner-dashboard). For the instructions on generating a private app, visit the [tutorial on generating private credentials](https://shopify.dev/tutorials/authenticate-a-private-app-with-shopify-admin#generate-credentials-from-the-shopify-admin)
|
93
|
+
|
94
|
+
### 2A) Private Apps
|
95
|
+
|
96
|
+
For a private App you just need to set the base site url as follows:
|
158
97
|
|
159
98
|
```ruby
|
160
99
|
shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com"
|
161
100
|
ShopifyAPI::Base.site = shop_url
|
162
|
-
ShopifyAPI::Base.api_version = '<version_name>' # find the latest stable api_version [here](https://
|
101
|
+
ShopifyAPI::Base.api_version = '<version_name>' # find the latest stable api_version [here](https://shopify.dev/concepts/about-apis/versioning)
|
163
102
|
```
|
164
103
|
|
165
|
-
That's it
|
104
|
+
That's it; you're done! Next, skip to step 6 and start using the API!
|
166
105
|
|
167
|
-
|
106
|
+
### 2B) Public and Custom Apps
|
107
|
+
|
108
|
+
For public and custom apps, you will need to supply two parameters to the Session class before you instantiate it:
|
168
109
|
|
169
110
|
```ruby
|
170
111
|
ShopifyAPI::Session.setup(api_key: API_KEY, secret: SHARED_SECRET)
|
171
112
|
```
|
172
113
|
|
173
|
-
Shopify maintains [`omniauth-shopify-oauth2`](https://github.com/Shopify/omniauth-shopify-oauth2) which securely wraps the OAuth flow and interactions with Shopify
|
114
|
+
Shopify maintains [`omniauth-shopify-oauth2`](https://github.com/Shopify/omniauth-shopify-oauth2), which simplifies and securely wraps the OAuth flow and interactions with Shopify. Using this gem is the recommended way to use OAuth authentication in your application.
|
115
|
+
|
116
|
+
### 3) Requesting access from a shop
|
117
|
+
|
118
|
+
Public and Custom apps need an access token from each shop to access that shop's data. Getting an access token is a two-stage process. The first stage is to redirect the merchant to a **permission URL** to grant access to the app.
|
174
119
|
|
175
|
-
|
120
|
+
We've added the `create_permission_url` method to make this easier :
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
# We need to instantiate the session object before using it
|
124
|
+
shopify_session = ShopifyAPI::Session.new(domain: "SHOP_NAME.myshopify.com", api_version: api_version, token: nil)
|
125
|
+
|
126
|
+
# Then, create a permission URL with the session
|
127
|
+
permission_url = shopify_session.create_permission_url(scope, "https://my_redirect_uri.com", { state: "My Nonce" })
|
128
|
+
```
|
129
|
+
|
130
|
+
After creating the permission URL, the user should be directed to this URL to approve the app.
|
131
|
+
|
132
|
+
Under the hood, the the `create_permission_url` method is preparing the app to make the following request :
|
176
133
|
|
177
134
|
```
|
178
135
|
GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
|
@@ -181,60 +138,42 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
|
|
181
138
|
with the following parameters:
|
182
139
|
|
183
140
|
* ``client_id`` – Required – The API key for your app
|
184
|
-
* ``scope`` – Required – The list of required scopes (explained here: https://
|
185
|
-
* ``redirect_uri`` – Required – The URL where you want to redirect the users after they authorize the client. The complete URL specified here must be identical to one of the Application Redirect URLs set in the
|
186
|
-
* ``state`` – Optional – A randomly selected value provided by your application, which is unique for each authorization request. During the OAuth callback phase, your application must check that this value matches the one you provided during authorization. [This mechanism is
|
187
|
-
* ``grant_options[]`` - Optional - Set this parameter to `per-user` to receive an access token that respects the user's permission level when making API requests (called online access).
|
141
|
+
* ``scope`` – Required – The list of required scopes (explained here: https://shopify.dev/tutorials/authenticate-with-oauth#scopes)
|
142
|
+
* ``redirect_uri`` – Required – The URL where you want to redirect the users after they authorize the client. The complete URL specified here must be identical to one of the Application Redirect URLs set in the app's section of the Partners dashboard.
|
143
|
+
* ``state`` – Optional – A randomly selected value provided by your application, which is unique for each authorization request. During the OAuth callback phase, your application must check that this value matches the one you provided during authorization. [This mechanism is essential for the security of your application](https://tools.ietf.org/html/rfc6819#section-3.6).
|
144
|
+
* ``grant_options[]`` - Optional - Set this parameter to `per-user` to receive an access token that respects the user's permission level when making API requests (called online access). We strongly recommend using this parameter for embedded apps.
|
188
145
|
|
189
|
-
|
146
|
+
### 4) Trading your `code` for an access token.
|
190
147
|
|
191
|
-
|
192
|
-
shopify_session = ShopifyAPI::Session.new(domain: "SHOP_NAME.myshopify.com", api_version: api_version, token: nil)
|
193
|
-
```
|
148
|
+
Once authorized, the shop redirects the owner to the return URL of your application with a parameter named `code`. The value of this parameter is a temporary token that the app can exchange for a permanent access token.
|
194
149
|
|
195
|
-
|
150
|
+
Before you proceed, make sure your application performs the following security checks. If any of the checks fail, your application must reject the request with an error, and must not proceed further.
|
196
151
|
|
197
|
-
|
198
|
-
|
199
|
-
|
152
|
+
1) Ensure the provided ``state`` is the same one that your application provided to Shopify in the previous step.
|
153
|
+
2) Ensure the provided hmac is valid. The hmac is signed by Shopify, as explained below in the Verification section.
|
154
|
+
3) Ensure the provided hostname parameter is a valid hostname, ends with myshopify.com, and does not contain characters other than letters (a-z), numbers (0-9), dots, and hyphens.
|
200
155
|
|
201
|
-
|
156
|
+
If all security checks pass, the authorization code can be exchanged once for a permanent access token. There is a method to make the request and get the token for you. Pass all the params received from the previous call and the method will verify the params, extract the temp code and then request your token:
|
202
157
|
|
203
158
|
```ruby
|
204
|
-
|
159
|
+
token = shopify_session.request_token(params)
|
205
160
|
```
|
206
161
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
* Ensure the provided ``state`` is the same one that your application provided to Shopify during Step 3.
|
212
|
-
* Ensure the provided hmac is valid. The hmac is signed by Shopify as explained below, in the Verification section.
|
213
|
-
* Ensure the provided hostname parameter is a valid hostname, ends with myshopify.com, and does not contain characters other than letters (a-z), numbers (0-9), dots, and hyphens.
|
214
|
-
|
215
|
-
If all security checks pass, the authorization code can be exchanged once for a permanent access token. The exchange is made with a request to the shop.
|
162
|
+
This method will save the token to the session object and return it. All fields returned by Shopify, other than the access token itself, are stored in the session's `extra` attribute. For a list of all fields returned by Shopify, read [our OAuth documentation](https://shopify.dev/tutorials/authenticate-with-oauth#confirming-installation).
|
163
|
+
|
164
|
+
If you prefer to exchange the token manually, you can make a POST request to the shop with the following parameters :
|
216
165
|
|
217
166
|
```
|
218
167
|
POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token
|
219
168
|
```
|
220
169
|
|
221
|
-
with the following parameters:
|
222
|
-
|
223
170
|
* ``client_id`` – Required – The API key for your app
|
224
171
|
* ``client_secret`` – Required – The shared secret for your app
|
225
172
|
* ``code`` – Required – The token you received in step 3
|
226
173
|
|
227
|
-
|
228
|
-
|
229
|
-
There is a method to make the request and get the token for you. Pass
|
230
|
-
all the params received from the previous call and the method will verify
|
231
|
-
the params, extract the temp code and then request your token:
|
174
|
+
You'll get your permanent access token back in the response.
|
232
175
|
|
233
|
-
|
234
|
-
token = shopify_session.request_token(params)
|
235
|
-
```
|
236
|
-
|
237
|
-
This method will save the token to the session object and return it. All fields returned by Shopify, other than the access token itself, are stored in the session's `extra` attribute. For a list of all fields returned by Shopify, read [our OAuth documentation](https://help.shopify.com/api/guides/authentication/oauth#confirming-installation). If you requested an access token that is associated with a specific user, you can retreive information about this user from the `extra` hash:
|
176
|
+
If you requested an access token that is associated with a specific user, you can retrieve information about this user from the `extra` hash:
|
238
177
|
|
239
178
|
```ruby
|
240
179
|
# a list of all granted scopes
|
@@ -247,24 +186,58 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
|
|
247
186
|
expires_at = shopify_session.extra['expires_at']
|
248
187
|
```
|
249
188
|
|
250
|
-
For the security of your application, after retrieving an access token you must validate the following:
|
189
|
+
For the security of your application, after retrieving an access token, you must validate the following:
|
251
190
|
1) The list of scopes in `shopify_session.extra['scope']` is the same as you requested.
|
252
191
|
2) If you requested an online-mode access token, `shopify_session.extra['associated_user']` must be present.
|
253
|
-
Failing either of these tests means the end-user may have tampered with the
|
192
|
+
Failing either of these tests means the end-user may have tampered with the URL parameters during the OAuth authentication phase. You should avoid using this access token and revoke it immediately. If you use the [`omniauth-shopify-oauth2`](https://github.com/Shopify/omniauth-shopify-oauth2) gem, these checks are done automatically for you.
|
193
|
+
|
194
|
+
### 5) Activating the session
|
254
195
|
|
255
|
-
|
196
|
+
Once you have a token, simply pass in the `token` and `extra` hash (optional) when creating the session object:
|
256
197
|
|
257
198
|
```ruby
|
258
199
|
shopify_session = ShopifyAPI::Session.new(domain: "SHOP_NAME.myshopify.com", token: token, api_version: api_version, extra: extra)
|
259
200
|
```
|
260
201
|
|
261
|
-
|
202
|
+
The session must be activated before use:
|
262
203
|
|
263
204
|
```ruby
|
264
205
|
ShopifyAPI::Base.activate_session(shopify_session)
|
265
206
|
```
|
266
207
|
|
267
|
-
|
208
|
+
### 6A) Making requests to the GraphQL API
|
209
|
+
|
210
|
+
The GraphQL API is the recommended way to consume the Shopify API. It is more fully-featured than REST, more performant, and future-proof. Whenever possible, GraphQL should be used to consume the Shopify API.
|
211
|
+
|
212
|
+
###### Note: the GraphQL client has improved and changed in version 9.0. See the [client documentation](docs/graphql.md) for full usage details and a [migration guide](docs/graphql.md#migration-guide).
|
213
|
+
|
214
|
+
This library also supports Shopify's [GraphQL Admin API](https://shopify.dev/docs/admin-api/graphql/reference)
|
215
|
+
via integration with the [graphql-client](https://github.com/github/graphql-client) gem.
|
216
|
+
The authentication process (steps 1-5 under [Getting Started](#getting-started))
|
217
|
+
is identical. Once your session is activated, simply access the GraphQL client
|
218
|
+
and use `parse` and `query` as defined by
|
219
|
+
[graphql-client](https://github.com/github/graphql-client#defining-queries).
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
client = ShopifyAPI::GraphQL.client
|
223
|
+
|
224
|
+
SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
|
225
|
+
{
|
226
|
+
shop {
|
227
|
+
name
|
228
|
+
}
|
229
|
+
}
|
230
|
+
GRAPHQL
|
231
|
+
|
232
|
+
result = client.query(SHOP_NAME_QUERY)
|
233
|
+
result.data.shop.name
|
234
|
+
```
|
235
|
+
|
236
|
+
[GraphQL client documentation](docs/graphql.md)
|
237
|
+
|
238
|
+
### 6B) Making requests to the REST API
|
239
|
+
|
240
|
+
Responses to REST requests are returned as ActiveResource instances:
|
268
241
|
|
269
242
|
```ruby
|
270
243
|
shop = ShopifyAPI::Shop.current
|
@@ -292,7 +265,7 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
|
|
292
265
|
end
|
293
266
|
```
|
294
267
|
|
295
|
-
|
268
|
+
If you would like to run a small number of calls against a different API version you can use this block syntax:
|
296
269
|
|
297
270
|
```ruby
|
298
271
|
ShopifyAPI::Session.temp(domain: "SHOP_NAME.myshopify.com", token: token, api_version: '2019-04') do
|
@@ -306,13 +279,13 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
|
|
306
279
|
end
|
307
280
|
```
|
308
281
|
|
309
|
-
|
282
|
+
If you want to work with another shop, you'll first need to clear the session:
|
310
283
|
|
311
284
|
```ruby
|
312
285
|
ShopifyAPI::Base.clear_session
|
313
286
|
```
|
314
287
|
|
315
|
-
|
288
|
+
## Console
|
316
289
|
|
317
290
|
This package also supports the ``shopify-api`` executable to make it easy to open up an interactive console to use the API with a shop.
|
318
291
|
|
@@ -322,9 +295,9 @@ This package also supports the ``shopify-api`` executable to make it easy to ope
|
|
322
295
|
gem install shopify_api_console
|
323
296
|
```
|
324
297
|
|
325
|
-
2. Obtain a private API key and password to use with your shop (step
|
298
|
+
2. Obtain a private API key and password to use with your shop (step 2A in "Getting Started")
|
326
299
|
|
327
|
-
3. Use the ``shopify-api`` script to save the credentials for the shop to quickly
|
300
|
+
3. Use the ``shopify-api`` script to save the credentials for the shop to quickly login.
|
328
301
|
|
329
302
|
```bash
|
330
303
|
shopify-api add yourshopname
|
@@ -344,60 +317,148 @@ gem install shopify_api_console
|
|
344
317
|
shopify-api help
|
345
318
|
```
|
346
319
|
|
347
|
-
##
|
320
|
+
## Thread safety
|
348
321
|
|
349
|
-
|
350
|
-
for full usage details and a [migration guide](docs/graphql.md#migration-guide).
|
322
|
+
ActiveResource is threadsafe as of version 4.1 (which works with Rails 4.x and above).
|
351
323
|
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
324
|
+
If you were previously using Shopify's [activeresource fork](https://github.com/shopify/activeresource), then you should remove it and use ActiveResource 4.1.
|
325
|
+
|
326
|
+
## Bulk Operations
|
327
|
+
|
328
|
+
With the GraphQL Admin API, you can use bulk operations to asynchronously fetch data in bulk. The API is designed to reduce complexity and improve performance when dealing with large volumes of data.
|
329
|
+
|
330
|
+
Instead of manually paginating results and managing a client-side throttle, you can instead run a bulk query operation. Shopify’s infrastructure does the hard work of executing your query, and then provides you with a URL where you can download all of the data.
|
331
|
+
|
332
|
+
Apps are limited to running a single bulk operation at a time per shop. When the operation is complete, the results are delivered in the form of a JSONL file that Shopify makes available at a URL.
|
333
|
+
|
334
|
+
### Example
|
335
|
+
|
336
|
+
The following mutation queries the products connection and returns each product's ID and title.
|
337
|
+
|
338
|
+
#### 1) Start the bulk operation
|
358
339
|
|
359
340
|
```ruby
|
360
341
|
client = ShopifyAPI::GraphQL.client
|
361
342
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
343
|
+
PRODUCTS_BULK_QUERY = client.parse <<-'GRAPHQL'
|
344
|
+
mutation {
|
345
|
+
bulkOperationRunQuery(
|
346
|
+
query: """
|
347
|
+
{
|
348
|
+
products {
|
349
|
+
edges {
|
350
|
+
node {
|
351
|
+
id
|
352
|
+
title
|
353
|
+
}
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
"""
|
358
|
+
) {
|
359
|
+
bulkOperation {
|
360
|
+
id
|
361
|
+
status
|
362
|
+
}
|
363
|
+
userErrors {
|
364
|
+
field
|
365
|
+
message
|
366
|
+
}
|
367
|
+
}
|
366
368
|
}
|
367
|
-
}
|
368
369
|
GRAPHQL
|
369
370
|
|
370
|
-
result = client.query(
|
371
|
-
result.data.shop.name
|
371
|
+
result = client.query(PRODUCTS_BULK_QUERY)
|
372
372
|
```
|
373
|
+
#### Step 2) Poll the status of the bulk operation
|
373
374
|
|
374
|
-
|
375
|
+
While the operation is running, you need to poll to see its progress using the `currentBulkOperation` field. The `objectCount` field increments to indicate the operation's progress, and the `status` field returns whether the operation is completed.
|
375
376
|
|
376
|
-
|
377
|
+
```ruby
|
378
|
+
BULK_POLL_QUERY = client.parse <<-'GRAPHQL'
|
379
|
+
query {
|
380
|
+
currentBulkOperation {
|
381
|
+
id
|
382
|
+
status
|
383
|
+
errorCode
|
384
|
+
createdAt
|
385
|
+
completedAt
|
386
|
+
objectCount
|
387
|
+
fileSize
|
388
|
+
url
|
389
|
+
partialDataUrl
|
390
|
+
}
|
391
|
+
}
|
392
|
+
GRAPHQL
|
377
393
|
|
378
|
-
|
394
|
+
result = client.query(BULK_POLL_QUERY)
|
395
|
+
```
|
379
396
|
|
380
|
-
|
397
|
+
The JSON response of a completed query will look like this :
|
398
|
+
|
399
|
+
```json
|
400
|
+
{
|
401
|
+
"data": {
|
402
|
+
"currentBulkOperation": {
|
403
|
+
"id": "gid:\/\/shopify\/BulkOperation\/720918",
|
404
|
+
"status": "COMPLETED",
|
405
|
+
"errorCode": null,
|
406
|
+
"createdAt": "2019-08-29T17:16:35Z",
|
407
|
+
"completedAt": "2019-08-29T17:23:25Z",
|
408
|
+
"objectCount": "57",
|
409
|
+
"fileSize": "358",
|
410
|
+
"url": "https:\/\/storage.googleapis.com\/shopify\/dyfkl3g72empyyoenvmtidlm9o4g?<params>",
|
411
|
+
"partialDataUrl": null
|
412
|
+
}
|
413
|
+
},
|
414
|
+
...
|
415
|
+
}
|
416
|
+
```
|
381
417
|
|
382
|
-
|
418
|
+
#### Step 3) Retrieve your data
|
419
|
+
|
420
|
+
Since bulk operations are specifically designed to fetch large datasets, we’ve chosen the [JSON Lines](http://jsonlines.org/) (JSONL) format for the response data so that clients have more flexibility in how they consume the data. JSONL is similar to JSON, but each line is a valid JSON object. The file can be parsed one line at a time by using file streaming functionality to avoid issues with memory consumption.
|
383
421
|
|
384
|
-
|
422
|
+
A JSONL output file is available for download at the URL specified in the `url` field when the operation completes.
|
423
|
+
|
424
|
+
Each line in the file is a node object returned in a connection. If a node has a nested connection, then each child node is extracted into a new object on the next line. Below is an example of a JSONL file.
|
425
|
+
|
426
|
+
```json
|
427
|
+
{"id":"gid://shopify/Product/1921569226808"}
|
428
|
+
{"id":"gid://shopify/ProductVariant/19435458986040","title":"70","__parentId":"gid://shopify/Product/1921569226808"}
|
429
|
+
{"id":"gid://shopify/Product/1921569259576"}
|
430
|
+
{"id":"gid://shopify/ProductVariant/19435459018808","title":"34","__parentId":"gid://shopify/Product/1921569259576"}
|
431
|
+
{"id":"gid://shopify/Product/1921569292344"}
|
432
|
+
{"id":"gid://shopify/ProductVariant/19435459051576","title":"Default Title","__parentId":"gid://shopify/Product/1921569292344"}
|
433
|
+
{"id":"gid://shopify/Product/1921569325112"}
|
434
|
+
{"id":"gid://shopify/ProductVariant/19435459084344","title":"36","__parentId":"gid://shopify/Product/1921569325112"}
|
435
|
+
{"id":"gid://shopify/Product/1921569357880"}
|
436
|
+
{"id":"gid://shopify/ProductVariant/19435459117112","title":"47","__parentId":"gid://shopify/Product/1921569357880"}
|
437
|
+
{"id":"gid://shopify/ProductVariant/19435458986123","title":"52","__parentId":"gid://shopify/Product/1921569226808"}
|
438
|
+
```
|
439
|
+
|
440
|
+
Here's a simple example in Ruby to demonstrate the proper way of loading and parsing a JSONL file:
|
385
441
|
|
386
|
-
Page based pagination
|
387
442
|
```ruby
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
443
|
+
# Efficient: reads the file a single line at a time
|
444
|
+
File.open(file) do |f|
|
445
|
+
f.each do |line|
|
446
|
+
JSON.parse(line)
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
# Inefficient: reads the entire file into memory
|
451
|
+
jsonl = File.read(file)
|
452
|
+
|
453
|
+
jsonl.each_line do |line|
|
454
|
+
JSON.parse(line)
|
395
455
|
end
|
396
456
|
```
|
397
457
|
|
398
|
-
|
458
|
+
## Pagination
|
459
|
+
|
460
|
+
Shopify uses [Relative cursor-based pagination](https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api) to provide more than a single page of results.
|
399
461
|
|
400
|
-
[Relative cursor based pagination](https://help.shopify.com/en/api/guides/paginated-rest-results)
|
401
462
|
```ruby
|
402
463
|
products = ShopifyAPI::Product.find(:all, params: { limit: 50 })
|
403
464
|
process_products(products)
|
@@ -407,7 +468,7 @@ while products.next_page?
|
|
407
468
|
end
|
408
469
|
```
|
409
470
|
|
410
|
-
If you want cursor
|
471
|
+
If you want cursor-based pagination to work across page loads, or wish to distribute workload across multiple background jobs, you can use #next_page_info or #previous_page_info methods that return strings:
|
411
472
|
|
412
473
|
```
|
413
474
|
first_batch_products = ShopifyAPI::Product.find(:all, params: { limit: 50 })
|
@@ -417,7 +478,129 @@ If you want cursor based pagination to work across page loads, or want to distri
|
|
417
478
|
|
418
479
|
Relative cursor pagination is currently available for all endpoints using the `2019-10` and later API versions.
|
419
480
|
|
420
|
-
|
481
|
+
Apps using older versions of the API may have used page-based pagination (deprecated starting in 2019-10) :
|
482
|
+
|
483
|
+
```ruby
|
484
|
+
page = 1
|
485
|
+
products = ShopifyAPI::Product.find(:all, params: { limit: 50, page: page })
|
486
|
+
process_products(products)
|
487
|
+
while(products.count == 50)
|
488
|
+
page += 1
|
489
|
+
products = ShopifyAPI::Product.find(:all, params: { limit: 50, page: page })
|
490
|
+
process_products(products)
|
491
|
+
end
|
492
|
+
```
|
493
|
+
|
494
|
+
# Breaking Change Notices
|
495
|
+
|
496
|
+
## Breaking change notice for version 8.0.0
|
497
|
+
|
498
|
+
Version 7.0.0 introduced ApiVersion, and known versions were hardcoded into the gem. Manually defining API versions is no longer required for versions not listed in the gem. Version 8.0.0 removes the following:
|
499
|
+
* `ShopifyAPI::ApiVersion::Unstable`
|
500
|
+
* `ShopifyAPI::ApiVersion::Release`
|
501
|
+
* `ShopifyAPI::ApiVersion.define_version`
|
502
|
+
|
503
|
+
The following methods on `ApiVersion` have been deprecated:
|
504
|
+
- `.coerce_to_version` deprecated. use `.find_version`
|
505
|
+
- `.define_known_versions` deprecated. Use `.fetch_known_versions`
|
506
|
+
- `.clear_defined_versions` deprecated. Use. `.clear_known_versions`
|
507
|
+
- `.latest_stable_version` deprecated. Use `ShopifyAPI::Meta.admin_versions.find(&:latest_supported)` (this fetches info from Shopify servers. No authentication required.)
|
508
|
+
- `#name` deprecated. Use `#handle`
|
509
|
+
- `#stable?` deprecated. Use `#supported?`
|
510
|
+
|
511
|
+
Version 8.0.0 introduces a _version lookup mode_. By default, `ShopifyAPI::ApiVersion.version_lookup_mode` is `:define_on_unknown`. When setting the api_version on `Session` or `Base`, the `api_version` attribute takes a version handle (i.e. `'2019-07'` or `:unstable`) and sets an instance of `ShopifyAPI::ApiVersion` matching the handle. When the version_lookup_mode is set to `:define_on_unknown`, any handle will naïvely create a new `ApiVersion` if the version is not in the known versions returned by `ShopifyAPI::ApiVersion.versions`.
|
512
|
+
|
513
|
+
To ensure you're setting only known and active versions, call :
|
514
|
+
|
515
|
+
```ruby
|
516
|
+
ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
|
517
|
+
ShopifyAPI::ApiVersion.fetch_known_versions
|
518
|
+
```
|
519
|
+
|
520
|
+
Known and active versions are fetched from https://app.shopify.com/services/apis.json and cached. Trying to use a version outside this cached set will raise an error. To switch back to naïve lookup and create a version if one is not found, call `ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown`.
|
521
|
+
|
522
|
+
|
523
|
+
## Breaking change notice for version 7.0.0
|
524
|
+
|
525
|
+
### Changes to ShopifyAPI::Session
|
526
|
+
When creating sessions, `api_version`is now required and uses keyword arguments.
|
527
|
+
|
528
|
+
To upgrade your use of ShopifyAPI you will need to make the following changes.
|
529
|
+
|
530
|
+
```ruby
|
531
|
+
ShopifyAPI::Session.new(domain, token, extras)
|
532
|
+
```
|
533
|
+
is now
|
534
|
+
```ruby
|
535
|
+
ShopifyAPI::Session.new(domain: domain, token: token, api_version: api_version, extras: extras)
|
536
|
+
```
|
537
|
+
Note `extras` is still optional. The other arguments are required.
|
538
|
+
|
539
|
+
```ruby
|
540
|
+
ShopifyAPI::Session.temp(domain, token, extras) do
|
541
|
+
...
|
542
|
+
end
|
543
|
+
```
|
544
|
+
is now
|
545
|
+
```ruby
|
546
|
+
ShopifyAPI::Session.temp(domain: domain, token: token, api_version: api_version) do
|
547
|
+
...
|
548
|
+
end
|
549
|
+
```
|
550
|
+
|
551
|
+
For example, if you want to use the `2019-04` version, you will create a session like this:
|
552
|
+
```ruby
|
553
|
+
session = ShopifyAPI::Session.new(domain: domain, token: token, api_version: '2019-04')
|
554
|
+
```
|
555
|
+
if you want to use the `unstable` version, you will create a session like this:
|
556
|
+
```ruby
|
557
|
+
session = ShopifyAPI::Session.new(domain: domain, token: token, api_version: :unstable)
|
558
|
+
```
|
559
|
+
|
560
|
+
### Changes to how to define resources
|
561
|
+
|
562
|
+
If you have defined or customized Resources, classes that extend `ShopifyAPI::Base`:
|
563
|
+
The use of `self.prefix =` has been deprecated; you should now use `self.resource =` and not include `/admin`.
|
564
|
+
For example, if you specified a prefix like this before:
|
565
|
+
```ruby
|
566
|
+
class MyResource < ShopifyAPI::Base
|
567
|
+
self.prefix = '/admin/shop/'
|
568
|
+
end
|
569
|
+
```
|
570
|
+
You will update this to:
|
571
|
+
```ruby
|
572
|
+
class MyResource < ShopifyAPI::Base
|
573
|
+
self.resource_prefix = 'shop/'
|
574
|
+
end
|
575
|
+
```
|
576
|
+
|
577
|
+
### URL construction
|
578
|
+
|
579
|
+
If you have specified any full paths for API calls in find
|
580
|
+
```ruby
|
581
|
+
def self.current(options={})
|
582
|
+
find(:one, options.merge(from: "/admin/shop.#{format.extension}"))
|
583
|
+
end
|
584
|
+
```
|
585
|
+
would be changed to
|
586
|
+
|
587
|
+
```ruby
|
588
|
+
def self.current(options = {})
|
589
|
+
find(:one, options.merge(
|
590
|
+
from: api_version.construct_api_path("shop.#{format.extension}")
|
591
|
+
))
|
592
|
+
end
|
593
|
+
```
|
594
|
+
|
595
|
+
### URLs that have not changed
|
596
|
+
|
597
|
+
- OAuth URLs for `authorize`, getting the `access_token` from a code, `access_scopes`, and using a `refresh_token` have _not_ changed.
|
598
|
+
- get: `/admin/oauth/authorize`
|
599
|
+
- post: `/admin/oauth/access_token`
|
600
|
+
- get: `/admin/oauth/access_scopes`
|
601
|
+
- URLs for the merchant’s web admin have _not_ changed. For example: to send the merchant to the product page the url is still `/admin/product/<id>`
|
602
|
+
|
603
|
+
# Using Development Version
|
421
604
|
|
422
605
|
Download the source code and run:
|
423
606
|
|
@@ -437,11 +620,12 @@ or you can even use our automated rake task for docker:
|
|
437
620
|
bundle exec rake docker
|
438
621
|
```
|
439
622
|
|
440
|
-
|
623
|
+
# Additional Resources
|
441
624
|
|
442
|
-
* [API Reference](https://
|
443
|
-
* [
|
625
|
+
* [GraphQL API Reference](https://shopify.dev/docs/admin-api/graphql/reference)
|
626
|
+
* [REST API Reference](https://shopify.dev/docs/admin-api/rest/reference)
|
627
|
+
* [Ask questions on the forums](https://community.shopify.com/c/Shopify-Community/ct-p/en?profile.language=en)
|
444
628
|
|
445
|
-
|
629
|
+
# Copyright
|
446
630
|
|
447
631
|
Copyright (c) 2014 "Shopify Inc.". See LICENSE for details.
|