What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to get single product with GraphQL Admin API

How to get single product with GraphQL Admin API

marshall2056
Visitor
3 0 0

Hi everyone!

I'm quite new to Shopify and GraphQL but I have some coding experience at hand..

I'm trying to fetch single product with Admin API but it is whining about "Variable $id of type ID! was provided invalid value".

What I'm trying to do is as follows:

 

query GetProductsById($id: ID!) {
  product(id: $id) {
    title
  }
}

 

 

When I do the almost the same, I can get product by id:

 

query {
  product(id: "gid://shopify/Product/1974208364566") {
    title
  }
}

 

 

Now I'm not sure what I'm doing wrong here, when I'm trying the same as above while fetching all products it goes well and there is no problem with my query.

Please note that I have also tried with encoding GID to base64 but with same error. Also I have tried with "node" fragments which gives me the same error.

Error:

 

{
  "errors": [
    {
      "message": "Variable $id of type ID! was provided invalid value",
      "locations": [
        {
          "line": 1,
          "column": 23
        }
      ],
      "extensions": {
        "value": null,
        "problems": [
          {
            "path": [],
            "explanation": "Expected value to not be null"
          }
        ]
      }
    }
  ]
}

 

 

Somehow it seems that variables are not passed, or I'm crazy.

😕NOTE: I'm testing these queries at https://shopify.dev/graphiql/admin-graphiql

Replies 6 (6)

Gregarican
Shopify Partner
1033 86 292

You have to provide the variable's value in the Query Variables section at the bottom of the form. See screen shot below. Hope this helps!

Untitled.png

marshall2056
Visitor
3 0 0

Thank you for the fast response,

I'm doing it quite the same as shown bellow, but somehow I'm getting this error and you don't 😕

marshall2056_0-1606915991712.png

 

marshall2056
Visitor
3 0 0

It's solved now, I'm not quite sure what I did differently but somehow it's started to work. Still their online explorer does not work.

Thank you for your support, much appreciated!

Gregarican
Shopify Partner
1033 86 292

Glad it's working. The other thing to check is that the ID is actually one that is defined. Easiest way to grab a few product ID's to test in your query above is something like this. Copy one of these and paste into your query variable. That should work!

{
  products(first: 5) {
    edges {
      node {
        id
      }
    }
  }
}

  

Kumar2573
Shopify Partner
174 8 23

{
product(id: "gid://shopify/Product/8444550185239") {
title
description
onlineStoreUrl
priceRangeV2 {
minVariantPrice {
amount
currencyCode
}
}
}
}

Shopify Certified Developer Profile - https://www.credly.com/badges/1733d05c-2a88-490a-aef5-b01baf3b94ce/public_url
Expert Shopify Plus & shopify Developer & Theme customizer
- If the problem is solved remember to click to Accept Solution
- Press Like ?? if the answer was helpful
- Still, Facing issue, Reach out to me Email :- jitendra3496@gmail.com | Skype :- jitendra2573_1
- Direct Hire me :- Direct Contact me

ShopifyDevSup
Shopify Staff
1453 238 525

Hey @marshall2056 ! Thanks for sharing those examples. The query looks correct. What are you defining for the variables? 


It should look like this: 

query GetProductsById($id: ID!) {
 product(id: $id) {
   title
 }
}

Variables: 

{
 "id": "gid://shopify/Product/1974208364566"
}

Screenshot from Graphiql for reference. 

 

Hope that helps! 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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