Otis Ottington
Otis Ottington

Reputation: 477

Graphql - Query returns null

I am using the graphql-java-annotations library for java to retrieve data from my spring backend.

<dependency>
     <groupId>io.github.graphql-java</groupId>
      <artifactId>graphql-java-annotations</artifactId>
      <version>7.1</version>
</dependency>

When I call the query it always return null.

This is my Provider class:

GraphQLAnnotations graphqlAnnotations = new GraphQLAnnotations();
GraphQLSchema graphQLSchema = newSchema()
            .query(graphqlAnnotations.object(QueryTest.class))
            .build();
this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();

This is the query:

@GraphQLName("queryTest")
public class QueryTest {

    @GraphQLField
    public static Test byId(final DataFetchingEnvironment env,         @GraphQLName("id") Long id) {
        return new Test();
    }
}

And finally the Test.class

@GraphQLName("Test")
public class Test {

    private String id;
    private String name;

    public Test() {
        this("0");
    }

    public Test(String id) {
        this.setName("Name" + id);
        this.setId(id);
    }

    @GraphQLField
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @GraphQLField
    public String getId() {
        return id;
   }

    public void setId(String id) {
        this.id = id;
    }
}

This is my call:

{ 
   "query" : "query queryTest { byId(id: 2) { getId } }",
   "operationName" : "queryTest"
}  

And this is the result i get:

{
  "data": {
    "byId": null
  }
}

I debugged into the graphql execution and found out that schema contains TestClass and Test. So type and query are known. With this configuration I don't have a fetcher or a resolver.

Upvotes: 2

Views: 973

Answers (1)

Otis Ottington
Otis Ottington

Reputation: 477

Found the solution:

I had to change my Provider class in order to create the schema correctly via the AnnotationsSchemaCreator Builder:

GraphQLSchema graphQLSchema = AnnotationsSchemaCreator.newAnnotationsSchema()
            .query(QueryTest.class)
            .typeFunction(new ZonedDateTimeFunction())
            .build();

this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();

Upvotes: 1

Related Questions