// Async code looks like sync code
val result = try {
val response = apolloClient.query(query).execute()
println(response.data)
} catch (e: Exception) {
e.printStackTrace()
}
// Rx adpaters are currently the only way to use the client from Java
// If you don't use/like Rx you're out of luck
Rx2Apollo.single(apolloClient.query(query)).subscribe(
response -> {
System.out.println(response.data);
},
e -> {
e.printStackTrace();
}
);
// We now have a callbacks based API
apolloClient.query(query).enqueue(new ApolloCallback<GetRandomQuery.Data>() {
@Override public void onResponse(@NotNull ApolloResponse<GetRandomQuery.Data> response) {
System.out.println(response.data);
}
@Override public void onFailure(@NotNull ApolloException e) {
e.printStackTrace();
}
});
// If you use/like Rx you can also still use the adapters like above
// Use of extensions
val cacheFactory = MemoryCacheFactory(cacheSize)
val apolloClient = ApolloClient.Builder()
.serverUrl("http://...")
.normalizedCache(cacheFactory)
.build()
// Use of a helper class
NormalizedCacheFactory cacheFactory = new MemoryCacheFactory(cacheSize);
ApolloClient.Builder apolloClientBuilder = new ApolloClient.Builder().serverUrl("http://...");
NormalizedCache.configureApolloClientBuilder(apolloClientBuilder, cacheFactory);
ApolloClient apolloClient = apolloClientBuilder.build();
// A method directly on the builder
NormalizedCacheFactory cacheFactory = new MemoryCacheFactory(cacheSize);
ApolloClient apolloClient = new ApolloClient.Builder()
.serverUrl("http://...")
.normalizedCache(cacheFactory)
.build();
// Use of extensions
val apolloClient = ApolloClient.Builder()
.serverUrl("http://...)
.httpCache(dir, cacheSize)
.build()
// Use of a helper class
ApolloClient.Builder apolloClientBuilder = new ApolloClient.Builder().serverUrl("http://...");
HttpCache.configureApolloClientBuilder(apolloClientBuilder, dir, cacheSize);
ApolloClient apolloClient = apolloClientBuilder.build();
// A method directly on the builder
ApolloClient apolloClient = new ApolloClient.Builder()
.serverUrl("http://...")
.httpCache(dir, cacheSize)
.build();
// Use of Flows
apolloClient.subscription(MySubscription())
.toFlow()
.collect {
println(it.data)
}
// Must use the Rx adapters
ApolloCall<MySubscription.Data> subscriptionCall = client.subscription(new MySubscription());
Flowable<ApolloResponse<MySubscription.Data>> subscriptionFlowable = Rx2Apollo.flowable(subscriptionCall);
subscriptionFlowable.subscribe(
response -> {
System.out.println(response.data);
},
e -> {
e.printStackTrace();
}
);
// Callbacks based
// You also can still use Rx if your project likes it
apolloClient.subscription(new MySubscription()).enqueue(
new ApolloCallback<MySubscription.Data>() {
@Override public void onResponse(@NotNull ApolloResponse<MySubscription.Data> response) {
System.out.println(response.data);
}
@Override public void onFailure(@NotNull ApolloException e) {
e.printStackTrace();
}
}
);
// Use of Flows
val interceptor = object: ApolloInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
return chain.proceed(request.newBuilder().addHttpHeader("header", "value").build())
}
}
apolloClient = ApolloClient.Builder()
.serverUrl("http://...")
.addInterceptor(interceptor)
.build()
// Manipulating Kotlin Flows from Java works for simple cases but is quite limitating
ApolloInterceptor interceptor = new ApolloInterceptor() {
@NotNull @Override public <D extends Operation.Data> Flow<ApolloResponse<D>> intercept(@NotNull ApolloRequest<D> request, @NotNull ApolloInterceptorChain chain) {
return chain.proceed(request.newBuilder().addHttpHeader("header", "value").build());
}
};
ApolloClient apolloClient = new ApolloClient.Builder()
.serverUrl("http://...")
.addInterceptor(interceptor)
.build();
// Callbacks based
ApolloInterceptor interceptor = new ApolloInterceptor() {
@Override public <D extends Operation.Data> void intercept(@NotNull ApolloRequest<D> request, @NotNull ApolloInterceptorChain chain, @NotNull ApolloCallback<D> callback) {
chain.proceed(request.newBuilder().addHttpHeader("header", "value").build(), callback);
}
};
ApolloClient apolloClient = new ApolloClient.Builder()
.serverUrl("http://...")
.addInterceptor(interceptor)
.build();