Shopify API call to get variant of a product graphQL

Solved

Shopify API call to get variant of a product graphQL

HiT2
Tourist
7 0 1

Hello I have the following cURL:

curl -X POST \
https://your-development-store.myshopify.com/admin/api/2022-10/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \s

- d

'{
"query": "query { productVariants(query : "(title:S) AND (product_id:7253232058557") { edges { node { id title inventoryQuantity} } } }"

}'
}

 

I am trying to get the variant title S of a specific product (Black Hoodie with ID :7253232058557) but it returns the error 400 saying "Bad Request". What am I doing wrong?

Accepted Solution (1)

Umiko
Shopify Staff
42 8 13

This is an accepted solution.

Hi @HiT2 👋

 

A few things to note here:

  • querying `productVariants` requires `first` or `last` as an argument
  • when using curl, you will need to pass the query through a variable so that it can be recognized as a string literal - otherwise there are too many quotations!

So the query would look something like the below:

query FilteredVariants($search: String!){
    productVariants(first:5, query: $search){
        nodes {
            title
            inventoryQuantity
        }
    }
}

{ // variables
    "search": "title:S AND product_id:7253232058557"
}

That would make the cURL request:

curl -X POST \
'https://{{SHOP}}.myshopify.com/admin/api/2022-10/graphql.json' \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {{TOKEN}}' \
-d '{"query":"query FilteredVariants($search: String!){ productVariants(first:5, query: $search){ nodes { title inventoryQuantity } }}","variables":{"search":"title:S AND product_id:7253232058557"}}'

 

Hope that helps!

Umiko | API 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

View solution in original post

Replies 3 (3)

Umiko
Shopify Staff
42 8 13

This is an accepted solution.

Hi @HiT2 👋

 

A few things to note here:

  • querying `productVariants` requires `first` or `last` as an argument
  • when using curl, you will need to pass the query through a variable so that it can be recognized as a string literal - otherwise there are too many quotations!

So the query would look something like the below:

query FilteredVariants($search: String!){
    productVariants(first:5, query: $search){
        nodes {
            title
            inventoryQuantity
        }
    }
}

{ // variables
    "search": "title:S AND product_id:7253232058557"
}

That would make the cURL request:

curl -X POST \
'https://{{SHOP}}.myshopify.com/admin/api/2022-10/graphql.json' \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {{TOKEN}}' \
-d '{"query":"query FilteredVariants($search: String!){ productVariants(first:5, query: $search){ nodes { title inventoryQuantity } }}","variables":{"search":"title:S AND product_id:7253232058557"}}'

 

Hope that helps!

Umiko | API 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

HiT2
Tourist
7 0 1

Thank you!
I was also wondenring, is it possible to change the variant of a product of an order through PUT API call?

HiT2
Tourist
7 0 1

Thank you!
I was also wondenring, is it possible to change the variant of a product of an order through PUT API call?