当我尝试使用下面给出的代码在Shopify android中通过id获取产品详细信息时
ID id = new ID(productId);
Storefront.QueryRootQuery query = Storefront.query(rootQueryBuilder ->
rootQueryBuilder
.node(id, nodeQuery ->
nodeQuery
.onProduct(productQuery ->
productQuery
.title()
.description()
)
)
);
QueryGraphCall call = App.graphClient().queryGraph(query);
call.enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
@Override
public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
}
@Override
public void onFailure(@NonNull GraphError error) {
}
});
}
但是我收到的错误就像Field'节'上的Argument'id'有一个无效的值。预期类型'ID!'。
我也试过了另一个代码但得到了同样的错误
Storefront.ProductQueryDefinition query = q -> q
.title()
.descriptionHtml()
.tags()
.images(args -> args.first(250), imageConnection -> imageConnection
.edges(imageEdge -> imageEdge
.node(Storefront.ImageQuery::src)
)
)
.options(option -> option
.name()
.values()
)
.variants(args -> args.first(250), variantConnection -> variantConnection
.edges(variantEdge -> variantEdge
.node(variant -> variant
.title()
.availableForSale()
.selectedOptions(selectedOption -> selectedOption
.name()
.value()
)
.price()
)
)
);
GraphCall<Storefront.QueryRoot> call = App.graphClient().queryGraph(Storefront.query(
root -> root
.node(new ID(productId), node -> node
.onProduct(query)
)
)
);
提前致谢。
字段'node'上的参数'id'具有无效值。预期类型'ID!'。
如果未提供正确的productID,则会发生此错误。试试这个 - 获取集合 - Storefront.QueryRootQuery query = Storefront.query(rootQuery -> rootQuery
.shop(shopQuery -> shopQuery
.collections(arg -> arg.first(30), collectionConnectionQuery -> collectionConnectionQuery
.edges(collectionEdgeQuery -> collectionEdgeQuery
.node(collectionQuery -> collectionQuery
.title()
.image(imageConnectionQuery -> imageConnectionQuery
.src()
)
)
)
)
)
);
现在使用集合ID来获取此集合的产品 -
Storefront.QueryRootQuery query = Storefront.query(rootQuery -> rootQuery
.node(new ID(collectionID), nodeQuery -> nodeQuery
.onCollection(collectionQuery -> collectionQuery
.products(
args -> args
.first(30),
productConnectionQuery -> productConnectionQuery
.pageInfo(pageInfoQuery -> pageInfoQuery
.hasNextPage()
)
.edges(productEdgeQuery -> productEdgeQuery
.cursor()
.node(productQuery -> productQuery
.title()
.productType()
.description()
.images(arg -> arg.first(10), imageConnectionQuery -> imageConnectionQuery
.edges(imageEdgeQuery -> imageEdgeQuery
.node(imageQuery -> imageQuery
.src()
)
)
)
.variants(arg -> arg.first(10), variantConnectionQuery -> variantConnectionQuery
.edges(variantEdgeQuery -> variantEdgeQuery
.node(productVariantQuery -> productVariantQuery
.price()
.compareAtPrice()
.title()
.available()
)
)
)
)
)
)
)
)
);
从响应中获取所有产品ID -
graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
@Override public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
progress.dismiss();
Storefront.Collection collection = (Storefront.Collection) response.data().getNode();
boolean hasNextProductPage = collection.getProducts().getPageInfo().getHasNextPage();
ArrayList products = new ArrayList<>();
String productID = new String[products.size()];
for (Storefront.ProductEdge productEdge : collection.getProducts().getEdges()) {
String productPageCursor = productEdge.getCursor();
products.add(productEdge.getNode());
}
for(int count=0;count<products.size();count++){
productID[count] = products.get(count).getId().toString();
}
@Override
public void onFailure(@NonNull GraphError error) {
}
});
在代码中使用这些产品ID。