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.