Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GraphQL从Shopify向ios应用程序查询数据。Xcode-SWIFT_Ios_Swift_Xcode_Graphql_Shopify - Fatal编程技术网

使用GraphQL从Shopify向ios应用程序查询数据。Xcode-SWIFT

使用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:

我正在尝试使用GraphQL从Shopify接收数据。我可以通过提供产品ID并在Storefront.product表单中接收数据来获取数据,但是我需要它位于Storefront.ProductEdge表单中

这是我的密码:

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
}