使用GraphQL从Shopify向ios应用程序查询数据。Xcode-SWIFT
我正在尝试使用GraphQL从Shopify接收数据。我可以通过提供产品ID并在Storefront.product表单中接收数据来获取数据,但是我需要它位于Storefront.ProductEdge表单中 这是我的密码:使用GraphQL从Shopify向ios应用程序查询数据。Xcode-SWIFT,ios,swift,xcode,graphql,shopify,Ios,Swift,Xcode,Graphql,Shopify,我正在尝试使用GraphQL从Shopify接收数据。我可以通过提供产品ID并在Storefront.product表单中接收数据来获取数据,但是我需要它位于Storefront.ProductEdge表单中 这是我的密码: final class ClientQuery { static func queryForProduct(id: String) -> Storefront.QueryRootQuery { let id = GraphQL.ID(rawValue:
final class ClientQuery {
static func queryForProduct(id: String) -> Storefront.QueryRootQuery {
let id = GraphQL.ID(rawValue: id)
return Storefront.buildQuery { $0
.node(id: id) { $0
.onProduct() { $0
.id()
.title()
.variants(first: 1){ $0
.edges() { $0
.node(){ $0
.availableForSale()
.priceV2(){ $0
.amount()
}
}
}
}
.description()
.images( first: 1){ $0
.fragmentForStandardProductImage()
}
}
}
}
}//end of final class
这段代码有效,我能够获得产品的某些值,比如标题和图片。
但是,我希望以类型Storefront.ProductEdge
而不是Storefront.Product
获取数据,以便更好地显示数据。
但当我将fetchProduct
更改为:
@discardableResult
func fetchProduct(id: String, completion: @escaping (Storefront.ProductEdge?) -> Void) -> Task {
let query = ClientQuery.queryForProduct(id: id)
let task = self.client.queryGraphWith(query) { (query, error) in
error.debugPrint()
if let product = query?.node as? Storefront.ProductEdge? {
completion (product)
}
else {
print("Failed to fetch Product: \(String(describing: error))")
completion (nil)
}
}
task.resume()
return task
}
然后我得到“无法获取产品:nil”
和“无法获取产品”
发生了什么事?如何将产品数据称为Storefront.ProductEdge类型
任何意见都将不胜感激
干杯
viewDidLoad(){
Client.shared.fetchProduct(id: sku) { Product1 in
guard let Product1 = Product1 else {
print("Failed to fetch Product")
return
}
}
@discardableResult
func fetchProduct(id: String, completion: @escaping (Storefront.ProductEdge?) -> Void) -> Task {
let query = ClientQuery.queryForProduct(id: id)
let task = self.client.queryGraphWith(query) { (query, error) in
error.debugPrint()
if let product = query?.node as? Storefront.ProductEdge? {
completion (product)
}
else {
print("Failed to fetch Product: \(String(describing: error))")
completion (nil)
}
}
task.resume()
return task
}