当前位置: 首页 > 知识库问答 >
问题:

GraphQL api使用spring boot Resttemplate,导致{“errors”:[{“message”:“不存在查询字符串”}]}

潘泳
2023-03-14

目前,我们希望在springboot应用程序中使用rest模板使用图形QLendpoint

然而,当我们用下面的查询发出POST请求时,我们总是收到相同的错误{“errors”:[{“message”:“没有查询字符串”}]}

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

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

有人能帮我们找到正确的资源吗

共有1个答案

巫懿轩
2023-03-14

您将内容类型头设置为“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查询:

如果您的服务器支持(应该支持),请将内容类型设置为“应用程序/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);
}
 类似资料:
  • 方法参数PeekMessageRequest queueName : String : required peekMessageArg : PeekMessageArg : required 查询消息输入的参数,提供三种查询参数模式,但是一次仅能选择一种. 三种查询参数模式是: receiptHandle : String 按消息发送成功时返回的消息id, 或者接收到消息时获取的消息句柄, 来查询

  • 我有以下命名查询: 当我尝试按如下方式执行时: 然后我得到以下例外: JAVAlang.IllegalArgumentException:您试图使用查询字符串中不存在的字符串名称从Userdetails u中选择u,其中u.username=:username来设置参数值。 这是怎么造成的,我该如何解决?

  • 问题内容: 我安装了16gb内存的Elasticsearch。我开始使用聚合,但是在尝试发出以下查询时遇到“ java.lang.OutOfMemoryError:Java堆空间”错误: query_string本身仅返回1266次匹配,因此OOM错误让我有些困惑。 我是否正确使用了聚合?如果没有,我该怎么做才能解决此问题?谢谢! 问题答案: 您正在将整个-,-和- 字段加载到内存中以进行汇总。这

  • 问题内容: 我尝试在提交之前对该字符串进行。 问题答案: 你需要将参数传递为映射或2元组序列,例如: Python 3或以上 采用: 请注意,这在通常意义上不会进行url编码(请看输出)。为此使用。

  • 问题内容: 我经常使用语法,但是现在ANY运算符不起作用。这是我第一次使用它来比较标量和CTE返回的整数,但是我认为这不会引起问题。 我的查询: 当我运行它时,它引发以下错误: 错误:运算符不存在:integer = integer []:WITH bar AS(SELECT array_agg(b)AS bs FROM foo WHERE c <3)SELECT a FROM foo WHERE