首页 > 解决方案 > 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
}

标签: javaspring-bootgraphqlgraphql-java

解决方案


您需要使用属性 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;
}

推荐阅读