2
私はapollo-GraphQLをAPIレスポンスに使用しています。インターセプタクラスにトークンを設定しました。主なアクティビティでは、データを取得するためにクエリを渡しているapolloClientを使用しています。android graphQL apollo
インターセプタクラスからAPIレスポンスを取得しましたが、クエリレスポンスを取得できませんでした。 Applicationクラスの
public class HttpInterceptor implements Interceptor {
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("okhttp");
String BASE_URL = "url_example.json";
Response response = null;
Request request = chain.request();
request =chain.request();
request = request.newBuilder()
.url(BASE_URL)
.header("token_key", "token_value")
.build();
long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
response = chain.proceed(request);
long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1)/1e6d, response.headers()));
return response;
}
コード:
private static final String BASE_URL = "url_example.json";
private ApolloClient apolloClient;
@Override public void onCreate() {
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpInterceptor())
.build();
apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
}
public ApolloClient apolloClient() {
return apolloClient;
}
コードまたはMainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
application = (SampleApplication) getApplication();
content = (ViewGroup) findViewById(R.id.rl_content_holder);
progressBar = (ProgressBar) findViewById(R.id.loading_bar);
RecyclerView collectionRecyclerView = (RecyclerView) findViewById(R.id.rv_collection_list);
mCollectionAdapter = new CollectionAdapter(this);
collectionRecyclerView.setAdapter(mCollectionAdapter);
collectionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
fetchCollection();
}
ApolloCall.Callback<ShopifyCollections.Data> mDataCallback =
new ApolloCall.Callback<ShopifyCollections.Data>() {
@Override
public void onResponse(@Nonnull com.apollographql.apollo.api.Response<ShopifyCollections.Data> response) {
Log.i("Shopify CallBack",response.toString());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Log.e(TAG, e.getMessage(), e);
}
};
public void fetchCollection(){
mHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpInterceptor())
.build();
ShopifyCollections collectionQuery = ShopifyCollections.builder()
.limit(250)
.build();
collectionCall = application.apolloClient().newCall(collectionQuery);
collectionCall.enqueue(mDataCallback);
}
問合せ:
query ShopifyCollections($limit: Int!){
shop{
name
collections(first: $limit)
{
edges{
node{
title
id
image{
src
}
}
}
}
}
}