shopify_api-graphql-bulk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.env.example +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +111 -0
- data/Rakefile +10 -0
- data/lib/shopify_api/graphql/bulk/cancel.rb +34 -0
- data/lib/shopify_api/graphql/bulk/create.rb +127 -0
- data/lib/shopify_api/graphql/bulk/operation/result.rb +48 -0
- data/lib/shopify_api/graphql/bulk/operation.rb +71 -0
- data/lib/shopify_api/graphql/bulk/result.rb +27 -0
- data/lib/shopify_api/graphql/bulk/version.rb +9 -0
- data/lib/shopify_api/graphql/bulk.rb +74 -0
- data/lib/tasks/bulk.rake +68 -0
- metadata +170 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 586c3d49043601a40e3ecdfa5810873d81b5655cfc2434742b2a568583b3f8e1
|
|
4
|
+
data.tar.gz: 262286a42dc2f5dc3ed773d184f0af946aea1717a7b8c01afc699ba9120a9145
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3081b0e7aecfbfe679f9b0d5289f5551b1aa43785569fd59a652a1f0237f9d72cbe2b6a3966cb6a515ea90bae99f0b585faafe33d1c98718a9a8ccbc0247b1e0
|
|
7
|
+
data.tar.gz: ae5e9ce73caa1aed82032ce85b360ddf051e0f3f88b4b179e55ee7b22441a2cdb40201e701ef09cff7c0ecaa7e601e4c96b75f5f7c3d8e0a8a7cd4a055dddb08
|
data/.env.example
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sshaw
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# ShopifyAPI::GraphQL::Bulk
|
|
2
|
+
|
|
3
|
+
Ruby Gem to Bulk import data using the [Shopify GraphQL Admin Bulk API](https://shopify.dev/docs/api/usage/bulk-operations/imports)
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
`Gemfile`:
|
|
8
|
+
|
|
9
|
+
```rb
|
|
10
|
+
gem "shopify_api-graphql-bulk"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or via the `gem` command:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
gem install "shopify_api-graphql-bulk"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```rb
|
|
22
|
+
# Mutation parameters. In this example we're using the productSet mutation.
|
|
23
|
+
params = [
|
|
24
|
+
{
|
|
25
|
+
:identifier => { :handle => "handle-1" },
|
|
26
|
+
:input => {
|
|
27
|
+
:title => "My New Title",
|
|
28
|
+
:handle => "handle-1",
|
|
29
|
+
# More params
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
:identifier => { :handle => "handle-2" },
|
|
34
|
+
:input => {
|
|
35
|
+
:title => "Another Title",
|
|
36
|
+
:handle => "handle-2",
|
|
37
|
+
# More params
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
# etc...
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
bulk = ShopifyAPI::GraphQL::Bulk.new(shop, token)
|
|
44
|
+
id = bulk.create("productSet", params)
|
|
45
|
+
|
|
46
|
+
# Wait a bit...
|
|
47
|
+
|
|
48
|
+
operation = bulk.result(id)
|
|
49
|
+
|
|
50
|
+
# returns a ShopifyAPI::GraphQL::Bulk::Operation instance
|
|
51
|
+
p operation.id
|
|
52
|
+
p operation.completed_at
|
|
53
|
+
p operation.url
|
|
54
|
+
# etc...
|
|
55
|
+
|
|
56
|
+
if operation.completed?
|
|
57
|
+
operation.results.each do |result|
|
|
58
|
+
# Each element is a Hash with the appropriate response, if any
|
|
59
|
+
result.data.each { }
|
|
60
|
+
result.errors.each { }
|
|
61
|
+
result.user_errors.each { }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`#create` also accepts a block:
|
|
67
|
+
|
|
68
|
+
```rb
|
|
69
|
+
id = bulk.create("productSet") do |args|
|
|
70
|
+
args << input1
|
|
71
|
+
args << input2 # etc...
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If you do not want the result to be fetched and parsed use `:parse_results => false`:
|
|
76
|
+
|
|
77
|
+
```rb
|
|
78
|
+
operation = bulk.result(id, :parse_results => false)
|
|
79
|
+
p operation.id
|
|
80
|
+
p operation.completed_at
|
|
81
|
+
p operation.result # now nil
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Cancel a pending request:
|
|
85
|
+
|
|
86
|
+
```rb
|
|
87
|
+
operation = bulk.cancel(id) # returns a ShopifyAPI::GraphQL::Bulk::Operation instance
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`ShopifyAPI::GraphQL::Bulk::Operation` corresponds to the GraphQL `BulkOperation` type.
|
|
91
|
+
Hashes returned by this gem have `Symbol` keys that are snake_cased.
|
|
92
|
+
|
|
93
|
+
## Development
|
|
94
|
+
|
|
95
|
+
Tests use VCR. To re-record you need to define bulk operation IDs in `.env`. `cp .env.example .env` and update `.env`
|
|
96
|
+
|
|
97
|
+
There are Rake tasks to help with bulk request generation.
|
|
98
|
+
|
|
99
|
+
## See Also
|
|
100
|
+
|
|
101
|
+
- [`ShopifyAPI::GraphQL::Request`](https://github.com/ScreenStaring/shopify_api-graphql-request/) - Simplify Shopify API GraphQL handling. Comes with built-in retry, pagination, error handling, and more!
|
|
102
|
+
- [`TinyGID`](https://github.com/sshaw/tiny_gid/) - Build Global ID (`gid://`) URI strings from scalar values
|
|
103
|
+
- [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
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
Made by [ScreenStaring](http://screenstaring.com)
|
data/Rakefile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ShopifyAPI
|
|
4
|
+
module GraphQL
|
|
5
|
+
module Bulk
|
|
6
|
+
class Cancel < ShopifyAPI::GraphQL::Request # :nodoc:
|
|
7
|
+
BULK_OPERATION_CANCEL = <<~GQL
|
|
8
|
+
#{BULK_OPERATION_FIELDS}
|
|
9
|
+
mutation bulkOperationCancel($id: ID!) {
|
|
10
|
+
bulkOperationCancel(id: $id) {
|
|
11
|
+
bulkOperation {
|
|
12
|
+
...BulkOperationFields
|
|
13
|
+
}
|
|
14
|
+
userErrors {
|
|
15
|
+
field
|
|
16
|
+
message
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
GQL
|
|
21
|
+
|
|
22
|
+
def execute(id, options = nil)
|
|
23
|
+
begin
|
|
24
|
+
data = super(BULK_OPERATION_CANCEL, :id => id).dig(:data, :bulk_operation_cancel, :bulk_operation)
|
|
25
|
+
rescue => e
|
|
26
|
+
raise Error, "cancel request failed: #{e}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
Operation.new(data, options)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "net/http/post/multipart"
|
|
6
|
+
require "tempfile"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
module ShopifyAPI
|
|
10
|
+
module GraphQL
|
|
11
|
+
module Bulk
|
|
12
|
+
class Create < ShopifyAPI::GraphQL::Request # :nodoc:
|
|
13
|
+
FILENAME = "bulk_import.jsonl"
|
|
14
|
+
|
|
15
|
+
STAGED_UPLOADS_CREATE = <<~GQL
|
|
16
|
+
mutation stagedUploadsCreate($input: [StagedUploadInput!]!) {
|
|
17
|
+
stagedUploadsCreate(input: $input) {
|
|
18
|
+
stagedTargets {
|
|
19
|
+
url
|
|
20
|
+
resourceUrl
|
|
21
|
+
parameters {
|
|
22
|
+
name
|
|
23
|
+
value
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
userErrors {
|
|
27
|
+
field
|
|
28
|
+
message
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
GQL
|
|
33
|
+
|
|
34
|
+
BULK_OPERATION_RUN_MUTATION = <<~GQL
|
|
35
|
+
#{BULK_OPERATION_FIELDS}
|
|
36
|
+
mutation bulkOperationRunMutation($mutation: String!, $stagedUploadPath: String!) {
|
|
37
|
+
bulkOperationRunMutation(mutation: $mutation, stagedUploadPath: $stagedUploadPath) {
|
|
38
|
+
bulkOperation {
|
|
39
|
+
...BulkOperationFields
|
|
40
|
+
}
|
|
41
|
+
userErrors {
|
|
42
|
+
field
|
|
43
|
+
message
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
GQL
|
|
48
|
+
|
|
49
|
+
def initialize(shop, token, options = nil)
|
|
50
|
+
super
|
|
51
|
+
|
|
52
|
+
@shop = shop
|
|
53
|
+
@token = token
|
|
54
|
+
@file = Tempfile.new([self.class.name, ".jsonl"])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def execute(mutation, data = nil)
|
|
58
|
+
@file.truncate(0)
|
|
59
|
+
@file.rewind
|
|
60
|
+
|
|
61
|
+
@mutation = mutation
|
|
62
|
+
|
|
63
|
+
Array(data).map { |d| self << d } if data
|
|
64
|
+
yield self if block_given?
|
|
65
|
+
|
|
66
|
+
@file.flush
|
|
67
|
+
raise ArgumentError, "no data proviced to upload" if @file.size == 0
|
|
68
|
+
|
|
69
|
+
input = [
|
|
70
|
+
:resource => "BULK_MUTATION_VARIABLES",
|
|
71
|
+
:filename => FILENAME,
|
|
72
|
+
:mime_type => "text/jsonl",
|
|
73
|
+
:http_method => "POST",
|
|
74
|
+
:file_size => @file.size.to_s
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
begin
|
|
78
|
+
target = super(STAGED_UPLOADS_CREATE, :input => input).dig(:data, :staged_uploads_create, :staged_targets, 0)
|
|
79
|
+
rescue => e
|
|
80
|
+
raise Error, "stage upload request failed: #{e.message}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
upload_jsonl(target)
|
|
84
|
+
upload_path = target[:parameters].find { |p| p[:name] == :key }
|
|
85
|
+
|
|
86
|
+
begin
|
|
87
|
+
data = super(BULK_OPERATION_RUN_MUTATION, :mutation => @mutation, :staged_upload_path => upload_path)
|
|
88
|
+
Operation.new(data.dig(:data, :bulk_operation_run_mutation, :bulk_operation))
|
|
89
|
+
rescue => e
|
|
90
|
+
raise Error, "bulk run mutation request failed: #{e.message}"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def <<(data)
|
|
95
|
+
@file.puts(JSON.generate(data))
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def stage_upload
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def upload_jsonl(target)
|
|
105
|
+
uri = URI(target[:url])
|
|
106
|
+
@file.rewind
|
|
107
|
+
io = UploadIO.new(@file, "text/jsonl", FILENAME)
|
|
108
|
+
|
|
109
|
+
params = {}
|
|
110
|
+
target[:parameters].each { |p| params[p[:name]] = p[:value] }
|
|
111
|
+
params["file"] = io
|
|
112
|
+
|
|
113
|
+
request = Net::HTTP::Post::Multipart.new(uri.path, params)
|
|
114
|
+
|
|
115
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
116
|
+
http.use_ssl = true
|
|
117
|
+
|
|
118
|
+
response = http.request(request)
|
|
119
|
+
|
|
120
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
121
|
+
raise Error, "file upload failed with status #{response.code}: #{response.body}"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ShopifyAPI
|
|
4
|
+
module GraphQL
|
|
5
|
+
module Bulk
|
|
6
|
+
class Operation
|
|
7
|
+
class Result
|
|
8
|
+
attr_reader :line, :data, :errors, :user_errors
|
|
9
|
+
|
|
10
|
+
def initialize(row)
|
|
11
|
+
@errors = []
|
|
12
|
+
@user_errors = []
|
|
13
|
+
|
|
14
|
+
@line = row["__lineNumber"]
|
|
15
|
+
|
|
16
|
+
if row.include?("errors")
|
|
17
|
+
@errors = row["errors"].map { |e| { :message => e["message"] } }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
if row["data"]
|
|
21
|
+
data = row["data"].values.first
|
|
22
|
+
if data
|
|
23
|
+
errors = data["userErrors"] || []
|
|
24
|
+
@user_errors = errors.map { |e| { :field => e["field"], :message => e["message"] } } if errors.any?
|
|
25
|
+
@data = snake_case_keys(data.reject { |k, _| k == "userErrors" })
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def snake_case_keys(object)
|
|
33
|
+
case object
|
|
34
|
+
when Hash
|
|
35
|
+
object.each_with_object({}) do |(key, value), result|
|
|
36
|
+
result[Strings::Case.snakecase(key).to_sym] = snake_case_keys(value)
|
|
37
|
+
end
|
|
38
|
+
when Array
|
|
39
|
+
object.map { |value| snake_case_keys(value) }
|
|
40
|
+
else
|
|
41
|
+
object
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
require_relative "operation/result"
|
|
6
|
+
|
|
7
|
+
module ShopifyAPI
|
|
8
|
+
module GraphQL
|
|
9
|
+
module Bulk
|
|
10
|
+
class Operation
|
|
11
|
+
STATUSES = %w[
|
|
12
|
+
CANCELED
|
|
13
|
+
CANCELING
|
|
14
|
+
COMPLETED
|
|
15
|
+
CREATED
|
|
16
|
+
EXPIRED
|
|
17
|
+
FAILED
|
|
18
|
+
RUNNING
|
|
19
|
+
].freeze
|
|
20
|
+
|
|
21
|
+
STATUSES.each do |status|
|
|
22
|
+
define_method("#{status.downcase}?") do
|
|
23
|
+
self.status == status
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
[
|
|
28
|
+
:id,
|
|
29
|
+
:status,
|
|
30
|
+
:error_code,
|
|
31
|
+
:object_count,
|
|
32
|
+
:partial_data_url,
|
|
33
|
+
:root_object_count,
|
|
34
|
+
:url
|
|
35
|
+
].each do |name|
|
|
36
|
+
define_method(name) { @data[name] }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
attr_reader :results, :created_at, :completed_at
|
|
40
|
+
|
|
41
|
+
def initialize(data, options = nil)
|
|
42
|
+
options ||= {}
|
|
43
|
+
|
|
44
|
+
url = data[:url] || data[:partial_data_url]
|
|
45
|
+
|
|
46
|
+
@data = data
|
|
47
|
+
@created_at = Time.parse(@data[:created_at])
|
|
48
|
+
@completed_at = Time.parse(@data[:completed_at]) if @data[:completed_at]
|
|
49
|
+
|
|
50
|
+
@results = parse_results(url) if url && options[:parse_results] != false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def parse_results(url)
|
|
56
|
+
response = Net::HTTP.get_response(URI(url))
|
|
57
|
+
raise Error, "failed to fetch bulk operation result: unsuccessful HTTP response #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
|
58
|
+
|
|
59
|
+
results = []
|
|
60
|
+
|
|
61
|
+
response.body.each_line do |line|
|
|
62
|
+
row = JSON.parse(line)
|
|
63
|
+
results << Operation::Result.new(row)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
results
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module ShopifyAPI
|
|
8
|
+
module GraphQL
|
|
9
|
+
module Bulk
|
|
10
|
+
class Result < ShopifyAPI::GraphQL::Request # :nodoc:
|
|
11
|
+
BULK_OPERATION_STATUS = <<~GQL
|
|
12
|
+
#{BULK_OPERATION_FIELDS}
|
|
13
|
+
query($id: ID!) {
|
|
14
|
+
bulkOperation(id: $id) {
|
|
15
|
+
...BulkOperationFields
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
GQL
|
|
19
|
+
|
|
20
|
+
def execute(id, options = nil)
|
|
21
|
+
data = super(BULK_OPERATION_STATUS, :id => id).dig(:data, :bulk_operation)
|
|
22
|
+
Operation.new(data, options)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shopify_api/graphql/request"
|
|
4
|
+
require "tiny_gid"
|
|
5
|
+
|
|
6
|
+
require_relative "bulk/version"
|
|
7
|
+
|
|
8
|
+
module ShopifyAPI
|
|
9
|
+
module GraphQL
|
|
10
|
+
module Bulk
|
|
11
|
+
Error = Class.new(Request::Error)
|
|
12
|
+
|
|
13
|
+
BULK_OPERATION_FIELDS = <<~GQL
|
|
14
|
+
fragment BulkOperationFields on BulkOperation {
|
|
15
|
+
id
|
|
16
|
+
status
|
|
17
|
+
errorCode
|
|
18
|
+
objectCount
|
|
19
|
+
partialDataUrl
|
|
20
|
+
rootObjectCount
|
|
21
|
+
url
|
|
22
|
+
createdAt
|
|
23
|
+
completedAt
|
|
24
|
+
}
|
|
25
|
+
GQL
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
def new(shop, token, options = nil)
|
|
29
|
+
Executor.new(shop, token, options)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Executor # :nodoc:
|
|
34
|
+
def initialize(shop, token, options)
|
|
35
|
+
raise ArgumentError, "shop required" if shop.to_s.strip.empty?
|
|
36
|
+
raise ArgumentError, "token required" if token.to_s.strip.empty?
|
|
37
|
+
|
|
38
|
+
@gid = TinyGID.new("shopify")
|
|
39
|
+
|
|
40
|
+
@create = Bulk::Create.new(shop, token, options)
|
|
41
|
+
@cancel = Bulk::Cancel.new(shop, token, options)
|
|
42
|
+
@result = Bulk::Result.new(shop, token, options)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def create(mutation, data = nil, &block)
|
|
46
|
+
@create.execute(mutation, data, &block)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def result(id, options = nil)
|
|
50
|
+
@result.execute(to_gid(id), options)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def cancel(id)
|
|
54
|
+
@cancel.execute(to_gid(id), options = nil)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def to_gid(id)
|
|
60
|
+
return id if id.to_s.start_with?("gid://")
|
|
61
|
+
|
|
62
|
+
@gid::BulkOperation(id)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private_constant :Executor
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
require_relative "bulk/create"
|
|
72
|
+
require_relative "bulk/result"
|
|
73
|
+
require_relative "bulk/cancel"
|
|
74
|
+
require_relative "bulk/operation"
|
data/lib/tasks/bulk.rake
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "shopify_api/graphql/bulk"
|
|
2
|
+
|
|
3
|
+
namespace :bulk do
|
|
4
|
+
desc "Submit a bulk task that can be used in test cases"
|
|
5
|
+
task :create do
|
|
6
|
+
shop = ENV.fetch("SHOPIFY_DOMAIN")
|
|
7
|
+
token = ENV.fetch("SHOPIFY_TOKEN")
|
|
8
|
+
|
|
9
|
+
mutation = <<~GQL
|
|
10
|
+
mutation productSet($input: ProductSetInput!, $identifier: ProductSetIdentifiers) {
|
|
11
|
+
productSet(input: $input, identifier: $identifier) {
|
|
12
|
+
product {
|
|
13
|
+
id
|
|
14
|
+
handle
|
|
15
|
+
tags
|
|
16
|
+
}
|
|
17
|
+
userErrors {
|
|
18
|
+
field
|
|
19
|
+
message
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
GQL
|
|
24
|
+
|
|
25
|
+
params = [
|
|
26
|
+
{
|
|
27
|
+
:identifier => { :handle => "handle-1" },
|
|
28
|
+
:input => {
|
|
29
|
+
:title => "My New Title",
|
|
30
|
+
:handle => "handle-1",
|
|
31
|
+
:productOptions => [
|
|
32
|
+
{ :name => "Title", :values => [{ :name => "Default Title" }] }
|
|
33
|
+
],
|
|
34
|
+
:variants => [
|
|
35
|
+
{
|
|
36
|
+
:sku => "SKU-001",
|
|
37
|
+
:optionValues => [
|
|
38
|
+
{ :optionName => "Title", :name => "Default Title" }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
:identifier => { :handle => "handle-2" },
|
|
46
|
+
:input => {
|
|
47
|
+
:title => "Another Title",
|
|
48
|
+
:handle => "handle-2",
|
|
49
|
+
:productOptions => [
|
|
50
|
+
{ :name => "Title", :values => [{ :name => "Default Title" }] }
|
|
51
|
+
],
|
|
52
|
+
:variants => [
|
|
53
|
+
{
|
|
54
|
+
:sku => "SKU-002",
|
|
55
|
+
:optionValues => [
|
|
56
|
+
{ :optionName => "Title", :name => "Default Title" }
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
bulk = ShopifyAPI::GraphQL::Bulk.new(shop, token, :version => ENV["SHOPIFY_API_VERSION"] || "2026-01")
|
|
65
|
+
puts "Making bulk request..."
|
|
66
|
+
puts bulk.create(mutation, params).id
|
|
67
|
+
end
|
|
68
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: shopify_api-graphql-bulk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Skye Shaw
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: multipart-post
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: shopify_api-graphql-request
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: strings-case
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: vcr
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: dotenv
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rake
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '13.0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '13.0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rspec
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '3.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '3.0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: webmock
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '3.0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '3.0'
|
|
125
|
+
description:
|
|
126
|
+
email:
|
|
127
|
+
- skye.shaw@gmail.com
|
|
128
|
+
executables: []
|
|
129
|
+
extensions: []
|
|
130
|
+
extra_rdoc_files: []
|
|
131
|
+
files:
|
|
132
|
+
- ".env.example"
|
|
133
|
+
- LICENSE.txt
|
|
134
|
+
- README.md
|
|
135
|
+
- Rakefile
|
|
136
|
+
- lib/shopify_api/graphql/bulk.rb
|
|
137
|
+
- lib/shopify_api/graphql/bulk/cancel.rb
|
|
138
|
+
- lib/shopify_api/graphql/bulk/create.rb
|
|
139
|
+
- lib/shopify_api/graphql/bulk/operation.rb
|
|
140
|
+
- lib/shopify_api/graphql/bulk/operation/result.rb
|
|
141
|
+
- lib/shopify_api/graphql/bulk/result.rb
|
|
142
|
+
- lib/shopify_api/graphql/bulk/version.rb
|
|
143
|
+
- lib/tasks/bulk.rake
|
|
144
|
+
homepage:
|
|
145
|
+
licenses:
|
|
146
|
+
- MIT
|
|
147
|
+
metadata:
|
|
148
|
+
bug_tracker_uri: https://github.com/ScreenStaring/shopify_api-graphql-bulk/issues
|
|
149
|
+
documentation_uri: https://rubydoc.info/gems/shopify_api-graphql-bulk
|
|
150
|
+
source_code_uri: https://github.com/ScreenStaring/shopify_api-graphql-bulk
|
|
151
|
+
post_install_message:
|
|
152
|
+
rdoc_options: []
|
|
153
|
+
require_paths:
|
|
154
|
+
- lib
|
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: 2.5.0
|
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
|
+
requirements:
|
|
162
|
+
- - ">="
|
|
163
|
+
- !ruby/object:Gem::Version
|
|
164
|
+
version: '0'
|
|
165
|
+
requirements: []
|
|
166
|
+
rubygems_version: 3.5.16
|
|
167
|
+
signing_key:
|
|
168
|
+
specification_version: 4
|
|
169
|
+
summary: Bulk import data using the Shopify GraphQL Admin Bulk API
|
|
170
|
+
test_files: []
|