Why is my GraphQL query not showing metafield for all products?

Erik_Abrio
Shopify Partner
23 5 2

Hello i have following Issue,

 

I want to get the metafield id of my sold_out_metafield to change its value in the productUpdate Mutation. My Problem is that this query is showing the metafield for some products but not for products where the metafield is not initialized with a value. How can i get the id of this metafield to set the value? or how can i initially set the value to an empty value so every id is initialized.

 data = {
      "query": `query {
  products(first: 10, after:"${cursor}",query:"(product_type:bundle)") {
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      node {
        id
        title
        tags
        variants(first: 1){
          edges{
            node{
              id
              inventoryItem{
                id
                tracked
                    inventoryLevels(first:1){
                      edges{
                        node{
                          location{
                            id
                          }
                        }
                      }
                }
                
              }
            }
          }
        }
        metafields(first: 1,namespace:"bundle"){
          edges{
            node{
              value
              key
              namespace
            }
          }
        }
        sold_out_metafield:metafields(first: 1,namespace:"sold_out"){
          edges{
            node{
              id
            }
          }
        }
      }
    }
  }
}`
,
}
      }


      let response = await axios($, {
      method: "post",
      url: `https://${this.shopify_developer_app.$auth.shop_id}.myshopify.com/admin/api/2023-04/graphql.json`,
      headers: {
        "X-Shopify-Access-Token": `${this.shopify_developer_app.$auth.access_token}`,
        "Content-Type": `application/json`,
      },
      data,
    })

Empty Metafield Response (showing only on some products): 

Erik_Abrio_0-1702982438792.png

Expected Response: 

Erik_Abrio_1-1702982494083.png

 

 

Reply 1 (1)

ClementOutis
Shopify Partner
24 4 9

Hi @Erik_Abrio,

 

First of all, a little tips when you get specific metafield on an object. 

You can query it like this :

{customer(id: "gid://shopify/Customer/2554556556556"){
    id
    example_metafield: metafield(namespace: "global", key: "example") {
        id 
        key
        namespace
        value 
    }
}}

With this version, you avoid to get the content inside an array (edges, node).

If the metafield doesn't exists, the value of the example_metafield will be null in the result.

 

A metafield cannot exist if it's not initialized, so cannot have ID.

 

You can create the metafield directly in the product update mutation if it doesn't exist.

Here is an example from the Shopify doc.

 

You can see below the content of the input for the productUpdate mutation (but it works for all object that can accept metafields).

"metafields": [
      {
        "namespace": "my_field",
        "key": "liner_material",
        "type": "single_line_text_field",
        "value": "Synthetic Leather"
      },
      {
        "id": "gid://shopify/Metafield/1069229034",
        "value": "Rubber"
      }
]

Here the metafield my_field.liner_material is created and the second metafield is updated with a new value just by passing it's ID.

 

Hope it's help you.

- Helpful? Like and Accept a solution
- Casper - Cart, sync and keep saved for a month your customer's cart across multiple device seamlessly.
- BrandCraft, give you all the keys to edit your checkout page, only for Shopify Plus with the Checkout Extensibility.