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!

Reply 1 (1)
oscprofessional
Shopify Partner
15736 2353 3050

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 | Custom Pricing Wholesale App : Free | Hire us | Guaranteed Site Speed Optimization | Website Free Audit | Shopify Theme Customization | Build Shopify Private App | Shopify SEO | Digital Marketing