Class: Lucid::Shopify::BulkRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid/shopify/bulk_request.rb

Defined Under Namespace

Classes: Operation

Constant Summary collapse

OperationError =
Class.new(Error)
CanceledOperationError =
Class.new(OperationError)
ExpiredOperationError =
Class.new(OperationError)
FailedOperationError =
Class.new(OperationError)
ObsoleteOperationError =
Class.new(OperationError)
TimeoutError =
Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#call(client, credentials, query) ⇒ Operation

Create and start a new bulk operation via the GraphQL API. Any currently running bulk operations are cancelled.

Examples:

bulk_request.(client, credentials, <<~QUERY).() do |products|
  {
    products {
      edges {
        node {
          id
          handle
        }
      }
    }
  }
QUERY
  db.transaction do
    products.each do |product|
      db[:products].insert(
        id: product['id'],
        handle: product['handle'],
      )
    end
  end
end

Parameters:

Returns:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/lucid/shopify/bulk_request.rb', line 182

def call(client, credentials, query)
  Shopify.assert_api_version!('2019-10')

  op = client.post_graphql(credentials, <<~QUERY)['data']['currentBulkOperation']
    {
      currentBulkOperation {
        id
        status
        url
      }
    }
  QUERY

  case op&.fetch('status')
  when 'CANCELING'
    Operation.new(client, credentials, op['id']).poll_until(['CANCELED'])
  when 'CREATED', 'RUNNING'
    Operation.new(client, credentials, op['id']).cancel
  end

  id = client.post_graphql(credentials, <<~QUERY)['data']['bulkOperationRunQuery']['bulkOperation']['id']
    mutation {
      bulkOperationRunQuery(
        query: """
          #{query}
        """
      ) {
        bulkOperation {
          id
        }
        userErrors {
          field
          message
        }
      }
    }
  QUERY

  Operation.new(client, credentials, id)
end