静态数据获取器 returns null (GraphQL/Java)
Static data fetcher returns null (GraphQL/Java)
我在 Java 中有以下简单的 schema/query 程序模式创建示例。在这里,我试图创建以下模式:
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 中有以下简单的 schema/query 程序模式创建示例。在这里,我试图创建以下模式:
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"
}