Variable $profile of type DeliveryProfileInput! was provided invalid value for variantsToAssociate

brterbush
Tourist
5 0 1

I am attempting to add multiple variants of a product to a delivery profile using the deliveryProfileUpdate mutation via the Shopify graphQL API. According to the documentation - https://shopify.dev/docs/admin-api/graphql/reference/mutation/input-object/deliveryprofileinput?api[... - the variantsToAssociate input fields accepts "product variant ids to be associated with this profile." 

However, when I make the request: 

 

 

 

            const graphQLQuery = `mutation deliveryProfileUpdate($id: ID!, $profile: DeliveryProfileInput!) {
              deliveryProfileUpdate(id: $id, profile: $profile) {
                profile {
                  id
                }
                userErrors {
                  field
                  message
                }
              }
            }`;

            const tagProductOptions = {
              method: 'POST',
              uri: `https://${shopName}.myshopify.com/admin/api/2020-07/graphql.json`,
              headers: {
                'X-Shopify-Access-Token': `${privateAppSecret}`,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
              },
              body: JSON.stringify({
                query: `${graphQLQuery}`,
                variables: {
                  id: "gid://shopify/DeliveryProfile/48121937986",
                  profile: {
                    "variantsToAssociate": `${productVariants}`
                  }
                }
              })
            };

 

 

 

 

 

 

And pass an array of productVariants I get the error:

Variable $profile of type DeliveryProfileInput! was provided invalid value for variantsToAssociate

Yet the mutation works if I only pass a single variant to variantsToAssociate

The documentation says I could pass multiple variants... what am I doing wrong?

I appreciate the help!

0 Likes
Kevin_A
Shopify Staff
Shopify Staff
292 36 47

Hey @brterbush 

My guess is that you aren't formatting the variable correctly. Based on your example, this formatting should work correctly for multiple variants:

{
  "id":"gid://shopify/DeliveryProfile/48121937986",
  "profile": {
    "variantsToAssociate": ["gid://shopify/ProductVariants/1234","gid://shopify/ProductVariants/1234"]
  }
}

 

Kevin_A | 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

0 Likes
brterbush
Tourist
5 0 1

Hey @Kevin_A - 

Hm, the only other code above that is - 

          const productVariants = product.variants.map((variant) => {
            return `gid://shopify/ProductVariant/${variant.id}`
          });

 

and in the terminal, the logged `productVariants` returns :

[ 'gid://shopify/ProductVariant/31247202680898',
  'gid://shopify/ProductVariant/31247202451522',
  'gid://shopify/ProductVariant/31247202517058',
  'gid://shopify/ProductVariant/31247202582594',
  'gid://shopify/ProductVariant/31247202648130',
  'gid://shopify/ProductVariant/31247202713666',
  'gid://shopify/ProductVariant/31247202418754',
  'gid://shopify/ProductVariant/31247202549826',
  'gid://shopify/ProductVariant/31247202615362',
  'gid://shopify/ProductVariant/31247202484290' ]

 

I'm assuming this is the correct formatting - would the single quotes make a difference?

Thanks!

B

0 Likes
Kevin_A
Shopify Staff
Shopify Staff
292 36 47

Hey @brterbush 

Did you try using double quotes to see if it makes a difference? I wasn't able to get single quotes to pass validation for me. Also, if that doesn't make a difference, can you try hardcoding the variables and test with that?

Kevin_A | 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

0 Likes
brterbush
Tourist
5 0 1

Hey @Kevin_A 

This error message makes me think that the array is not being properly parsed - 

{"errors":[{"message":"Variable $profile of type DeliveryProfileInput! was provided invalid value for variantsToAssociate.0 (Invalid global id 'gid:\/\/shopify\/ProductVariant\/31944143142978,gid:\/\/shopify\/ProductVariant\/31944143241282,gid:\/\/shopify\/ProductVariant\/31944143339586,gid:\/\/shopify\/ProductVariant\/31944143437890,gid:\/\/shopify\/ProductVariant\/31944143536194')","locations":[{"line":1,"column":42}],"extensions":{"value":{"variantsToAssociate":"gid:\/\/shopify\/ProductVariant\/31944143142978,gid:\/\/shopify\/ProductVariant\/31944143241282,gid:\/\/shopify\/ProductVariant\/31944143339586,gid:\/\/shopify\/ProductVariant\/31944143437890,gid:\/\/shopify\/ProductVariant\/31944143536194"},"problems":[{"path":["variantsToAssociate",0],"explanation":"Invalid global id 'gid:\/\/shopify\/ProductVariant\/31944143142978,gid:\/\/shopify\/ProductVariant\/31944143241282,gid:\/\/shopify\/ProductVariant\/31944143339586,gid:\/\/shopify\/ProductVariant\/31944143437890,gid:\/\/shopify\/ProductVariant\/31944143536194'","message":"Invalid global id 'gid:\/\/shopify\/ProductVariant\/31944143142978,gid:\/\/shopify\/ProductVariant\/31944143241282,gid:\/\/shopify\/ProductVariant\/31944143339586,gid:\/\/shopify\/ProductVariant\/31944143437890,gid:\/\/shopify\/ProductVariant\/31944143536194'"}]}}]}

 

but I'm not entirely understanding the break  

Any ideas?

0 Likes
brterbush
Tourist
5 0 1

Also, I should mention in response to your earlier post @Kevin_A - when I hardcoded the variable it was successful. When I log 'productVariants' in the terminal, the array is formatted correctly, and when I paste that in to variables as a hardcoded array it successfully adds the variants to the shipping profile, but when passing a template literal for the array as `${productVariants}` it is suddenly parsed incorrectly and the error message returns. I'm not understanding where the break occurs then  

0 Likes
brterbush
Tourist
5 0 1

SOLVED: 

When forming the request and JSON.stringifying the body, I was stringifying a JavaScript object that contains JSON strings as values (the 'productVariants' variable) which was malforming the data and not submitting an array to the graphQL point as expected, but submitting a single string. - 

            const tagProductOptions = {
              method: 'POST',
              uri: `https://${shopName}.myshopify.com/admin/api/2020-07/graphql.json`,
              headers: {
                'X-Shopify-Access-Token': `${privateAppSecret}`,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
              },
              body: JSON.stringify({
                query: `${graphQLQuery}`,
                variables: {
                  id: "gid://shopify/DeliveryProfile/48121937986",
                  profile: {
                    "variantsToAssociate": `${productVariants}`
                  }
                }
              })
            };

 

The solution was to remove the 'profile' variable object from the body of the request and instead add directly to the query while stringifying the array there, instead - 

            const graphQLQuery = `mutation deliveryProfileUpdate($id: ID!) {
              deliveryProfileUpdate(id: $id, profile: {variantsToAssociate: ${JSON.stringify(productVariants)}}) {
                profile {
                  id
                }
                userErrors {
                  field
                  message
                }
              }
            }`;

 

Thank you for your help though, @Kevin_A ! Hopefully this helps someone else if they get stuck