Getting Product Metafield and Usage of [HasMetafieldsIdentifier!]! (iOS SDK and GraphQL related)

Topic summary

A developer is attempting to retrieve product metafields using Shopify’s iOS SDK and GraphQL but encountering implementation challenges.

Current Approach:

  • Using the metafield(namespace: "custom", key: "myKey") method in their product fragment query
  • Attempting to assign an alias to access the metafield value in their ProductViewModel
  • Getting a fatal error: “field metafield__myAlias wasn’t queried”

Key Issue:
The aliased metafield approach isn’t working when creating the Product object. The developer tried assigning the key as the alias but was unsuccessful.

Alternative Mentioned:
The developer notes that documentation suggests a more efficient method using [HasMetafieldsIdentifier!]! to retrieve all metafields at once, but they haven’t been able to implement this approach successfully.

Working Reference:
They have a working GraphQL query using nodes(ids: [ID!]!) that successfully retrieves metafields, suggesting the issue is specific to the iOS SDK implementation pattern.

The discussion remains open with the developer seeking guidance on proper metafield retrieval implementation.

Summarized with AI on November 23. AI used: claude-sonnet-4-5-20250929.

Hi,

I’m trying to get my product metafields using the following code:

func fragmentForStandardProduct() -> Storefront.ProductConnectionQuery { return self
.pageInfo { $0
.hasNextPage()
}
.edges { $0
.cursor()
.node { $0
.id()
.title()
.handle()
.descriptionHtml()
.description()
.variants(first: 250) { $0
.fragmentForStandardVariant()
}
.images(first: 250){ $0
.fragmentForStandardProductImage()
}
.metafield(namespace: "custom", key: "myKey") { $0 // Attemping to use this method
.value(alias: "myAlias")
}
}
}
}

Creating my Product Object

final class ProductViewModel: ProductModel {

typealias ModelType = Storefront.ProductEdge

let model: ModelType
let cursor: String

let id: String
let title: String
let handle: String
let summary: String
let unformattedSummary: String
let price: String
let images: PageableArray<ImageViewModel>
let variants: PageableArray<VarientViewModel>
let metaField1: String

// ----------------------------------
// MARK: - Init -
//
required init(from model: ModelType) {
self.model = model
self.cursor = model.cursor

let variants = model.node.variants.edges.viewModels.sorted {
$0.price < $1.price
}

let lowestPrice = variants.first?.price

self.id = model.node.id.rawValue
self.title = model.node.title
.replacingOccurrences(of: "-", with: " ")
.capitalized
self.handle = model.node.handle
self.summary = model.node.descriptionHtml
self.unformattedSummary = model.node.description
self.price = lowestPrice == nil ? "No price" : Currency.stringFrom(lowestPrice!)

self.images = PageableArray(
with: model.node.images.edges,
pageInfo: model.node.images.pageInfo
)

self.variants = PageableArray(
with: model.node.variants.edges,
pageInfo: model.node.variants.pageInfo
)
self.metaField1 = model.node.aliasedMetafield(alias: "myAlias")?.value ?? "" // Attemping to get that metafield I assigned an alias. I tried assign the key as the alias, however it didn't work
}
}

Error Message from xcode

Buy/GraphQL.swift:193: Fatal error: field metafield__myAlias wasn't queried
2023-02-05 22:35:40.172892+0200 travSIM[48928:4746891] Buy/GraphQL.swift:193: Fatal error: field metafield__myAlias wasn't queried

Working GraphQL query

query nodes($ids: [ID!]!) {
nodes(ids: $ids) {
... on Collection {
id
products(first: 50) {
edges {
node {
id
title
metafield(namespace: "custom", key: "myKey") {
value
}
}
}
}
}
}
}

I’m learning from the documentation that there’s a more efficient manner of getting all my metafields through the use of [HasMetafieldsIdentifier!]!, unfortunetly I’m failing to find a way of using it.

Any guidance would be apprecited, thanx!