1

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

}
0

2 Answers 2

1

You're not allowed to have a query that "ends" at an object type. When your query selects articles that's a list of objects, but you have to select at least one field from within those objects. You might try instead

graphQl.execute("{ articles { id title text } }")
Sign up to request clarification or add additional context in comments.

Comments

0

You can try with following:

type Query {

  articles(id: Int!, title: String, text: String): [Article]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.