【问题标题】:Static data fetcher returns null (GraphQL/Java)静态数据获取器返回 null (GraphQL/Java)
【发布时间】:2019-06-21 22:24:48
【问题描述】:

我有以下简单的模式/查询示例,其中包含 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}

我错过了什么吗?

【问题讨论】:

    标签: java graphql-java


    【解决方案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"
        }
    

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2020-04-05
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 2019-06-14
      相关资源
      最近更新 更多