我有以下简单的模式/查询示例,其中包含使用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}
我在这里想念东西吗?
答案 0 :(得分:1)
我本人并不陌生,但是我会给它一个机会。
//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"
}