Search code examples
javaspring-bootresttemplategraphql-java

GraphQL api consuming with spring boot Resttemplate resulting in {"errors":[{"message":"No query string was present"}]} always


Currently we want to consume a graphQL endpoint in a springboot application using resttemplate

However, when we make a POST request with the below query we are always receiving the same error {"errors":[{"message":"No query string was present"}]}

Below is the snippet, we want to run,

    @Test
    public void testSwoop(){

        RestTemplate restTemplate = restTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer *************");
        headers.add("content-type", "application/graphql");

        String query1 = "{\n" +
                "  \"query\": query {\n" +
                "    \"locationTypes\": {\n" +
                "      \"edges\": \n" +
                "        {\n" +
                "          \"node\": \n" +
                "        {\n" +
                "          \"name\"\n" +
                "        }\n" +
                "        }\n" +
                "    }\n" +
                "  }\n" +
                "}";

        String URL = "https://staging.joinswoop.com/graphql";

        ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
      System.out.println("The response================="+response);
    }

However from Postman, we dont have any issue in consuming the endpoint, and we get response just fine postman output

Can someone please help us in directing us to the correct resource


Solution

  • you set the content type header to "application/graphql", but yo are sending a JSON as data. Two solutions that might work:

    Sending JSON:

    Set the content type to "application/json" and send a JSON formatted query:

    @Test
    public void testSwoop(){
    
        RestTemplate restTemplate = restTemplate();
    
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer *************");
        headers.add("content-type", "application/json"); // just modified graphql into json
    
        String query1 = "{\n" +
                "  \"query\": query {\n" +
                "    \"locationTypes\": {\n" +
                "      \"edges\": \n" +
                "        {\n" +
                "          \"node\": \n" +
                "        {\n" +
                "          \"name\"\n" +
                "        }\n" +
                "        }\n" +
                "    }\n" +
                "  }\n" +
                "}";
    
        String URL = "https://staging.joinswoop.com/graphql";
    
        ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
      System.out.println("The response================="+response);
    }
    

    Sending GraphQL Query:

    If your server support this (it should), set the content type to "application/graphql", and send a real graphql query as a string.

    @Test
    public void testSwoop(){
    
        RestTemplate restTemplate = restTemplate();
    
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer *************");
        headers.add("content-type", "application/graphql"); // maintain graphql
    
        // query is a grapql query wrapped into a String
        String query1 = "{\n" +
                "    locationTypes: {\n" +
                "      edges: \n" +
                "        {\n" +
                "          node: \n" +
                "        {\n" +
                "          name\n" +
                "        }\n" +
                "        }\n" +
                "    }\n" +
                "  }";
    
        String URL = "https://staging.joinswoop.com/graphql";
    
        ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
      System.out.println("The response================="+response);
    }