【问题标题】:Graphql send POST with variables in Postman as textGraphql 在 Postman 中以文本形式发送带有变量的 POST
【发布时间】:2019-06-14 16:31:58
【问题描述】:

我有一个端点,当它是我的请求时:

query {
    getItem(dictionaryType: "test1") {
        code
        name
        description
    }
}

它工作正常,请参阅:

我想测试变量 - 所以我想把它改成这样:

query {
    getItem($dictionaryType: String) {
        code
        name
        description
    }
}
variables {
    dictionaryType: "test1"
}

我不想使用除邮递员以外的任何其他工具,或者我宁愿不使用文本以外的其他格式。 执行第二个输出时出现以下错误:

"errors": [
    {
        "message": "Invalid Syntax",
        "locations": [
            {
                "line": 2,
                "column": 9,
                "sourceName": null
            }
        ],

如何修正请求的语法?

编辑: 我什至对使用如下语法的请求有疑问:https://stackoverflow.com/a/50043390/4983983 query { getDataTypes } 将其翻译成 json 例如: {"query": "{getDataTypes}"} 不起作用并给出 JSON 解析错误: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token\n at [Source: (PushbackInputStream 错误。

见:

目前Posts 端点的code 看起来像:

@PostMapping("graphql")
public ResponseEntity<Object> getResource(@RequestBody String query) {
    ExecutionResult result = graphQL.execute(query);
    return new ResponseEntity<Object>(result, HttpStatus.OK);
}

如果我将其更改为:

@PostMapping("graphql")
public ResponseEntity<Object> getResource(@RequestBody Object query) { // String query
    ExecutionResult result;
    if (query instanceof String) {
        result = graphQL.execute(query.toString());
    } else{
        Map b = (HashMap) query;
        result = graphQL.execute(b.get("query").toString());
    }
    return new ResponseEntity<Object>(result, HttpStatus.OK);
}

现在似乎只有json 版本有效。因为当我使用文本时,我得到:

"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/graphql"

还有其他配置选项吗?不知道最后一个例子中variables会不会处理好。

【问题讨论】:

  • 你需要发送一个正确的 JSON 请求,比如{"query": "...", "variables": "..."}
  • 变量替换的语法是不是类似于{{dictionaryType}}
  • 为什么不发短信?对于其他请求,它可以工作。
  • 请注意,我给出的示例 - 查询中的 ... 将包含实际查询。

标签: java graphql postman graphql-java


【解决方案1】:

您需要将 postman 版本更新到 7.2。 Postman 现在支持 graphql 请求。

https://blog.getpostman.com/2019/06/18/postman-v7-2-supports-graphql/

参考下图。

【讨论】:

  • 酷 - 当我写这篇文章时它不存在。
【解决方案2】:

您可以尝试类似下面的示例

{ "查询":"查询采访($id:ID!){ 采访(interviewGuid: $id) { 面试指导面试代码面试职称面试OwnerGuid interviewStartDate interviewEndDate}}", "变量":{"id":"725000ae-ba39-45a5-a10f-79e5b27fe747"} }

【讨论】:

    猜你喜欢
    • 2015-04-11
    • 2016-08-25
    • 2021-10-21
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2018-09-10
    • 2021-10-12
    • 2015-11-17
    相关资源
    最近更新 更多