Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
The documentation says:
MetafieldReference
Returns a reference object if the metafield definition's type is a resource reference.
but if `type` is `collection_reference` the `reference` is always `null`.
I want my articles to have the same background color saved in the referenced collection's metafields. This should be possible by traversing the graph based on the documentation. This is my query:
fragment ArticleCore on Article {
id
handle
title
content
tags
blog {
id
handle
}
collection: metafield(namespace: "custom", key: "related_collection") {
id
type
reference {
__typename
}
}
}
and this is the response:
{
"id": "gid://shopify/Article/596103856243",
"handle": "test-article-1",
"title": "Test Article 1",
"content": "lorem ipsum",
"tags": [
"cotton",
"summer",
"vintage"
],
"blog": {
"id": "gid://shopify/Blog/80360636531",
"handle": "news",
"__typename": "Blog"
},
"blocks": {
"id": "gid://shopify/Metafield/20625132814451",
"namespace": "custom",
"key": "blocks",
"description": null,
"type": "json",
"value": "{}",
"__typename": "Metafield"
},
"collection": {
"type": "collection_reference",
"reference": null, // should not be null...
"__typename": "Metafield"
},
"__typename": "Article"
}
This query should work on the API but it doesn't work because `reference` union is `GenericFile | MediaImage | Page | Product | ProductVariant | Video`
fragment ArticleCore on Article {
id
handle
title
content
tags
blog {
id
handle
}
collection: metafield(namespace: "custom", key: "related_collection") {
id
type
reference {
__typename
... on Collection {
color: metafield(namespace: "custom", key: "color") {
value
}
}
}
}
}
This is I believe broken functionality disregarding the tech for this type of metafield. If you add the field and just use native Shopify metafield on clean installation of Shopify and add a dummy collection to a product and just output it in liquid it will always be null too. Its seems to be an issue with this specific metafield. I do not think your code is at fault here.
For some reason Shopify returns the gid data but the .value does not contain Shopify object data so logic is broken here and should be checked debugged and fixed by Shopify team.
I jumped the gun. it is working as intended tested with graphql for both products & articles. Please check if your collection is available to Online store, enabled. Null that happens is probably because the collection is disabled or in incorrect sales channel.
Try running clean article graphl query to see what you get in repsonse.