How to get single product with GraphQL Admin API

New Member
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

0 Likes
Shopify Partner
871 76 189

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

0 Likes
New Member
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

 

0 Likes
New Member
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!

0 Likes
Shopify Partner
871 76 189

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

  

0 Likes