metafield reference is always null if type is collection_reference

metafield reference is always null if type is collection_reference

bkiac
Shopify Partner
1 0 1

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
					}
			}
		}
	}
}

 

 

 

 

Replies 2 (2)

GoranHorvat
Shopify Partner
9 1 6

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.

GoranHorvat
Shopify Partner
9 1 6

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.