Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

bulkOperationRunMutation does not create all products

bulkOperationRunMutation does not create all products

haiderdev
Shopify Partner
6 0 0

I am using the Graphql api to do a "bulkOperationRunMutation". But for example, out of 100 products that are sent in the JSONL, some products are not created and it happens randomly. Sometimes, it creates 60 products, sometimes 70 and sometimes 80. It happens randomly for random users.

Replies 11 (11)

SomeUsernameHe
Shopify Partner
517 57 110

Have you checked your API response to see if there are any issues or errors with your request?

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
haiderdev
Shopify Partner
6 0 0

I receives this response:

"{"errors":false,"response":{"GuzzleHttp\\Psr7\\Response":[]},"status":200,"body":{"Gnikyt\\BasicShopifyAPI\\ResponseAccess":{"data":{"bulkOperationRunMutation":{"bulkOperation":{"id":"gid://shopify/BulkOperation/******************","url":null,"status":"CREATED"},"userErrors":[]}},"

and it clearly shows errors as false and status as CREATED

ShopifyDevSup
Shopify Staff
1453 238 522

Hi @haiderdev 👋

We recommend including `userErrors.message` in the underlying mutation. The resulting bulk operation url should include error messages for lines where products were not created successfully.


Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ShopifyDevSup
Shopify Staff
1453 238 522

To clarify, `userErrors.message` would be added to the underlying mutation: 

mutation {
 bulkOperationRunMutation(
   mutation: # add to this underlying mutation section as shown below
"mutation ($input: ProductInput!) { productCreate(input: $input) { ... } userErrors { message field } } }",
   stagedUploadPath: ...) {
   bulkOperation { ... }
   userErrors { # not this section 
     message
     field
   }
 }
}

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

SomeUsernameHe
Shopify Partner
517 57 110

Oh my, tis the official Shopify Dev Support 😮

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
haiderdev
Shopify Partner
6 0 0

This is the same thing that I'm doing.

SomeUsernameHe
Shopify Partner
517 57 110

Could you possibly share some of the code you are using to send this request? 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
haiderdev
Shopify Partner
6 0 0
$query = <<<QUERY
mutation {
bulkOperationRunMutation(
mutation: "mutation call(\$input: ProductInput!) { productCreate(input: \$input) { product {id title} userErrors { message field } } }",
stagedUploadPath: "$fileKey") {
bulkOperation {
id
url
status
}
userErrors {
message
field
}
}
}
QUERY;
$resp = $api->graph($query);
SomeUsernameHe
Shopify Partner
517 57 110

I mean it looks correct to me.

 

function createBulkOperation($shopName, $accessToken, $fileKey) {
    $url = "https://{$shopName}.myshopify.com/admin/api/2024-01/graphql.json";
    $headers = [
        "X-Shopify-Access-Token: {$accessToken}",
        'Content-Type: application/json'
    ];

    $query = <<<QUERY
mutation {
  bulkOperationRunMutation(
    mutation: "mutation call(\$input: ProductInput!) { productCreate(input: \$input) { product {id title} userErrors { message field } } }",
    stagedUploadPath: "$fileKey") {
    bulkOperation {
      id
      url
      status
    }
    userErrors {
      message
      field
    }
  }
}
QUERY;

    $payload = json_encode(['query' => $query]);
    $context = stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => $headers,
            'content' => $payload
        ]
    ]);

    $response = file_get_contents($url, false, $context);
    if ($response === FALSE) {
        // Handle error
        error_log("Error making API request");
        return null;
    }

    return json_decode($response, true);
}

// Usage
$shopName = 'your-shop-name';
$accessToken = 'your-access-token';
$fileKey = 'path_to_your_jsonl_file';
$response = createBulkOperation($shopName, $accessToken, $fileKey);

if ($response) {
    // Process the response
    print_r($response);
}

 


You can try something like this to see if you can get those errors to be thrown but without those errors I am unsure. I haven't worked with bulkOperationRunMutation much.

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
haiderdev
Shopify Partner
6 0 0

And I've conformed the JSONL file in staged upload, it contains all 100 products. and there are also no errors there.

SomeUsernameHe
Shopify Partner
517 57 110

Haha first rule of development: always assume every line of code has an error or will throw one... 😆 

 

I'm on lunch right now, when I get back I will take a look for you. 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee