Get metafield count on product under namespace

Ari9
Excursionist
35 3 10

Using GraphQL and the Shopify storefront API, there's no way of getting the number of metafields under a namespace for a given product.

I don't understand why this method would only be under the admin API, and not available in GraphQL.

 

It would be great to be able to run a query such as:

 

{
  products(first: 10) {
    edges {
      node {
        metafields(namespace: "reviews") {
          count
        }
      }
    }
  }
}

 

But unfortunately there is no field of count available.

banned
Replies 11 (11)

HunkyBill
Shopify Expert
4846 60 552

Probably because the front-end is meant to be a fast support mechanism for getting data, and not for analytic use of said data. In thinking about it, if you know your namespace, it means you know your metafields. In other words, you should know how many to expect in the namespace. So why is a count needed? If you do need to go through a namespace thoroughly because you know it exists, but you have no idea how much stuff exists there, then yes, paging is your only solution.

So even if you got a count, you still have to page, so having a count is not terribly helpful. It kinda sucks that you are stuck with a primitive review system that might just be writing a new metafield entry per review in a namespace. That is dumb because you can store a large JSON structure in a key, meaning you could store a large X number of reviews in one single metafield.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Ari9
Excursionist
35 3 10

I need to know the total number of metafield reviews so I can show a summary of "This product has 1000 reviews".

It doesn't make sense to store all the reviews in a giant json metafield... that would be so slow to read and not scalable. I will paginate when I show the reviews individually, but I want a summary of the number of reviews. I don't want to store an entirely new metafield which is "reviewsCount" because it could go out of sync with the real number of reviews.

The API for getting the count of reviews exists under the admin API, my point is that it is not private information and I think should be moved to the storefront API.

banned
HunkyBill
Shopify Expert
4846 60 552

So make an API call the backend and get your precious count. One call won't hurt you. Secondly, while you are correct that storing reviews in JSON structures that could become large is a thing, you have zero evidence that would be slow and hard to scale. It is dumb simple Javascript to chew through and produce output, probably faster than your API intensive GQL calls. Prove it with benchmarks, I will concede. Otherwise, it's piffle.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Ari9
Excursionist
35 3 10

I don't get where your attitude comes from. I'm asking a question and didn't expect sarcasm from a "Shopify Expert".

I'm well aware that I can make a backend API for it to get my    " precious count "    ... but it requires me to make multiple calls to fetch just the count when it can easily be fetched within the same products query.

I'm not going to prove jack HunkyBill, I didn't make a question to provide proof... I didn't even object to the idea of making a backend API which fetches the count. I think you're piffle and I don't appreciate your lack of help. You literally contributed nothing so please let someone else make a contribution.

Thanks HunkyBill.

banned
HunkyBill
Shopify Expert
4846 60 552

One call to get a count, why more than one? As for my attitude, right back at you is some advice to not make blanket sweeping disparaging comments like "Does not scale", when really, that is not helpful either, since you have no clue if that is remotely true or not. Since you failed in the first place to articulate what you "are well aware of", I took it upon myself to try and steer you with 2 solutions, whereas you seem fixated on maintaining the status quo for yourself, if only Shopify would do the right thing for you, and give you a number. Anyway, whatever... thin skinned, thick-skinned, we both have better things to do than beat this dead horse.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Ari9
Excursionist
35 3 10

It is not scalable simply because: As the number of reviews increases, the slower it is for customers to load the reviews (not paginatable) and the more quota will be used from my GQL query.

If you're unhappy with your life, Shopify forums is not the place to prey on people. Good luck with your future support HunkyBill. I look forward to not seeing your reply.

If you don't have anything to contribute to a question, next time don't try to answer because it wastes everyones time.

banned
HunkyBill
Shopify Expert
4846 60 552

You are quick with the shallow thinking about the problem, same as the folks that implemented the crappy review system that uses metafields so poorly. I need no luck Ari9.

You have the privilege to not read my reply, nor to comment back, but you also have zero privilege to tell me what I can and cannot do with regards to these public forums. Happy to see you here asking questions, please do get comfortable with the world at large and not just the speck at the end of your nose.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

srdjan
Shopify Staff (Retired)
7 1 2

So, the main issue here is the usage of the Storefront API. As that API has a different set of access scopes than Admin API, we require an additional step to expose product metafields to it. There's a tutorial for it.

Because of that, there might be metafields under the namespace that aren't exposed to the Storefront API. As such, support for count doesn't make sense.

To learn more visit the Shopify Help Center or the Community Blog.

HunkyBill
Shopify Expert
4846 60 552

So by this explanation, simply put, if you have a namespace "reviews", and perhaps 1000 reviews saved in that namespace, you also have to have added a visibility record to each and every review with the review's key? Wow... so when this primitive review system adds a single new review to the namespace, it also has to create the permission for Storefront API to read it. Interesting.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
srdjan
Shopify Staff (Retired)
7 1 2

Yes, that's correct. Since there can be all kinds of data stored in metafields, we are requiring developers to be intentional about what is exposed to the Storefront API. These restrictions do not apply to Admin API, since the merchants authorize that access by approving app's access scopes.

To learn more visit the Shopify Help Center or the Community Blog.

Ari9
Excursionist
35 3 10

Thank you srdjan.

Having to have a separate database hosted by myself is something I really want to avoid. This way I don't have to manage my own database.


@srdjan wrote:

there might be metafields under the namespace that aren't exposed to the Storefront API. As such, support for count doesn't make sense.


I was thinking that the Storefront API would get the count for only the total number of public metafields in the namespace. The main benefit of this is that I can fetch all the product information in the one GraphQL call and I don't have to make a subsequent call to a personal API to fetch the number of metafields for the product.


This flow wouldn't be great for user experience. They would have to: Load the products > Load the total number of reviews for each product, which is an additional call for each and every product.

This means if my page has 20 products visible on the page, I'll need to make 20 API calls to 

/admin/api/2020-10/products/632910392/metafields/count.json

This isn't really a solution for me and it is a shame if I have to go down the path of hosting a separate database for the product reviews.

banned