1

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.

1 Answer 1

1

You can retrieve more information through a reference or references.

Reference: A referenced object if the field type is a resource reference.
References: A list of referenced objects if the field type is a resource reference list.

You may try querying data like this:

{
  metaobject(handle: {handle: "template-home", type: "header_template"}) {
    handle
    type
    page_url: field(key: "page-url") {
      value
    }
    header_info: field(key: "header_info") {
      value
      __typename
      reference {
        __typename
        ... on Product {
          id
          title
        }
        ... on Metaobject {
          fields {
            key
            type
            value
          }
        }
      }
      references(first: 10) {
        edges {
          node {
            __typename
            ... on Product {
              id
              title
            }
            ... on Metaobject {
              fields {
                key
                type
                value
              }
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.