|
| 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 | +## Usage |
| 6 | + |
| 7 | +```rb |
| 8 | +# Mutation parameters. Here we'll use the productSet mutation. |
| 9 | +params = [ |
| 10 | + { |
| 11 | + :identifier => { :handle => "handle-1" }, |
| 12 | + :input => { |
| 13 | + :title => "My New Title", |
| 14 | + :handle => "handle-1", |
| 15 | + # More params |
| 16 | + } |
| 17 | + }, |
| 18 | + { |
| 19 | + :identifier => { :handle => "handle-2" }, |
| 20 | + :input => { |
| 21 | + :title => "Another Title", |
| 22 | + :handle => "handle-2", |
| 23 | + # More params |
| 24 | + } |
| 25 | + }, |
| 26 | + # etc... |
| 27 | +] |
| 28 | + |
| 29 | +bulk = ShopifyAPI::GraphQL::Bulk.new(shop, token) |
| 30 | +id = bulk.create("productSet", params) |
| 31 | + |
| 32 | +# Wait a bit... |
| 33 | + |
| 34 | +operation = bulk.result(id) # ShopifyAPI::GraphQL::Bulk::Operation instance |
| 35 | +p operation.id |
| 36 | +p operation.completed_at |
| 37 | +p operation.url |
| 38 | +# etc... |
| 39 | + |
| 40 | +if operation.completed? |
| 41 | + operation.results[:data].each { } |
| 42 | + operation.results[:errors].each { } |
| 43 | + operation.results[:user_errors].each { } |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +`#create` also accepts a block: |
| 48 | + |
| 49 | +```rb |
| 50 | +id = bulk.create("mutationName") do |args| |
| 51 | + args << input1 |
| 52 | + args << input2 # etc... |
| 53 | +end |
| 54 | +``` |
| 55 | + |
| 56 | +Cancel a pending request: |
| 57 | + |
| 58 | +```rb |
| 59 | +operation = bulk.cancel(id) # returns ShopifyAPI::GraphQL::Bulk::Operation instance |
| 60 | +``` |
| 61 | + |
| 62 | +`ShopifyAPI::GraphQL::Bulk::Operation` corresponds to the GraphQL `BulkOperation` type. |
| 63 | +Hashes returned by this gem have `Symbol` keys that are snake_cased. |
| 64 | + |
| 65 | +## Development |
| 66 | + |
| 67 | +Tests use VCR. To re-record you need to define bulk operation IDs in `.env`. `cp .env.example .env` and update `.env` |
| 68 | + |
| 69 | +## See Also |
| 70 | + |
| 71 | +- [`ShopifyAPI::GraphQL::Request`](https://github.com/ScreenStaring/shopify_api-graphql-request/) - Simplify GraphQL handling. Comes with built-in retry, pagination, error handling, and more! |
| 72 | +- [`TinyGID`](https://github.com/sshaw/tiny_gid/) - Build Global ID (`gid://`) URI strings from scalar values |
| 73 | +- [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 |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +Made by [ScreenStaring](http://screenstaring.com) |
0 commit comments