Query product with app metafield value on checkout

I have an app that has its own metafields assigned to the products. They are read-only fields, as they only need to be used by one product. I am creating a checkout UI extension that will add a banner with that product info to allow a customer to add it to their cart if it doesn’t already exist. Very similar to this example. I am having a difficult time querying that product by the custom metafields set up by my app. Here is my current query:

`query ($query: String!) {
    products(first: 1, query: $query) {
        nodes {
            id
            title
            images(first: 1) {
                nodes {
                    url
                }
            }
            variants(first: 1) {
                nodes {
                    id
                    price {
                        amount
                    }
                }
            }
            metafield(namespace: "$app:protection_product", key: "is-protection-product") {
                value
            }
        }
    }
}`,
{
    variables: {
        query: "metafields.$app:protection_product.is-protection-product:true"
    }
}

I’m aware that the “$app:” in the query essentially stops the query, thinking everything after the colon is the value I’m looking for, which obviously doesn’t work. I’ve also manually put in the app namespace (app–12456–), but still the query is not working. The field is a true/false field, I just need to query the product with the “true” value. Regardless of how I mess with it, it is just returning the first product in my product list, which is not the correct product.

I’ve gone and added the metafield to my extension’s TOML file, and messed around with the useAppMetafields functionality, but from my understanding that’s only for the products that are present in checkout. I am looking to show a product that isn’t presently in the checkout/cart.

Any assistance on this would be much appreciated.

I’ve updated my query to look something like this:

"metafield:app--123456--protection_product.is-protection-product:true"

But, still not giving me back the proper response.

Can you try the query in the GraphiQL app to see if it works there, and you could simplify it a bit to be like:

query {
  products(first: 1, query: "metafields.app--123456--protection_product.is-protection-product:true") {
    nodes {
      id
      title
      metafield(namespace: "app--123456--protection_product", key: "is-protection-product") {
        value
      }
    }
  }
}

Hi, @Liam-Shopify. Thanks for the response. I tried it out in GraphiQL, and unfortunately I’m still just getting the first product in my list, which is not the correct product.

I tried the following query strings as well (updating the namespace to match my app’s ID):

"metafields.app--123456--protection_product.is-protection-product:\"true\""
"metafields.app--123456--protection_product.is-protection-product:\"True\""
"metafields.app--123456--protection_product.is-protection-product:true"
"metafields.app--123456--protection_product.is-protection-product:True"

All giving the same incorrect result. Am I just doing the query incorrectly? Not sure where to go from here.

It is a read only field, so not sure if that has something to do with it. Also, I do have “Filtering for products” off for that field. Not sure if that would mess with my results, too. But when I do see the product in the results, when I change the amount returned, the metafield does show in the result.

Thanks again for your help.

Following up with a solution, but along with another question. It looks like that the “adminFilterable” capability needs to be enabled in order to allow those metafield queries to work. After doing that, I was able to query the product I was looking for.

Which leads me to my next question. How do we use the dynamic “$app:” namespace in that query? When I insert that, I get an “Invalid search field for this query” error. Safe to assume the $app namespace will change on each site that it is installed on, so manually typing in the app namespace won’t work elsewhere.

Thanks.