java - Spring Boot GraphQL 无法加载架构
问题描述
我在 Spring Boot 2+ 和 GraphQL 中使用以下示例,但在加载 graphiql 时出现以下错误,架构未加载。
https://github.com/TechPrimers/spring-boot-graphql-query-example
该代码通过来自邮递员但来自 Graphiql 的 POST 调用工作。
@RestController
public class GraphQLController {
private GraphQLService graphQLService;
public GraphQLController(GraphQLService graphQLService) {
this.graphQLService = graphQLService;
}
@PostMapping("/graphql")
public ExecutionResult graphQl(@RequestBody String query) {
ExecutionResult result = graphQLService.getGraphQL().execute(query);
return result;
}
}
{
"data": null,
"errors": [
{
"message": "Invalid Syntax : offending token '\"query\"' at line 1 column 1",
"sourcePreview": "{\"query\":\"\\n query IntrospectionQuery {\\n __schema {\\n queryType { name }\\n mutationType { name }\\n types {\\n ...FullType\\n }\\n directives {\\n name\\n description\\n locations\\n args {\\n ...InputValue\\n }\\n }\\n }\\n }\\n\\n fragment FullType on __Type {\\n kind\\n name\\n description\\n fields(includeDeprecated: true) {\\n name\\n description\\n args {\\n ...InputValue\\n }\\n type {\\n ...TypeRef\\n }\\n isDeprecated\\n deprecationReason\\n }\\n inputFields {\\n ...InputValue\\n }\\n interfaces {\\n ...TypeRef\\n }\\n enumValues(includeDeprecated: true) {\\n name\\n description\\n isDeprecated\\n deprecationReason\\n }\\n possibleTypes {\\n ...TypeRef\\n }\\n }\\n\\n fragment InputValue on __InputValue {\\n name\\n description\\n type { ...TypeRef }\\n defaultValue\\n }\\n\\n fragment TypeRef on __Type {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n ofType {\\n kind\\n name\\n }\\n }\\n }\\n }\\n }\\n }\\n }\\n }\\n\"}\n",
"offendingToken": "\"query\"",
"locations": [
{
"line": 1,
"column": 1,
"sourceName": null
}
],
"errorType": "InvalidSyntax",
"path": null,
"extensions": null
}
],
"dataPresent": false,
"extensions": null
}
解决方案
您需要使用属性 query、operationName 和 arg 创建自己的对象。
public class Data {
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
@PostMapping("/graphql")
public ExecutionResult graphQl(@RequestBody Data query) {
ExecutionResult result = graphQLService.getGraphQL().execute(query.getQuery());
return result;
}
推荐阅读
- kubernetes - 在 Kubernetes 中保持 pod 卷挂载可配置
- java - 如何使用 gson 或其他 Java 库从包含 json 的字符串中提取字段
- kubernetes - Kubernetes限制和资源请求最好更接近
- android - 如何在活动转换中包含未附加的视图?
- java - 如何修复“java.lang.IllegalArgumentException:预期命名参数但得到 [XXXX]”
- django - 部署开发后的 AWS Lambda 502
- ruby - 在 docker 机器上运行黄瓜测试会破坏我的测试数据库
- terraform - Terraform 的 for_each 中的迭代器功能是什么?
- python - 使用字符串格式格式化数字但不改变值
- java - 关于异构容器的一般问题?