gpt4 book ai didi

java - Spring Boot GraphQL 无法加载架构

转载 作者:行者123 更新时间:2023-12-02 01:38:07 25 4
gpt4 key购买 nike

我将以下示例与 Spring Boot 2+ 和 GraphQL 结合使用,但在加载 graphiql 时收到以下错误,架构未加载。

https://github.com/TechPrimers/spring-boot-graphql-query-example

该代码通过来自 postman 但来自 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;
}

关于java - Spring Boot GraphQL 无法加载架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54895420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com