Spring GraphQL return NULL

Spring GraphQL return NULL


I want to create my own website in the form of a blog using Spring Boot and GraphQL API, but I don’t understand how to connect the database to GraphQL. When making a request through /graphiql, it returns NULL. I would appreciate any help and advice. I am using PHPMAdmin as database interface. Leave a comment if any more information comes up.


  "data": {
    "posts": null




package com.example.demo.graphql;

import java.util.List;

import com.example.demo.models.Post;
import com.example.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.coxautodev.graphql.tools.GraphQLQueryResolver;

public class PostQuery implements GraphQLQueryResolver {

    private PostService service;

    public List<Post> getPosts() {
        return service.getAllPosts();

    public Post getPost(long id) {
        return service.getPost(id);



package com.example.demo.models;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Post {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private  Long id;
    private  String title, anons, full_text;
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public String getAnons() { return anons; }
    public void setAnons(String anons) { this.anons = anons; }
    public String getFull_text() { return full_text; }
    public void setFull_text(String full_text) { this.full_text = full_text; }

    public Post() { }

    public Post(String title, String anons, String full_text) {
        this.title = title;
        this.anons = anons;
        this.full_text = full_text;



package com.example.demo.repositorys;

import com.example.demo.models.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

public interface PostRepository extends JpaRepository<Post, Long> {




package com.example.demo.service;

import com.example.demo.models.Post;

import java.util.List;

public interface PostService {
    public List<Post> getAllPosts();
    public Post getPost(long id);
    public Post createPost(String title, String anons, String full_text);



package com.example.demo.service;

import java.util.List;
import com.example.demo.models.Post;
import com.example.demo.repositorys.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class PostServiceImpl implements PostService{

    PostRepository repository;

    public List<Post> getAllPosts() {
        return repository.findAll();

    public Post getPost(long id) {
        return repository.getById(id);

    public Post createPost(String title, String anons, String full_text) {
        final Post post = new Post();
        return repository.save(post);



type Query {
    postById(id: ID): Post
    posts: [Post]

type Post {
    id: ID,
    title: String,
    anons: String,
    full_text: String

If we go into more detail, my idea is to create a portfolio website with a list and description of projects using various programming languages. I want each project to be presented as a separate block with the project name, a brief description, and a more detailed explanation of the project. Initially, I planned to directly output the text from the database onto the webpage, but eventually decided to use an API. However, when I started to rework it, I wasn’t sure how to connect the database itself and the GraphQL queries.

1 Answer


You are not telling us what kind of GraphQL Library you are using. It seems to be graphql-java-kickstart. The package name com.coxautodev is deprecated by the way.

I guess you missed the glue as you need to parse the schema to get started:

    .schemaString("Query { }")

See https://www.graphql-java-kickstart.com/tools/schema-definition/

But you really should take a look into Spring GraphQL and the tutorial for Spring GraphQL.

Leave a Reply

Your email address will not be published. Required fields are marked *