Hi,
I am accessing my metafields successfully from the API. The issue I have is that one of the metafields is a MediaImage and I can’t can’t access the image URL as one of the properties.
My working Swift Query looks like this:
.edges { $0
.cursor()
.node { $0
.id()
.title()
.handle()
.descriptionHtml()
.description()
.variants(first: 250) { $0
.fragmentForStandardVariant()
}
.images(first: 250){ $0
.fragmentForStandardProductImage()
}
.metafields(identifiers: arrMetafieldIDs) { $0
.value()
.key()
.reference { $0
.onMediaImage { $0 // This is how I request it
.image { $0
.url() // I need this URL
}
}
}
}
}
}
GraphQL working Query:
{
product(handle: "myHandle") {
title
myFancyName: metafield(namespace: "custom", key: "myKey") {
reference {
... on MediaImage {
image {
url
}
}
}
value
}
}
}
GraphiQL Response from the above query:
{
"data": {
"product": {
"title": "My Product Title",
"myFancyName": {
"reference": {
"image": {
"url": "https://cdn.shopify.com/s/files/1/0569/7244/3738/files/SHOPIFY_Product_Image.jpg?v=448688474"
}
},
"value": "gid://shopify/MediaImage/2365495288996"
}
}
}
}
Trying to Access the Image URL of the MetaField
private var metaField: [Storefront.Metafield?]
let filteredArr = metaFields.filter { item in
item?.key == "myKey"
}
let metafieldURL = filteredArr[0]!.reference! // Can't get beyond the reference
Printing the metafieldURL variable gives the following:
<MediaImage: ["image": {
url = "https://cdn.shopify.com/s/files/1/0569/7244/3738/files/SHOPIFY_Product_Image.jpg?v=448688474";
}, "__typename": MediaImage]>