How can I get list type references of metafield?

Hi community,

I would like to retrieve variants in product metadata.

I couldn’t find any sample code for the list.variant_reference case in the documentation.

This is my query.

query { product(handle: "quicklashcurler") {
stylingProducts: metafield(namespace: "styling", key: "products") {
value type reference { ... on ProductVariant { title } } }
}}

This is result. API returns "reference": null.

{"data": {"product": {"stylingProducts": {"value": "[\"gid://shopify/ProductVariant/44210311430390\",\"gid://shopify/ProductVariant/44210171183350\",\"gid://shopify/ProductVariant/44210164531446\"]","type": "list.variant_reference","reference": null}}}}

How can I get list type reference?

4 Likes

(English Below)

いまのところ、ベストショットはmetafieldのvalueで返ってきた値をparseして、

const ids = JSON.parse(product.{key_name}.value);

再度、queryで取得する方法しかわかりませんでした。

const EXAMPLE_QUERY = gql`
  query Product($ids: [ID!]!) {
    nodes(ids: $ids) {
      ... on Product {
        title
      }
    }
  }
`;

So far, I could only find the way to get values of list-type reference by parsing the string values of metafield to the JSON, and using it with the query above.
that’s the only & best shot I could find.

Does anybody have a better solution?

Kind Regards,
K

1 Like

It seems that the 2022-10 update supports list type references.

You can now access a references connection on a metafield. Use this connection to resolve metafield values of a list reference type to their underlying resource. This new connection is paginated and works similarly to the existing reference field, which is used for single references.

https://shopify.dev/api/release-notes/2022-10#graphql-storefont-api-changes

Thank you shopify team!

2 Likes

Great thanks! Just used this way and it works perfectly:

query {
	product(handle: "product-handle") {
		related_products: metafield(
			namespace: "custom"
			key: "related_products"
		) {
			references(first: 10) {
				edges {
					node {
						...on Product {
							title
							handle
						}
					}
				}
			}
		}
	}
}