GraphQL update multiples using variables?

MozzoERP
Shopify Partner
83 4 18

Looking to update inventory levels in bulk:

Considering this, which works, but just curious if there is a way to use variables to reduce the size of this payload as we'll be updating many rows at once.

 

mutation {
  item1: inventoryActivate (inventoryItemId: "gid://shopify/InventoryItem/401570dddd91", locationId: "gid://shopify/Location/5ddddd983", available: 22) {
    inventoryLevel {
      available
    }
    userErrors {
      field
      message
    }
  }
  
  item2: inventoryActivate (inventoryItemId: "gid://shopify/InventoryItem/40157dddd1423", locationId: "gid://shopify/Location/5916816ddddd3", available: 23 ) {
    inventoryLevel {
      available
    }
    userErrors {
      field
      message
    }
  }
}

 

 

Additionally, is it documented what the max nbr of rows you can update in a single request? i.e. in the above, how many items can I update at a time in a single call? Nevermind: found it: https://shopify.dev/concepts/about-apis/rate-limits

Chad Richardson
Mozzo Software - Modular Software that grows with you from solopreneur to a 200 person mega team. Why keep outgrowing your Shopify Apps? Start with us, and just use the modules you need, then add more as you grow. http://MozzoERP.com
Replies 6 (6)
_JCC_
Shopify Staff
Shopify Staff
196 27 54

Hi Chad,

Happy to help you out with this. You could use a fragment for what you want returned as illustrated below.

mutation {
  item1: inventoryActivate (inventoryItemId: "gid://shopify/InventoryItem/401570dddd91", locationId: "gid://shopify/Location/5ddddd983", available: 22) { 
      ...inventoryFields
   }
  
  item2: inventoryActivate (inventoryItemId: "gid://shopify/InventoryItem/40157dddd1423", locationId: "gid://shopify/Location/5916816ddddd3", available: 23 ) { 
      ...inventoryFields
   }
}
fragment inventoryFields on  InventoryActivatePayload{
   inventoryLevel {
      available
    }
    userErrors {
      field
      message
    }
}

 Hope this helps, if you have any other questions please don't hesitate to reach out.

Regards,

John

John C | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me 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

MozzoERP
Shopify Partner
83 4 18

@_JCC_ 

Thanks John, I think I wasn't clear. I apologize. I was referring to the mutation that I'm posting, not the returned data. I am curious if there is a way to essentially do an Insert of multiple data objects without replicating the mutation for each. i.e. listing the mutation only once, but then having a list of variables representing each data object to insert.

i.e. I was thinking something more along the lines of the below where the input is an array of inputs:

mutation ($input: InventoryActivate!) {
  inventoryActivate (input: $input) { 
      inventoryLevel {
         available
      }
      userErrors {
        field
        message
      }
   } 
}
{
    "input" : [
        {
             inventoryItemId: "gid://shopify/InventoryItem/401570dddd91", 
             locationId: "gid://shopify/Location/5ddddd983", available: 22
        },
        {
             inventoryItemId: "gid://shopify/InventoryItem/401570dddd91", 
             locationId: "gid://shopify/Location/5ddddd983", available: 22
        }
    ]
}

 

 

Chad Richardson
Mozzo Software - Modular Software that grows with you from solopreneur to a 200 person mega team. Why keep outgrowing your Shopify Apps? Start with us, and just use the modules you need, then add more as you grow. http://MozzoERP.com
_JCC_
Shopify Staff
Shopify Staff
196 27 54

Ok I see what you mean. I'm not sure in this instance with inventoryActivate it's possible. The closest to this I've tried is with customerUpdate doing something like this:

mutation ($john:CustomerInput!,$bob:CustomerInput!){
  john:customerUpdate(input: $john) {
    ...customerFields
  }
  bob:customerUpdate(input: $bob) {
    ...customerFields
  }
}
fragment customerFields on CustomerUpdatePayload {
  customer{
    lastName
    email
    id
  }
  userErrors{
    field
    message
  }
}

// Variables
{
	"john": {
		"id": "gid://shopify/Customer/someid",
		"firstName": "John"
	},
    "bob": {
		"id": "gid://shopify/Customer/someid",
		"firstName": "bob"
	}
}

 

John C | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me 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

MozzoERP
Shopify Partner
83 4 18

@_JCC_ thanks for your continued support. I see what you did, but it still is repeating code in the mutation definition, which I was hoping to avoid. I did find some stack overflow posts on using arrays as input parameters for mutations. This one is structured the way I was thinking:

type Movie {
   name: String
  }
  input MovieInput {
   name: String
  }
  mutation {
   addMovies(movies: [MovieInput]): [Movie]
  }

mutation {
  addMovies(movies: [{name: 'name1'}, {name: 'name2'}]) {
    name
  }
}

 

Have you seen any instances of using an array of objects as a mutation input like this or think it's possible?

Chad Richardson
Mozzo Software - Modular Software that grows with you from solopreneur to a 200 person mega team. Why keep outgrowing your Shopify Apps? Start with us, and just use the modules you need, then add more as you grow. http://MozzoERP.com
_JCC_
Shopify Staff
Shopify Staff
196 27 54

I see what you're getting at with the movie example.  I definitely see your point, and the benefit to having something like an inventoryActivations mutation that accepted an array of items to activate but that's not available today. Shopify doesn't provide a lot of bulk write operations in this manner but I can certainly pass along this feedback. My examples were just some context in my own testing and experiences, I apologize that they were not more relevant. 

If you want to send multiple inventoryActivate mutations in one HTTPS request the payload will be larger yes, but assuming the payload can be received by Shopify, there could still be savings in making one request containing say one hundred inventoryActivate mutations  vs. one hundred individual requests. 

The update you're making is 10 units of complexity per mutation. So in theory you could send 100 inventoryActivate mutations in one request which would use up all of your rate limit. At a refresh rate of 50 points per second you'd have to wait to send the next batch of 100. I apologize but I don't have any test data or metrics on the potential time savings processing a large dataset in this way.

Regards,

John

 

 

John C | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me 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

MozzoERP
Shopify Partner
83 4 18

@_JCC_  thanks John. No, your examples were fine. Thank you.

Yes, that'd be great if you could pass it on. I'd think that would be a great feature. I've seen other posts about updating inventory in bulk as well and I think it would be a great feature to support for bulk updating of any object (i.e. Product prices, etc).

Again, thanks for your time and help!

Chad Richardson
Mozzo Software - Modular Software that grows with you from solopreneur to a 200 person mega team. Why keep outgrowing your Shopify Apps? Start with us, and just use the modules you need, then add more as you grow. http://MozzoERP.com