Solved

Storefront API returning undefined object when searching for product variant

FauxNostalgia
Shopify Partner
2 1 0
I'm having an issue where I'm fetching a request from my store using the Storefront API and I want to retrieve my one and only product in my store. 

My tech stack is simply nextjs.

My query is as follows:
 {
  products(first: 1) {
    edges {
      node {
        id
        title
        description
        images(first: 3) {
          edges {
            node {
              url
            }
          }
        }
        variants(first: 1) {
          edges {
            node {
              price
              compareAtPrice
              availableForSale
            }
          }
        }
      }
    }
  }
} 
The issue is that the query returns an undefined object ONLY when I include 'variant' in my query. When I remove it as follows, I'm able to successfully retrieve my product using the query below.
 
{
  products(first: 1) {
    edges {
      node {
        id
        title
        description
        images(first: 3) {
          edges {
            node {
              url
            }
          }
        }
      }
    }
  }
}
Accepted Solution (1)

FauxNostalgia
Shopify Partner
2 1 0

This is an accepted solution.

Solution:

The first query I sent doesn't work for the Storefront API (only works for the Admin API, as tested with the GraphiQL Explorer).

This works

 

query {
              products(first: 1) {
                edges {
                  node {
                    id
                    title
                    description
                    images(first: 3) {
                      edges {
                        node {
                          url
                        }
                      }
                    }
                    variants(first: 3) {
                      edges {
                        node {
                          title
                          price {amount}
                          availableForSale
                        }
                      }
                    }
                  }
                }
              }
          }
        `,

 

 

View solution in original post

Reply 1 (1)

FauxNostalgia
Shopify Partner
2 1 0

This is an accepted solution.

Solution:

The first query I sent doesn't work for the Storefront API (only works for the Admin API, as tested with the GraphiQL Explorer).

This works

 

query {
              products(first: 1) {
                edges {
                  node {
                    id
                    title
                    description
                    images(first: 3) {
                      edges {
                        node {
                          url
                        }
                      }
                    }
                    variants(first: 3) {
                      edges {
                        node {
                          title
                          price {amount}
                          availableForSale
                        }
                      }
                    }
                  }
                }
              }
          }
        `,