Class: Lucid::Shopify::BulkRequest

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(http: Container[:http]) ⇒ BulkRequest

Returns a new instance of BulkRequest.

Parameters:

  • http (HTTP::Client) (defaults to: Container[:http])


17
18
19
# File 'lib/lucid/shopify/bulk_request.rb', line 17

def initialize(http: Container[:http])
  @http = http
end

Instance Method Details

#call(client, credentials, query) {|String| ... } ⇒ Object

Examples:

bulk_request.(client, credentials, <<~QUERY)
  {
    products {
      edges {
        node {
          id
          handle
          publishedAt
        }
      }
    }
  }
QUERY

Parameters:

Yields:

  • (String)

    each parsed line of JSONL (streamed to limit memory usage)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lucid/shopify/bulk_request.rb', line 41

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

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

  url = poll(client, credentials, id)

  # TODO: Verify signature?

  begin
    file = Tempfile.new(mode: 0600)
    body = @http.get(url).body
    until (chunk = body.readpartial).nil?
      file.write(chunk)
    end
    file.rewind
    file.each_line do |line|
      block.call(JSON.parse(line))
    end
  ensure
    file.close
    file.unlink
  end
end