In Shopify using GraphQL how should I query a metaobject and a sub-query metaobject in a single query?

1k Views Asked by At

I am building a website with Shopify Hydrogen.

Using GraphQL I am accessing MetaObjects.

The structure of the MetaObject is one field and one sub-MetaObject (a generic, reusable meta object).

I wish to have a query that gives me the fields from the MetaObject (working) and the Sub-MetaObject (not working).

My query looks like this...

query {
    metaobject(
        handle: {
            handle: "template-home",
            type: "header_template",
        }
    ) {
        handle,
        type
        page_url: field(key: "page-url") {
            value
        },
        header_info: field(key: "header_info") {
            value
        },    
    }
}

..and that gets half the job done but doesn't let me see the sub-metaobject and the result is...

{
    "data": {
        "metaobject": {
            "handle": "template-home",
            "type": "header_template",
            "page_url": {
                "value": "/"
            },
            "header_info": {
                "value": "gid://shopify/Metaobject/1234567890"
            }
        }
    }
}

I don't want the key to the Metaobject returned, I want the fields from the meta object as a sub-result (or even flattened in the parent would be fine too).

How do I write another sub-query to expose all the fields within this sub-meta object so I can run one command. The point of GraphQL is to avoid additional server side trips in comparison to REST, so presume this is possible but new to GraphQL.

If not possible, how do I avoid multiple calls to Shopify in Remix?

Thanks.

0

There are 0 best solutions below