Apollo GraphQL Client in java

Hey there,
im right now working on my Bachelorthesis which includes building some microservices to make a comparison between GraphQL API’s and RESTFUL API’s.
At this time I have some GraphQL-Servers which are working like a charm. My problem consists in making Graphql-Clients in Java.

I used the plugin to create java-querie-objects from graphql-queries and my schema.
Executing queries is working somewhat okayish.My main probelm consists in me notknowing how to execute mutations. In the kotlin doc from Apollo are some examples but neither v1 or v2 are working for me cause of some dependencies not resolving.

this is right now how i handle queries: (i dont know how to handly asynchoneus behaviour in java, so i fill a List in “on Response” and set java to wait for 0.5 seconds for the query to execute)


   ApolloClient apolloClient = ApolloClient.builder()
                    .serverUrl("http://localhost:8087/graphql")
                    .build();

         List<ShopItem> shopItems = new LinkedList<ShopItem>();


            apolloClient.query(new ArticlesQuery())
                    .enqueue(new ApolloCall.Callback<ArticlesQuery.Data>() {
                        @Override
                        public void onResponse(@NotNull Response<ArticlesQuery.Data> response) {

                           for (ArticlesQuery.Article article:response.getData().articles()) {

                            ShopItem shopItem = new ShopItem();
                            shopItem.setArticleName(article.articleName());
                            shopItems.add(shopItem);
                        }

                        }

                        @Override
                        public void onFailure(@NotNull ApolloException e) {
                            e.printStackTrace();
                        }
                    });

try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

I had hope that its possible to execute queries and mutations in java somewhat like in the following (pseudocode):

client = ApolloClient.builder().serverUrl(url).build();
response = apolloClient.query(new QueryObject()).execute();

These are the maven dependencies im using right now, but im felxible to make some changes.


        <dependency>
            <groupId>com.apollographql.apollo</groupId>
            <artifactId>apollo-runtime</artifactId>
            <version>2.5.11</version>
        </dependency>

            <plugin>
                <groupId>com.github.aoudiamoncef</groupId>
                <artifactId>apollo-client-maven-plugin</artifactId>
                <version>4.0.5</version>
            </plugin>

Maybe you guys can tell me if im simply using the wrong versions of if im trying to do something impossible (i dont know what is even possible in java with apollo). I hope you guys have some advice, sources or even kind words for me. Im kind of desperate and definitely bit more than i can chew with this project.

Hi :wave:. Mutations can be executed like queries by enqueuing a callback. The difference is that you use mutate instead of query (doc here):

apolloClient
    .mutate(upvotePostMutation)
    .enqueue(
        new ApolloCallback<>(new ApolloCall.Callback<UpvotePost.Data>() {
          @Override public void onResponse(@NotNull Response<UpvotePost.Data> response) {
              Log.i(TAG, response.toString());
          }
        
          @Override public void onFailure(@NotNull ApolloException e) {
              Log.e(TAG, e.getMessage(), e);
          }
        });
    );

About this part:

response = apolloClient.query(new QueryObject()).execute();

This is the way to execute a query in v3. This uses Kotlin coroutines to handle asynchronicity behind the hood but it’s not available in Java. The closest you can do is use a reactive framework like RxJava for an example.

Let me know how that goes!

Hey,
wow i thought i did try to solve my problem that way but apparently i didnt :wink:
Tried a few combinations with client.mutate and the enqueue-call but in hindside i cant figure out what i was doing wrong. Thank you a lot you really helped me out.

Do you know an easy way to store the response in a kind of synchronous way?
Unfortunaltely i dont have time to figure out asynchrous behavement and handling in java.
My solution with setting the method to sleep for x time works for me but is far from being perfect.

To get the response in a blocking way, I’d recommend using RxJava blockingGet. You can read more about how to integrate with RxJava here

@eike Thanks for raising this concern. I was curious, what does your application do exactly? Are you using Java for Mobile Android development, or is this something else all together? Any additional information would help us tremendously to better understand your use case here. Thank you!