I am using java 17, spring-boot: 3.0.5 and maven in my project. I am calling a GraphQL endpoint using HttpGraphQlClient. Below is my expected code snippet:
Mono<Entity> entity = this.graphQlClient.documentName("getDetails").variables(Map.of("xx", "xx"))
.retrieve("data")
.toEntity(Entity.class)
.onErrorResume(FieldAccessException.class, ex -> {
ClientGraphQlResponse response = ex.getResponse();
// ...
ClientResponseField field = ex.getField();
// ...
});
However, I am getting syntax error at onErrorResume(....)
. Below is the error I am getting:
Cannot resolve method 'onErrorResume(Class, )'
Below are the suggestions I get:
Candidates for method call this.graphQlClient.documentName("getDetails").variables(Map.of("xx", "xx")) .retrieve("data") .toEntity(Entity.class).onErrorResume(FieldAccessException.class, ex -> { }) are:
Spring documention has the same code block given as example: Spring-graphql-retrieve-method
I would like to know how to fix this error? How to handle error using retrieve method ?
I want to handle error thrown from graphQL endpoint in an effective manner. Eventhough, we can use .execute() method to have more control on the GraphQL response, I wanted to use .retrieve() since it is provided in the library.
The onErrorResume
operator means that you will handle errors of a certain type and provide a fallback (see the Javadoc here).
In your case you should return a fallback value of type Entity
.
Mono<Entity> entity = this.graphQlClient.documentName("getDetails")
.variables(Map.of("xx", "xx"))
.retrieve("data")
.toEntity(Entity.class)
.onErrorResume(FieldAccessException.class, ex -> {
return Mono.just(new Entity("defaultValue"));
});
You are getting a compiler error because the compiler expects onErrorResume
to return a type Mono<Entity>
.