Issue
I am working on a project and trying to use the GraphQL backend server in my SpringBoot application. I have written a client which connects to graphQL backend and creates/fetches the data but when I am trying to pass the variable from the application and pass with a query then it's throwing an error. But it's working fine if I just comment the variable part and hardcode the value directly in query.
Mutation Query- mutation($names: String!) { createList (name: $names){ updated listId name } }
Variable - { "names" : "bagName" }
Java Client -
'''
public CreateListDto getFavouritesDetails(String shoppingListName, String customerId, String storeId, String langId) throws IOException {
GraphqlRequestBody graphQLRequestBody = new GraphqlRequestBody();
System.out.println("print this"+shoppingListName+"custId"+customerId);
final String query = GraphqlSchemaReaderUtil.getSchemaFromFileName("createList");
String variables = GraphqlSchemaReaderUtil.getSchemaFromFileName("variables");
graphQLRequestBody.setVariables(variables.replace("bagName", "Avisss"));
graphQLRequestBody.setQuery(query);
//System.out.println("graphQLRequestBody----------"+graphQLRequestBody.getQuery());
System.out.println(graphQLRequestBody.getVariables());
CreateListDto webClient = WebClient.builder().defaultHeaders(httpHeaders -> {
httpHeaders.set("X-User-Id", "XXXXXXXXXXX");
httpHeaders.set("X-Retail-Id",storeId );
httpHeaders.set("X-Is-Anonymous", Boolean.toString(true));
}).build()
.post()
.uri(url)
.bodyValue(graphQLRequestBody)
.retrieve()
.bodyToFlux(CreateListDto.class)
.blockFirst();
System.out.println(graphQLRequestBody.getQuery());
//System.out.println(graphQLRequestBody.getVariables());
return webClient;
'''
Maven dependencies --
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Getting below error in console together with printing the request System outs -
GraphQL Request Body ::: - mutation{ createList(name: "sl114") { updated listId name } } GraphQL Request variable ::: - { "name" : "Avisss" }
2022-01-23 16:29:40.697 ERROR 3748 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/shoppingbag] threw exception [Request processing failed; nested exception is org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from POST https://dev.test.com/graphql] with root cause
org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from POST https://dev.test.com/graphql at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:196) ~[spring-webflux-5.3.14.jar:5.3.14] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ 400 from POST https://dev.test.com/graphql [DefaultWebClient] Original Stack Trace: at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:196) ~[spring-webflux-5.3.14.jar:5.3.14] at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:207) ~[spring-webflux-5.3.14.jar:5.3.14] at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106) ~[reactor-core-3.4.13.jar:3.4.13] at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.13.jar:3.4.13] at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:101) ~[reactor-core-3.4.13.jar:3.4.13]>
Solution
It got resolved. There were two issues - 1) with the graphQL schema ad 2) with the WebClient.
I corrected the schema and using the HttpClient instead of WebClient.
Answered By - java tech
Answer Checked By - Marilyn (JavaFixing Volunteer)