client.product.fetch(productId) not returning product's Tags

Solved
Steve_Leggat
Shopify Partner
6 1 3

Using JS Buy SDK I'm trying to get all fields from a product using the following code (from here).

client.product.fetch(productId).then((product) => {
  // Do something with the product
  console.log(product);
});

It seems to return everything else, but not product Tags. Is there something I'm overlooking, or should I be trying to retrieve the product another way?

0 Likes
Steve_Leggat
Shopify Partner
6 1 3

This is an accepted solution.

Finally worked out an alternative, albeit longwinded, way.

 

 

const productsQuery = shopClient.graphQLClient.query((root) => {
  root.addConnection('products', { args: { first: 1, query: 'id:{{the_product_id}}'}}, (product) => {
    product.add('title');
    product.add('tags');
    product.add('availableForSale');
    product.add('createdAt');
    product.add('updatedAt');
    product.add('descriptionHtml');
product.add('handle'); product.add('productType'); product.add('vendor'); product.add('publishedAt'); product.add('onlineStoreUrl'); product.addConnection('images', { args: { first: 250 } }, (images) => { images.add('src'); images.add('id'); images.add('altText'); }) product.addConnection('variants', { args: { first: 250 } }, (variants) => { variants.add('id'); variants.add('product'); variants.add('title'); variants.add('price'); variants.add('image', (image) => { image.add('src'); image.add('id'); image.add('altText'); }) variants.add('selectedOptions', (opts) => { opts.add('name') opts.add('value') }) }) }) }); shopClient.graphQLClient.send(productsQuery).then(({product, data}) => { console.log(JSON.stringify(product, null, 4)); });

 

 

0 Likes
akievet
New Member
1 0 0

I'm looking for a way to return the product tags for the line items in a cart. How can I query for just those products' tags? Thanks!

0 Likes
city17
Tourist
8 0 0

Hi Steve, did you ever find a better solution to this? I'm trying to fetch 'quantityAvailable', which isn't there in the standard query.

Seems like there has to be a way to change the graphql schema somehow for Shopify-Buy SDK?

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 228

@city17 , this is in response to this and your other recent post. If you can construct a GQL request such as this in your client then it works fine. If you don't need to pull specific per-location availability and just the sum total on-hand it's all good.

{
  productVariant(id: "gid://shopify/ProductVariant/31620852416564") {
    inventoryQuantity
  }
}

 

0 Likes
city17
Tourist
8 0 0

Hi Greg, thanks, that seems about what I need. Similar to what Steve posted I think?

Still not entirely sure how to go about it. Haven't used the SDK so much yet, as gatsby-source-shopify does most of the work, but this was a specific change I needed to make.

The original code is this:

      client.product.fetch(productId).then(fetchedProduct => {
        // this checks the currently selected variant for availability
        const result = fetchedProduct.variants.filter(variant => {
          return variant.id === productVariant.shopifyId
        })
        if (result.length > 0) {
          setAvailable(result[0].available)
        }
      })

How exactly would you use that productId to pass it to a custom graphql request?

 

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 228

You would have to fit the GQL syntax to your own client's wrapper formatting, but the raw GQL request would look something like this example. I have the product ID and am iterating through the first 5 variants. Pulling the variant ID, the variant title, and the quantity available. Hope this helps!

{
  product(id: "gid://shopify/Product/4452686168116") {
    title
    variants(first: 5) {
      edges {
        node {
          id
          title
          inventoryQuantity
        }
      }
    }
  }
}

 

 

city17
Tourist
8 0 0

Thanks again, Greg. I've tried to implement this, using this syntax: https://github.com/Shopify/js-buy-sdk/issues/753#issuecomment-641705496

However it results in the following error: 

Error: No field of name "quantityAvailable" found on type "ProductVariant" in schema.

I'm a bit stuck why this isn't working. If I do a regular GQL query at build time it works fine. And according to the Shopify docs it should work as well. If I remove the bit about quantityAvailable (`variant.add(quantityAvailable`) the fetch request works fine.

Any idea where things might be going wrong?

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 228

If you look at my example, the field is inventoryQuantity I believe. Try that and see how it goes!

0 Likes
city17
Tourist
8 0 0

Found the solution! Needed to change the Shopify Buy SDK to the unoptimized version.

inventoryQuantity is used in the standard API, StoreFront API uses quantityAvailable (wonder why they named them differently?)

0 Likes