Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Metaobject list reference not working with the storefront api

Metaobject list reference not working with the storefront api

PaulBouisset
Shopify Partner
5 0 6

Hi, 
I love the new metaobject but I have an issue using them with the api, I can't retrieve the metaobject reference on metafields.

I have a metafield on my pages called "sections" which is a list of metaobject. When using the storefront api to get this meta fields I see that it's working but the reference is always null

My Query:

metafield(namespace:"front",key:"sections"){
    id 
    type
    value
    reference{
      ...on Metaobject{
        id
      }
   }

The api returns

"metafield": {
        "id": "gid://shopify/Metafield/25653462532283",
        "type": "list.mixed_reference",
        "value": "[\"gid://shopify/Metaobject/7798971\",\"gid://shopify/Metaobject/7831739\"]",
        "reference": null,
        "references": null
      }

Do you know why my reference and references are null even if I can see on the api documentation that the query should work?

 

Replies 11 (11)

ShopifyDevSup
Shopify Staff
1453 238 527

Hi @PaulBouisset 👋

 

Since the metafield is of `list.mixed_references` type, you will need to use the `Metafields.references` connection in your query. It should something like the below:

 

 

{
  metafield(id: "gid://shopify/Metafield/25653462532283") {
    id
    type
    value
    references(first: 10) {
      nodes {
        ... on Metaobject {
          id
        }
      }
    }
  }
}

 

 

 

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

PaulBouisset
Shopify Partner
5 0 6

Hi, thank you for your help but my query was not complete in my previous message.
There is still an issue.
Here is my full query:

query PageContent{
 page(handle:"parent"){
  	id
  handle
  metafields(identifiers:[{namespace:"front",key:"sections"}]){
    id 
    type
    value
    reference{
      ...on Metaobject{
        id
      }
    }
    references(first:10){
      edges{
        node{
         	...on Metaobject{
            id
            type
          } 
        }
      }
      pageInfo{
        hasNextPage
      }
    }
  }
	}
}

And the answer

{
  "data": {
    "page": {
      "id": "gid://shopify/Page/87762206907",
      "handle": "parent",
      "metafields": [
        {
          "id": "gid://shopify/Metafield/25653462532283",
          "type": "list.mixed_reference",
          "value": "[\"gid://shopify/Metaobject/7798971\",\"gid://shopify/Metaobject/7831739\"]",
          "reference": null,
          "references": null
        }
      ]
    }
  }
}

As you can see references is null as well even if the value on my mixed reference contains metaobjects.

ShopifyDevSup
Shopify Staff
1453 238 527

Would you please try this curl request instead? We are unable to replicate this behaviour using version 2023-01.

 

curl -L -X POST 'https://STORE_NAME.myshopify.com/admin/api/2023-01/graphql.json' \
-H 'X-Shopify-Access-Token: ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"query ($mf: ID!) {\n metafield(id: $mf) {\n id\n type\n value\n references(first: 10) {\n nodes {\n ... on Metaobject {\n id\n }\n }\n }\n }\n}\n","variables":{"mf":"gid://shopify/Metafield/25653462532283"}}'

 

Otherwise, there may be an issue with this particular metafield ID 25653462532283 and we would need you to contact us here with an authenticated account with permissions to this merchant store. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

PaulBouisset
Shopify Partner
5 0 6

Thank you for taking the time on this!

This specific query works but it's via the admin api and I want to use the storefront api. The second issue is that for the query you sent I would need to know the metaobject id in advance so do two requests to get the data I need.

It seems that there is an issue getting the metaobjects reference in the storefront api

ShopifyDevSup
Shopify Staff
1453 238 527

Hey @PaulBouisset - we think we may have a resolution for this. Can you confirm if you've requested the unauthenticated_read_metaobjects  storefront API scope for your app on the shop? Ideally, if the scope wasn't approved by the shop, it would return an error message stating this, but it is possible that it could return a null array for the metaobject references field too. 

Hope to hear from you soon - let us know if this resolves the issue. If not, we'll take a closer look to confirm next steps (including a note to improve error messaging if this is the case here). Thanks again for reaching out, happy to keep digging into this if need be.  

Al | Shopify Developer Support 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

pietvanleeuwen
Shopify Partner
6 0 0

I'm also having this same issue.

 

v2023-01

 

Requested - 

swatch: metafield(namespace: "custom", key: "swatch") {
   key
   value
   references(first: 250) {
      edges {
      node {
         ... on Metaobject {
         fields {
            key
           value

...

 

Returning - 

 

"swatch": {
"key": "swatch",
"value": "[\"gid://shopify/Metaobject/191070393\",\"gid://shopify/Metaobject/191103161\",\"gid://shopify/Metaobject/191135929\"]",
"references": null
}

pietvanleeuwen
Shopify Partner
6 0 0

I tested with a single Metaobject and the results were the same.

PaulBouisset
Shopify Partner
5 0 6

Hi, I am using the Shopify graphql app and I re-installed it with all the permissions to make sure but there is still the same issue.
The references are always null.
Is it possible that this bug only happen because the permissions of the Shopify Graphql app are not working correctly?

pietvanleeuwen
Shopify Partner
6 0 0

I don't believe it is, I have all permissions set for storefront, and have tested in graphql app and directly to the api. @ShopifyDevSup can you confirm this works correctly in your test environment? Seems like something is a miss on the api. If I test a list of related products they come through correctly.

 

related: metafield(namespace: "custom", key: "related") {
   key
   value
   references(first: 250) {
      edges {
         node {
             ... on Product {
                title
                ...
 
Comes back as expected.

esposama
Shopify Partner
1 0 0

I am having this issue. Were you able to resolve it? 

pietvanleeuwen
Shopify Partner
6 0 0

I requested the metaobjects directly so - 

 

swatches: metaobjects(first: 250, type: "Swatch") {
edges {
node {
fields {
key
value
}
}
}
}

product(handle: $handle) {
id
...
 
That was sufficient for me, I didn't really need a restricted list of metaobjects, so retrieving them all worked.