您找到解决方案了吗?即使使用您的代码Storefront.Product,我也没有得到产品详细信息?
@discardableResult
func fetchProduct(id: String, completion: @escaping (Storefront.Product?) -> 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.Product? {
completion (product)
}
else {
print("Failed to fetch Product: \(String(describing: error))")
completion (nil)
}
}
task.resume()
return task
}
//-
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()
.onlineStoreUrl()
}
}
}
}//end of final class
在这里,我正在调用我的函数
let id = GraphQL.ID(rawValue: "gid://shopify/Product/5375310233754")
Client.shared.fetchProduct(id: id.rawValue) {
Product1 in
guard let Product1 = Product1 else {
print("Failed to fetch Product")
return
}
我收到此错误。
Graph.QueryError:invalidQuery(原因:[MobileBuySDK.Graph.QueryError.Reason(消息:“无效的全局ID gid://shopify/Product/5375317082266
”,行:nil,列:nil)])
无法提取产品
答案 0 :(得分:0)
像这样对我来说这是有效的代码
func queryForProducts(in collection: Storefront.CollectionEdge?, limit: Int, after cursor: String? = nil) -> Storefront.QueryRootQuery {
let id = collection?.node.id.rawValue //?? UserDefaultsConfig.our_shop_id
return Storefront.buildQuery { $0
Collection { $0
.products(first: Int32(limit), after: cursor) { $0
.fragmentForStandardProduct()
}
.node(id: GraphQL.ID(rawValue: id)) { $0
.on
}
}
}
}
extension Storefront.ProductConnectionQuery {
@discardableResult
func fragmentForStandardProduct() -> Storefront.ProductConnectionQuery { return self
.pageInfo { $0
.hasNextPage()
}
.edges { $0
.cursor()
.node { $0
.id()
.title()
.tags()
.productType()
.descriptionHtml()
.createdAt()
.priceRange { $0
.maxVariantPrice { $0
.amount()
}
.minVariantPrice{ $0
.amount()
}
}
/*.metafields{ $0
.edges { $0
.node { $0
.id()
.key()
}
}
}*/
.availableForSale()
.variants(first: 10) { $0
.fragmentForVarient()
}
.images(first: 10){ $0
.edges { $0
.node{ $0
.id()
.originalSrc()
}
}
}
}
}
}
}
func query_graph(root_query: Storefront.QueryRootQuery ,completion:@escaping (_ result: Storefront.QueryRoot?, _ error: Graph.QueryError?) -> Void){
let task = client.queryGraphWith(root_query) { response, error in
if let response = response {
completion(response, nil)
} else {
completion(nil, error)
}
}
task.resume()
}
进行查询
let query = Model.shared.queryForProducts(in: self.obj_catagory, limit: 10, after: self.productsCursor)
通话查询功能
query_graph(root_query: query) { (response, error) in
//Model.shared.hideActivityIndicator(controller: self)
if error != nil {
//error
}
else {
let collection = response?.node as? Storefront.Collection
print(collection)
}
}