Synchronous and Asynchronous Version of GraphQL Client in Java

Hello Team,
Its regarding Synchronous and Asynchronous Responses for Apollo Client in Java. While i was exploring the Kotlin Client initially but as per the suggestions on the forum Android V2 version is more close for Java Functionality.

I tried the basic operations and really liked the way things work, however most of the Functionality and API Calls exposed in V2 Client are asynchronous.

As a part of evaluation and at-times users require operations to be blocking and hence, i need to know if there is a way to get the responses in Synchronous way,

If yes please do let me know how do we get to work with making the Operations Synchronous in Java.

If not then what are all the alternatives?

Thank You in Advance!!


Not sure if you are working on an Android project, but if so, blocking the main thread or executing any sort of disk or network operation on it is a bad practice which is why an asynchronous API would make more sense here.

If you really need to, you could make your own wrapper to transform the callback-based API into a blocking one, for instance by using a CountDownLatch.

Hi @Benoit_Lubek , No i am not working on an Android Project, I have an internal application in Java which will be using it to query GraphQL Server. The other perspective is i think using Kotlin Client its possible in V3, was thinking if there is a way to leverage the functionality or if it got introduced in V3 only.

When I’ve bumped into this, I’ve gone with RxJava blockingFirst. The RxJava doc for Apollo is here. All in all, it’ll look like this:

// Create a query object
EpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();

// Create an ApolloCall object
ApolloCall<EpisodeHeroName.Data> apolloCall = apolloClient.query(query);

// RxJava2 Observable
Observable<Response<EpisodeHeroName.Data>> observable2 = Rx2Apollo.from(apolloCall);

// block to get the first response
Response<EpisodeHeroName.Data> response = observable2.blockingFirst()

I used RxJava2 above because this is what I’m used to but I’m pretty sure RxJava3 is similar.