|
| 1 | +# ShopifyAPI::GraphQL::Bulk |
| 2 | + |
| 3 | +Bulk import data with 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. Here we'll use 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) # ShopifyAPI::GraphQL::Bulk::Operation instance |
| 49 | +p operation.id |
| 50 | +p operation.completed_at |
| 51 | +p operation.url |
| 52 | +# etc... |
| 53 | + |
| 54 | +if operation.completed? |
| 55 | + operation.results[:data].each { } |
| 56 | + operation.results[:errors].each { } |
| 57 | + operation.results[:user_errors].each { } |
| 58 | +end |
| 59 | +``` |
| 60 | + |
| 61 | +`#create` also accepts a block: |
| 62 | + |
| 63 | +```rb |
| 64 | +id = bulk.create("mutationName") do |args| |
| 65 | + args << input1 |
| 66 | + args << input2 # etc... |
| 67 | +end |
| 68 | +``` |
| 69 | + |
| 70 | +Cancel a pending request: |
| 71 | + |
| 72 | +```rb |
| 73 | +operation = bulk.cancel(id) # returns ShopifyAPI::GraphQL::Bulk::Operation instance |
| 74 | +``` |
| 75 | + |
| 76 | +`ShopifyAPI::GraphQL::Bulk::Operation` corresponds to the GraphQL `BulkOperation` type. |
| 77 | +Hashes returned by this gem have `Symbol` keys that are snake_cased. |
| 78 | + |
| 79 | +## Development |
| 80 | + |
| 81 | +Tests use VCR. To re-record you need to define bulk operation IDs in `.env`. `cp .env.example .env` and update `.env` |
| 82 | + |
| 83 | +## See Also |
| 84 | + |
| 85 | +- [`ShopifyAPI::GraphQL::Request`](https://github.com/ScreenStaring/shopify_api-graphql-request/) - Simplify GraphQL handling. Comes with built-in retry, pagination, error handling, and more! |
| 86 | +- [`TinyGID`](https://github.com/sshaw/tiny_gid/) - Build Global ID (`gid://`) URI strings from scalar values |
| 87 | +- [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 |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +Made by [ScreenStaring](http://screenstaring.com) |
0 commit comments