Shopify / mobile-buy-sdk-ios

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.
MIT License
453 stars 198 forks source link

Getting Product Metafield and Usage of [HasMetafieldsIdentifier!]! #1196

Closed ashleydee1999 closed 1 year ago

ashleydee1999 commented 1 year ago

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!