javaspring-bootgraphqlresttemplatespring-resttemplate

Call to GraphQL Query with RestTemplate is giving either 400 or invalid syntax message


I have been experiencing a weird issue trying to call a GraphQL query using RestTemplate from the SpringBoot framework. I say it is weird because it is only happening with this particular query.

This is the code I am using (because that is the standard being used at the company):

var queryString = String.format("{ \"query\": \"{ parameterByName(name:%s) { name, value }}\" }", "Limit50-120");
var httpHeaders = new HttpHeaders(...Here I set all the required headers...);
var endpoint = "http://myservice.com/graphql";
var endpointAsUri = new URI(endpoint);

ResponseEntity<ParametersResponse> parametersResponseEntity =
                this.restTemplate.postForEntity(serviceUri, new HttpEntity<>(queryString, httpHeaders), ParametersResponse.class);

The response entity holds data as null and in GraphQL errors I am getting the following message:

Invalid Syntax : token recognition error at: '-120R' at line 1 column 34

Seems like it is considering that I am not sending a String (although in other services we have similar implementation and there are no issues), that's why I tried changing the query string to use double quotes:

var queryString = String.format("{ \"query\": \"{ parameterByName(name:%s) { name, value }}\" }", "\"Limit50-120\"");

Then I get:

400 response: no body.

It is weird because it is only happening with this particular call, as mentioned, we have many services that are working with this same approach and we are not having any issues.

Not sure if I am missing something. By the way, I am using the following dependencies (and I am limited to make this call using these dependencies, I cannot switch to another library or a more modern version of GraphQL):

    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>5.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-tools</artifactId>
        <version>5.2.4</version>
    </dependency>

Any clue or help will be highly appreciated. Thanks in advance


Solution

  • I managed to find a solution, not sure if this is the best way to achieve it, but it solved my problem, if I come up with a more efficient solution, I will post it here for sure.

    What I did so far was to create a parameterByName.graphql file in my resources folder with the query spec:

    query ($name: String) {
        parameterByName(name: $name) {
            name
            value
        }
    }
    

    Then, when I call the service I did the following:

    Create a GraphQLRequest custom POJO:

    @AllArgsConstructor
    @Data
    public class GraphQLRequest {
        private final String query;
        private Map<String, Object> variables = new HashMap<>();
    }
    

    Then, I create the request through RestTemplate using the following code:

    Map<String, Object> variables = new HashMap<>();
    variables.put("name", "Limit50-120");
    var request = new GraphQLRequest(query, variables);
    ResponseEntity<String> response = restTemplate.postForEntity(endpointUrl, new HttpEntity<>(request, headers), String.class);
    

    And now I am able to get the response without issues. Not sure why GraphQL does not recognized it as a valid value before, but with this, it works.