Shopify 集成在安卓中



嗨,我已经将 shopify sdk 集成到我的应用程序中,现在版本已更改,所以我必须将我的 API v2 迁移到我的安卓应用程序的 v3 中。在这里,他们一直在使用 GraphQL 概念而不是用于 Web 服务调用的 REST API 来获取产品和集合详细信息。

这是我的代码。

graphClient = GraphClient.builder(getActivity())
.shopDomain(shopUrl)
.accessToken(shopifyAPIKey)
//   .httpClient(httpClient) // optional
.httpCache(new File(getActivity().getCacheDir(), "/http"), 10 * 1024 * 1024) // 10mb for http cache
.defaultHttpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(5, TimeUnit.MINUTES)) // cached response valid by default for 5 minutes
.build();

query = Storefront.query(rootQuery -> rootQuery
.shop(shopQuery -> shopQuery
.name()
.currencyCode()
.refundPolicy(refundPolicyQuery -> refundPolicyQuery
.title()
.url()
)
)
);

graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
@Override
public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
String name = response.data().getShop().getName();
System.out.println("Response of Shopify : " + response.data().toString());
System.out.println("Shop Name : " + name);

}
@Override
public void onFailure(@NonNull GraphError error) {
error.printStackTrace();
System.out.println("Shopify Error : " + error.getMessage());
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});

此代码用于查询以获取集合和产品

query = Storefront.query(rootQuery -> rootQuery
.shop(shopQuery -> shopQuery
.collections(collectionCount, collectionConnectionQuery -> collectionConnectionQuery
.edges(collectionEdgeQuery -> collectionEdgeQuery
.node(collectionQuery -> collectionQuery
.title()
.products(productCount, productConnectionQuery -> productConnectionQuery
.edges(productEdgeQuery -> productEdgeQuery
.node(productQuery -> productQuery
.title()
.productType()
.description()
.images(2, imageConnectionQuery -> imageConnectionQuery
      .edges(imageEdgeQuery -> imageEdgeQuery
              .node(imageQuery -> imageQuery
                      .src()
              )
      )
)
.variants(2, variantConnectionQuery -> variantConnectionQuery
      .edges(variantEdgeQuery -> variantEdgeQuery
              .node(productVariantQuery -> productVariantQuery
                      .price()
                      .title()
                      .available()

              )

      )
)
)
)
)
)
)
)));

在这里,我是 GraphQL 概念的新手,那么如何获取产品图片网址、价格和其他详细信息?请指导我使用此 GraphQL 获取有关产品和其他详细信息的任何数据

虽然这里晚了是解决方案。

在给定的代码中,您已经创建了一个图形客户端,提供了您的域和 API 密钥。接下来,您刚刚提到了通过查询需要的所有数据。

现在需要调用 Shopify 终端节点,以便您可以获取所需的数据。

QueryGraphCall call_products = graphClient.queryGraph(query);
call_products.enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
@Override
public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
List<Storefront.ProductEdge> list_edge = response.data().getShop().getProducts().getEdges(); //List of products that you have in your store.
for(int i=0;i<list_edge.size();i++){
Log.d(TAG,"product title: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getTitle());
Log.d(TAG,"product id: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getId());
Log.d(TAG,"product description: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getDescription());
}
}
@Override
public void onFailure(@NonNull GraphError error) {
Log.e(TAG, "onFailure: " + error.toString());
}
});

希望它能:)

最新更新