dheeraj suthar
dheeraj suthar

Reputation: 147

Static data fetcher returns null (GraphQL/Java)

I have following simple schema/query example with programmatic schema creation in Java. Here, I am trying to create following schema:

query{
  board {
   name: string
 }
}

Code:

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());

Expected output:

{name:hello}

Actual output:

{name:null}

Am I missing something here?

Upvotes: 0

Views: 1013

Answers (1)

Sondre
Sondre

Reputation: 364

Im new to this myself but this i'll give it a shot.

    //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();

This works with a query like

board{name}

Should give you the response:

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

Upvotes: 1

Related Questions