Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

GraphQL Getting metafields

GraphQL Getting metafields

OutboardCare
Shopify Partner
4 0 0

Hiya, I am having trouble retrieving a custom metafield of a product using the GraphQL API.

 

I am trying to retrieve metafield supersessions in the namespace custom, so custom.supersessions.

 

This is my original query:

 

query getProductsWithMetaField($first: Int!, $cursor: String) {
    products(first: $first, after: $cursor) {
        pageInfo {
            hasNextPage
        }
        edges {
            cursor
            node {
                id
                title
                vendor
                variants(first: 1) {
                    edges {
                        node {
                            sku
                            price
                        }
                    }
                }
            }
        }
    }
}

This is how I tried to retrieve te metafields but failed because key and namespace can not be used together in this context:

 

query getProductsWithMetaField($first: Int!, $cursor: String) {
    products(first: $first, after: $cursor) {
        pageInfo {
            hasNextPage
        }
        edges {
            cursor
            node {
                id
                title
                vendor
                variants(first: 1) {
                    edges {
                        node {
                            sku
                            price
                        }
                    }
                }
                metafields(first: 1, namespace: "custom", keys: "supersessions") {
                    edges {
                        node {
                            value
                        }
                    }
                }
            }
        }
    }
}

I am however struggling to find a correct way to get this metafield whereas I have tried several options but was not able to find a solution. 

 

Thanks in advance

Reply 1 (1)

kjchabra
Shopify Partner
26 1 7

@OutboardCare in the metafields query block the word keys should be key.

 

I believe if you run that you would get all products in the particular paginated set. The products that don't have that metafield value you will see null as the value for those products. You will most likely have to use code to filter out the products after you get the results.