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

Accessing the MediaImage from a Metafield #1197

Open ashleydee1999 opened 1 year ago

ashleydee1999 commented 1 year ago

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]>
lmartinresnick commented 1 year ago

I'm having the exact same issue and trying to access the image url the same way. Have you made any progress on this @ashleydee1999?

ashleydee1999 commented 1 year ago

Hi @lmartinresnick, I never pursued it further due to no responses to my query. I'll reattempt it sometime this week and see how far I get

lmartinresnick commented 1 year ago

@ashleydee1999 I decided to use the first product's image for the entire collection for the meantime

ashleydee1999 commented 1 year ago

@lmartinresnick yeah, that's my current implementation as well. This has been the one of the most difficult SDK's I've worked with, but hopefully they'll be a breakthrough on this particular issue

SimonMaze commented 12 months ago

@ashleydee1999 You can type cast the reference using the correct Storefront type (you need to make sure to use the correct type, otherwise casting to an incorrect type will return a runtime exception.)

let metafieldURL = (filteredArr[0]?.reference as? Storefront.MediaImage).image?.url

Just make sure to safely unwrap your optionals and provide a default value in case of nil