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 Clienthttps://www.apollographql.com/docs/kotlin/v2/essentials/get-started-java 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!!

Hi!

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.

@Benoit_Lubek @mbonnin : Another quick query i have is that the Asynchronous operations in GraphQL Java Client is supported only by callbacks or do we have the support/future plans for supporting Asynchronous operations using Promises as well?

Thank You!!

Hi! For now the only supported API is coroutines, and adapters for Rx. There are also ways to bridge coroutines with Java’s Future for instance (see here), so you may want to experiment with it.

We also intend to have a better Java interop in the future, and you can monitor this ticket to be informed about it and give feedback.

Hey, does the below solution of RxJava worked for you?

@sophia987 : Yes it did, and its fairly straight forward.

1 Like

@mbonnin : I have another query related to the responses from the Query Execution, In the examples at https://graphql.org/learn/schema/ generally the responses of a GraphQL Query Operation is a json but the responses that we get from Apollo Android Client is not a Json String. We can use the Object Oriented notation to fetch the information of appropriate fields but is there a way to get this response as proper Json Format.

Thank You in Advance!!.

is there a way to get this response as proper Json Format.

Not really. The big value proposition of Apollo Kotlin is typesafe models. You lose that if you’re only using Json. If you want access to the raw Json, there are certainly ways to do it with Apollo Kotlin but that’d be using the wrong tool for the Job. Something like OkHttp and sending your requests manually would do the job equally well.

Thank You @mbonnin for the quick response.