I am new to GraphQL. I am trying to implement the server-side code for a very simple schema in Java using graphql-java. However, I get "WARNING: Query failed to validate : '{articles}'" and a NullPointerException (which makes sense). How do I get results from my simple query? What am I doing wrong? The interesting thing is that when I change the articles type in my Query to String (as follows), it works!
type Query {
articles: String
}
Here is my actual schema:
schema {
query: Query
}
type Query {
articles: [Article]
}
type Article {
id: Int!
title: String
text: String
}
Here is my Java bean (I have used Lombok annotations):
package org.code.beans;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Article {
private int id;
private String title;
private String text;
}
Repository class:
package org.code.repositories;
import java.util.ArrayList;
import java.util.List;
import org.code.beans.Article;
public class ArticleRepository {
private List<Article> articles;
public ArticleRepository() {
this.articles = new ArrayList<>();
}
public void addArticle(Article article) {
articles.add(article);
}
public List<Article> findAll() {
return articles;
}
public Article findOne(int id) {
return articles.stream()
.filter(article -> article.getId() == id)
.findFirst()
.orElse(null);
}
}
The executable Test class that contains the GraphQL code:
package org.code.tests;
import java.io.File;
import java.util.List;
import org.code.beans.Article;
import org.code.repositories.ArticleRepository;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
public class Test {
public static void main(String[] args) {
ArticleRepository articleRepository = new ArticleRepository();
Article article1 = new Article(1, "Article 1", "Text 1");
Article article2 = new Article(2, "Article 2", "Text 2");
articleRepository.addArticle(article1);
articleRepository.addArticle(article2);
SchemaParser parser = new SchemaParser();
TypeDefinitionRegistry registry = parser.parse(new File("./src/org/code/resources/schema.graphql"));
DataFetcher<List<Article>> articlesDataFetcher = new DataFetcher<List<Article>>() {
@Override
public List<Article> get(DataFetchingEnvironment env) throws Exception {
return articleRepository.findAll();
}};
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", wiring -> wiring.dataFetcher("articles", articlesDataFetcher))
.build();
SchemaGenerator generator = new SchemaGenerator();
GraphQLSchema schema = generator.makeExecutableSchema(registry, runtimeWiring);
GraphQL graphQl = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQl.execute("{articles}");
System.out.println(result.getData().toString());
}
}