Shopify Storefront API GraphQL doesn't return published products

sabaraouf
New Member
6 0 0

Hi everybody, I'm trying to get published products from Storefront API but it returns unpublished products. Is it a bug? Or something wrong with query?

here is my query

 

{
  products(first: 1, query: "published_status:published") {
    edges {
      cursor
      node {
        id
        title
        handle
        onlineStoreUrl
        vendor
        variants(first: 1) {
          edges {
            node {
              title
              image {
                src
              }
              price
            }
          }
        }
      }
    }
  }
}

 

I also need to get products by Collections but I cannot add query in products, how do I get published products filter by a collection? This query gives error.

{
collectionByHandle(handle: "HANDLE") {
          products(first:1, query: "published_status:published") {
            edges {
              cursor
              node {
                id
                title
                handle
                onlineStoreUrl
                variants(first: 1) {
                  edges {
                    node {
                      title
                      image {
                        src
                      }
                      price
                    }
                  }
                }
              }
            }
          }
        }
        }

 

Replies 11 (11)

KarlOffenberger
Shopify Partner
1873 184 900

The first query you are running is for GraphQL Admin API - Storefront API does not have a query filter of published_status. For Storefront API, it is either published to that channel and thus queryable or it isn't and thus you won't be able to query it no matter what. Do not confuse Admin APIs and Storefront API - the GraphQL schema is different.

sabaraouf
New Member
6 0 0

1. Is there a way I can get published products filter by a collection from Storefront API?

2. If I have to use Admin graphql api can you please share an example of published products filter by a collection?

KarlOffenberger
Shopify Partner
1873 184 900
  1. When using Storefront API you always only get published resources. This is for Storefront API (but also works for Admin)
    {
      collectionByHandle(handle: "foo-collection") {
        products(first: 5) {
          edges {
            node {
              title
            }
          }
        }
      }
    }
    Will only return products in that collection that are published i.e. to the Online Store channel and publication date is less than now.
  2. Not sure what you are trying to accomplish, but if it is anything to do with fetching data for a storefront to show, then I wouldn't advise you to use the Admin API at all. Even if, you will run into the issue of access denied etc. which all boils down to publications_read and publications_write scopes not being available (the entire publications stuff seems in a very wonky state API wise) and AFAIK Shopify haven't voiced any statement yet.

    The most you can do is check whether the products are published on the current publication, but since that will be tied to your app's channel, it's sort of useless for most common use cases (below query is for GraphQL Admin API)
    {
      collectionByHandle(handle: "foo-collection") {
        products(first: 5) {
          edges {
            node {
              title
              publishedOnCurrentPublication
            }
          }
        }
      }
    }
sabaraouf
New Member
6 0 0

Can you please explain why does this query return unpublished products? This is from storefront api https://help.shopify.com/en/api/custom-storefronts/storefront-api/reference/queryroot

{
  products(first: 10) {
    edges {
      node {
        id
        onlineStoreUrl
      }
    }
  }
}
KarlOffenberger
Shopify Partner
1873 184 900

@sabaraouf wrote:

Can you please explain why does this query return unpublished products?


What is the URL you are querying?

/admin/api/graphql.json

 or 

/api/graphql

 

sabaraouf
New Member
6 0 0

Thanks for the details answer. I only need to fetch the published products filtered by a collection. 

I'm using api endpoint https://store.myshopify.com/api/graphql and this is my query as you shared

{
  collectionByHandle(handle: "HANDLE") {
    products(first: 5) {
      edges {
        node {
          title
          onlineStoreUrl
        }
      }
    }
  }
}

Screenshot 2019-02-22 at 10.57.16 PM.png

 

It returns some onlineStoreUrl: null which indicates product is unpublished. Why is this so?

KarlOffenberger
Shopify Partner
1873 184 900

Most likely because you have it published on your app channel. Check the product where you are seeing that onlineStoreUrl is null and you will probably find that it is published for your app name that you are using for Storefront API access.

 

You would need to unpublish it for your app channel and only have it in unpublished state for the Online Store like so

jhfqoVQ

sabaraouf
New Member
6 0 0

I see.. So I created a private app for Storefront API and gave it permissions to read the products and it attached all the products to the private app. Now is there an easy way to undo this operation for unpublished products? Any option in admin UI? 

KarlOffenberger
Shopify Partner
1873 184 900

rgbblBu

KarlOffenberger
Shopify Partner
1873 184 900

Or if you have plenty, bulk actions

 

M6pWOi3

sabaraouf
New Member
6 0 0

Thank you. Yes I was looking for bulk operation option.