Class: ShopifyClient::BulkRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify-client/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, query) ⇒ Operation

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

Examples:

bulk_request.(client, <<~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:

  • client (Client)
  • query (String)

    the GraphQL query

Returns:



178
179
180
181
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
# File 'lib/shopify-client/bulk_request.rb', line 178

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

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

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

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

  Operation.new(client, id)
end