Solved

Could I query a productvariant by id of the variant?

guid
Tourist
14 0 1

I use the follow command to get, but the response error is "Field 'productVariant' doesn't exist on type 'QueryRoot'".

 

curl -H "X-Shopify-Storefront-Access-Token: xxxxxxxxxxx" -H "Content-Type: application/graphql" https://connecty-store.myshopify.com/api/2019-10/graphql.json -d 'query {productVariant(id:"31338369777769") {... on productVariant{image{src}}}}'

Yes, I can't find productVariant in the list of  Storefront API > API reference > QueryRoot, but it exist in  Storefront API > API reference > Queryable objects, so I believe productVariant can be queried. 

 

Is it possible to query productVariant use Storefront API ?

 

Accepted Solution (1)
hassain
Shopify Staff (Retired)
624 104 187

This is an accepted solution.

Hi @guid ,

 

As you mentioned in your above comment, Product Variant is not a Query Root Object for the Storefront API. This means that you cannot direct query for Product Variants or by a specific Product Variant using the Storefront API. However you can do this if you use the GraphQL Admin API.

 

However if you already do know the Product Variant ID in it's Base64 encoded value (as per your last comment), you can query for it by using the "node" Query Root. Here is the query to show how you can do this: 

 

query {
  node(id: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMTMzODM2OTc3Nzc2OQ==") {
    ... on ProductVariant {
      image {
        originalSrc
      }
    }
  }
}

 

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 3 (3)

guid
Tourist
14 0 1

convert gid://shopify/ProductVariant/31338369777769 to "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMTMzODM2OTc3Nzc2OQ==" and use this id to query

curl -H "X-Shopify-Storefront-Access-Token: xxxxxxxxxxx" -H "Content-Type: application/graphql" https://connecty-store.myshopify.com/api/2019-10/graphql.json -d 'query {productVariant(id:"Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMTMzODM2OTc3Nzc2OQ==") {... on productVariant{image{src}}}}'

I get the same error

hassain
Shopify Staff (Retired)
624 104 187

This is an accepted solution.

Hi @guid ,

 

As you mentioned in your above comment, Product Variant is not a Query Root Object for the Storefront API. This means that you cannot direct query for Product Variants or by a specific Product Variant using the Storefront API. However you can do this if you use the GraphQL Admin API.

 

However if you already do know the Product Variant ID in it's Base64 encoded value (as per your last comment), you can query for it by using the "node" Query Root. Here is the query to show how you can do this: 

 

query {
  node(id: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMTMzODM2OTc3Nzc2OQ==") {
    ... on ProductVariant {
      image {
        originalSrc
      }
    }
  }
}

 

To learn more visit the Shopify Help Center or the Community Blog.

guid
Tourist
14 0 1

Hi, hassain