Can bulkOperationRunQuery operation be used for creating multiple products in one request?

GeorgianaCG
Visitor
2 0 0

Hello, 

I came across this documentation (https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api#bulk-query-workflow) , because some of my operations with GraphQL exceed the 1000 cost per request.

Is it possible to use "bulkOperationRunQuery" for putting together a mutation to create multiple products at once and not exceeding the max cost?

This is how the current mutation looks like:

 

mutation ($media: [CreateMediaInput!], $input1: ProductInput!, $input2: ProductInput!) {
  Product1: productCreate(input: $input1, media: $media) {
    product {
      id
      handle
      status
      metafields(first: 1) {
        edges {
          node {
            key
            value
          }
        }
      }
      variants(first: 12) {
        edges {
          node {
            sku
            id
            selectedOptions {
              name
              value
            }
            displayName
            inventoryItem {
              id
            }
          }
        }
      }
      mediaCount
    }
    userErrors {
      field
      message
    }
  }
  Product2: productCreate(input: $input2, media: $media) {
    product {
      id
      createdAt
      metafields(first: 1) {
        edges {
          node {
            key
            value
          }
        }
      }
      variants(first: 12) {
        edges {
          node {
            sku
            selectedOptions {
              name
              value
            }
            id
            inventoryItem {
              id
            }
          }
        }
      }
      mediaCount
    }
    userErrors {
      field
      message
    }
  }
}

 

Any info would be appreciated. Thanks!

Replies 2 (2)

oscprofessional
Shopify Partner
15905 2378 3087

Hello @GeorgianaCG 

Yes, it is possible to use the bulkOperationRunQuery mutation to create multiple products at once and avoid exceeding the maximum cost limit.

To achieve this, you can modify your mutation to include multiple product creations within a single bulkOperationRunQuery mutation. Instead of making individual productCreate mutations for each product, you can combine them into a single mutation.

Here's an example of how you can structure your mutation to create multiple products using bulkOperationRunQuery:

 

mutation ($media: [CreateMediaInput!], $input1: ProductInput!, $input2: ProductInput!) {
  bulkOperationRunQuery(
    query: """
      mutation ($media: [CreateMediaInput!], $input1: ProductInput!, $input2: ProductInput!) {
        Product1: productCreate(input: $input1, media: $media) {
          product {
            id
            handle
            status
            metafields(first: 1) {
              edges {
                node {
                  key
                  value
                }
              }
            }
            variants(first: 12) {
              edges {
                node {
                  sku
                  id
                  selectedOptions {
                    name
                    value
                  }
                  displayName
                  inventoryItem {
                    id
                  }
                }
              }
            }
            mediaCount
          }
          userErrors {
            field
            message
          }
        }
        Product2: productCreate(input: $input2, media: $media) {
          product {
            id
            createdAt
            metafields(first: 1) {
              edges {
                node {
                  key
                  value
                }
              }
            }
            variants(first: 12) {
              edges {
                node {
                  sku
                  selectedOptions {
                    name
                    value
                  }
                  id
                  inventoryItem {
                    id
                  }
                }
              }
            }
            mediaCount
          }
          userErrors {
            field
            message
          }
        }
      }
    """
    variables: { media: $media, input1: $input1, input2: $input2 }
  ) {
    bulkOperation {
      id
      status
      errorCode
      createdAt
    }
    userErrors {
      field
      message
    }
  }
}

 

In this modified mutation, we encapsulate your existing mutation inside the bulkOperationRunQuery mutation. The bulkOperationRunQuery mutation takes a query argument, which is the inner mutation you provided. It also accepts variables to pass the values for $media, $input1, and $input2.

By using bulkOperationRunQuery, you can combine multiple product creations into a single request and execute them as a bulk operation. This can help you manage the cost and reduce the number of individual requests made.

Remember to adjust the values of $media, $input1, and $input2 to match your specific product data.

Note: Be aware of the limitations and restrictions associated with using bulk operations in Shopify. Ensure that you follow the guidelines provided in the Shopify documentation and test your implementation thoroughly.

Thanks

Get pass your Store Core Web Vital Free Speed Optimization Audit, Chat on WhatsApp | Skype : oscprofessionals-87 | Email: pallavi@oscprofessionals.com | Hire us | Guaranteed Site Speed Optimization | Website Free Audit | Shopify Theme Customization | Build Shopify Private App | Shopify SEO | Digital Marketing | Oscp Upsell & Cross sell App : Free | Oscp Sales & Volume Discount App : Free | Custom Pricing Wholesale App : Free
Ahmed__
Shopify Partner
3 0 2

I tried this but I got 

"Field 'bulkOperationRunQuery' doesn't accept argument 'variables'"
 
If I remove that variables line I get
 
"Variable $input1 is declared by anonymous mutation but not used"
 
Do you know what is needed to get this working? I'm testing on postman, I also tried with NodeJS but I get the same results.
 
Thanks,
 
Ahmed