2017-07-12 9 views
0

こんにちは私は私のappにshopify sdkを統合しました。今バージョンが変更されていますので、私のapi v2を私のAndroid app用にv3に移行する必要があります。ここでは、REST APIの代わりにGraphQL Conceptsを使用して、製品やコレクションの詳細を取得するWebサービス呼び出しを使用しています。Shopify Integration with android

ここに私のコード。

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コンセプトに新しいですので、どのように私は、商品画像のURL、価格やその他の詳細情報を得ることができますか?このGraphQLを使用して、製品やその他の詳細について何でも必要なデータを手に入れてください。

答えて

0

ここには解決策があります。

このコードでは、ドメインと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()); 
      } 
     }); 

は、それが動作ホープ:)