使用 Apollo 客户端购物 "Failed to parse http response"

Shopify with Apollo Client "Failed to parse http response"

我在产品查询时遇到解析问题。这是我的查询:

query getProducts($productCount: Int, $variantsCount: Int) {
products(first: $productCount) { edges { node { title variants(first: $variantsCount) { edges { node { title id image { originalSrc } } } } } } } }

如果我排除 image { originalSrc },此查询工作正常。所以图像 URL 解析导致了问题。其他相关信息:

ApolloClient

apolloClient = ApolloClient.builder()
                .serverUrl("https://" + SHOP_URL + "/api/graphql")
                .okHttpClient(okHttpClient)
                .defaultHttpCachePolicy(HttpCachePolicy.NETWORK_ONLY)
                .build();

StackTrace

Caused by: java.lang.IllegalArgumentException: Can't map GraphQL type: URL to: class java.lang.Object. Did you forget to add custom type adapter?
        at com.apollographql.apollo.response.ScalarTypeAdapters.adapterFor(ScalarTypeAdapters.java:29)
        at com.apollographql.apollo.internal.response.RealResponseReader.readCustomType(RealResponseReader.java:192)
        at com.XXX.XXXX.GetProductsQuery$Image$Mapper.map(GetProductsQuery.java:1027)
        at com.XXX.XXXX.GetProductsQuery$Node1$Mapper.read(GetProductsQuery.java:935)
        at com.XXX.XXXX.GetProductsQuery$Node1$Mapper.read(GetProductsQuery.java:932)
        at com.apollographql.apollo.internal.response.RealResponseReader.readObject(RealResponseReader.java:138)
        at com.XXX.XXXX.GetProductsQuery$Node1$Mapper.map(GetProductsQuery.java:932)
        at com.XXX.XXXX.GetProductsQuery$Edge1$Mapper.read(GetProductsQuery.java:803)
        at com.XXX.XXXX.GetProductsQuery$Edge1$Mapper.read(GetProductsQuery.java:800)
        at com.apollographql.apollo.internal.response.RealResponseReader.readObject(RealResponseReader.java:138)
        at .GetProductsQuery$Edge1$Mapper.map(GetProductsQuery.java:800)
        at com.XXX.XXXX.GetProductsQuery$Variants$Mapper.read(GetProductsQuery.java:704)
        at com.XXX.XXXX.GetProductsQuery$Variants$Mapper.read(GetProductsQuery.java:701)
        at com.apollographql.apollo.internal.response.RealResponseReader$ListItemReader.readObject(RealResponseReader.java:312)
        at com.XXX.XXXX.GetProductsQuery$Variants$Mapper.read(GetProductsQuery.java:701)
        at com.XXX.XXXX.GetProductsQuery$Variants$Mapper.read(GetProductsQuery.java:698)
        at com.apollographql.apollo.internal.response.RealResponseReader.readList(RealResponseReader.java:165)
        at com.XXX.XXXX.GetProductsQuery$Variants$Mapper.map(GetProductsQuery.java:698)
        at com.XXX.XXXX.GetProductsQuery$Node$Mapper.read(GetProductsQuery.java:597)
        at com.XXX.XXXX.GetProductsQuery$Node$Mapper.read(GetProductsQuery.java:594)
        at com.apollographql.apollo.internal.response.RealResponseReader.readObject(RealResponseReader.java:138)
        at com.XXX.XXXX.GetProductsQuery$Node$Mapper.map(GetProductsQuery.java:594)
        at com.XXX.XXXX.GetProductsQuery$Edge$Mapper.read(GetProductsQuery.java:478)
        at com.XXX.XXXX.GetProductsQuery$Edge$Mapper.read(GetProductsQuery.java:475)
        at com.apollographql.apollo.internal.response.RealResponseReader.readObject(RealResponseReader.java:138)
        at com.XXX.XXXX.GetProductsQuery$Edge$Mapper.map(GetProductsQuery.java:475)
        at com.XXX.XXXX.GetProductsQuery$Products$Mapper.read(GetProductsQuery.java:379)
        at com.XXX.XXXX.GetProductsQuery$Products$Mapper.read(GetProductsQuery.java:376)
        at com.apollographql.apollo.internal.response.RealResponseReader$ListItemReader.readObject(RealResponseReader.java:312)
        at com.XXX.XXXX.GetProductsQuery$Products$Mapper.read(GetProductsQuery.java:376)
        at com.XXX.XXXX.GetProductsQuery$Products$Mapper.re

谢谢。

在使用调用进行测试之前,您应该先验证您的调用。如果您使用提供的工具来构建查询并以这种方式测试它们,那么使用 GraphQL 会容易得多。然后,一旦您开始取回数据,将工作查询插入您的代码以获得结果。比像你这样猜测正确的调用结构要容易得多。

例如,此应用可能最有帮助:https://shopify-graphiql-app.shopifycloud.com/login