gpt4 book ai didi

java - 静态数据 getter 返回 null (GraphQL/Java)

转载 作者:行者123 更新时间:2023-12-02 09:45:41 27 4
gpt4 key购买 nike

我有以下简单的模式/查询示例,其中使用 Java 进行编程模式创建。在这里,我尝试创建以下架构:

query{
board {
name: string
}
}

代码:

GraphQLObjectType queryType = GraphQLObjectType.newObject()
.name("BoardQuery")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("board")
.type(GraphQLObjectType.newObject().name("boardType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("name")
.type(GraphQLString).build()))

.build())

.build();

GraphQLCodeRegistry graphQLCodeRegistry = GraphQLCodeRegistry.newCodeRegistry()
.dataFetcher(FieldCoordinates.coordinates("boardType","name"),
new StaticDataFetcher("hello")).build();
GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
.query(queryType)
.codeRegistry(graphQLCodeRegistry)
.build();
GraphQL graphQl = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = graphQl.execute("query { board { name } }");
System.out.println(executionResult.getData().toString());

预期输出:

{name:hello}

实际输出:

{name:null}

我在这里遗漏了什么吗?

最佳答案

我自己对此很陌生,但我会尝试一下。

    //The dataFetcher used to fetch a board object.
DataFetcher boardDataFetcher() {
return environment -> {
Object board = new Object() {
String name = "board Name";
};
return board;
};
}
//Your deffinition of what a board type contains/what of the board type you want to expose
public GraphQLObjectType boardType = newObject()
.name("boardType")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
)
.build();
// Define your query types
public GraphQLObjectType queryType = newObject()
.name("Query")
.field(newFieldDefinition()
.name("board")
.type(boardType)
)
.build();

// wire the query, board and datafetcher together
public GraphQLCodeRegistry codeRegistry = newCodeRegistry()
.dataFetcher(
coordinates("Query", "board"), boardDataFetcher()
)
.build();
//create the schema
GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
.query(queryType)
.codeRegistry(codeRegistry)
.build();

这适用于像这样的查询

board{name}

应该给你回复:

"board": {
"name": "board Name"
}

关于java - 静态数据 getter 返回 null (GraphQL/Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681033/

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