GraphQL api 使用 spring 引导 Resttemplate 导致 {"errors":[{"message":"No query string was present"}]} 总是

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

目前我们想使用 resttemplate 在 springboot 应用程序中使用 graphQL 端点

但是,当我们使用以下查询发出 POST 请求时,我们总是收到相同的错误 {"errors":[{"message":"No query string was present"}] }

下面是代码片段,我们要运行,

    @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);
    }

但是从 Postman 那里,我们在使用端点时没有任何问题,我们得到的响应也很好

谁能帮我们找到正确的资源

您将内容类型 header 设置为 "application/graphql",但您发送的是 JSON 作为数据。 两个可能有效的解决方案:

发送JSON:

将内容类型设置为 "application/json" 并发送 JSON 格式的查询:

@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);
}

正在发送 GraphQL 查询:

如果您的服务器支持(应该),请将内容类型设置为 "application/graphql",然后将真正的 graphql 查询作为字符串发送。

@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);
}