提问者:小点点

Shopify集成在Android


您好,我已经将shopify sdk集成到我的应用程序中,现在版本已经更改,所以我必须为我的android应用程序将api v2迁移到v3。在这里,他们使用GraphQL概念代替restapi,使用for-webservice调用来获取产品和集合的详细信息。

这是我的密码。

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获取任何关于产品和其他细节的数据


共1个答案

匿名用户

虽然晚了是解决办法。

在给定的代码中,您创建了一个图形客户端,提供了域和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());
            }
        });

希望它工作:)