【发布时间】: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}}? -
为什么不发短信?对于其他请求,它可以工作。
-
@Arnaud 看这里:graphql.org/learn/queries/#variables
-
请注意,我给出的示例 - 查询中的
...将包含实际查询。
标签: java graphql postman graphql-java