How to properly setup GraphQL with Spring boot and PostgreSQL?

433 Views Asked by At

I have the following issues.

  1. introspection request failed : 404 error when trying to send requests to http://localhost:8080/graphql using the altair graphql client.
  2. http://localhost/graphiql is not accessible.

I am new to GraphQL in Spring boot. I am using JDK 17 and IntelliJ Community Edition.

I want to make a GraphQL + Spring boot app with Postgresql. Using the Spring Initializr, I created a project and added the following dependencies.

I also added the following to the application.properties file.

spring.jpa.hibernate.ddl-auto=update
spring.jackson.serialization.fail-on-empty-beans=false
spring.datasource.url=jdbc:postgresql://localhost:5432/demo
spring.datasource.username=demo
spring.datasource.password=demo

When I start this application, I get the following output:

...
Tomcat started on port(s): 8080 (http) with context path ''
Started JavaSpringGraphqlApplication in 4.203 seconds (process running for 4.607)

and when I paste http://localhost:8080/graphql in altair, the console shows the following lines

...
Initializing Spring DispatcherServlet 'dispatcherServlet'
Initializing Servlet 'dispatcherServlet'
Completed initialization in 2 ms

I have worked with Nestjs + GraphQL and it works fine but with Spring boot I can't understand what I am doing wrong. Can someone help me set up this to the point where I can

  1. set the localhost:8080/graphql in altair.
  2. access graphiql or graphql playground.
1

There are 1 best solutions below

0
easyChi On

Ok Let me try help you get setup:

  1. go to https://start.spring.io and choose
    • Maven
    • Java
    • latest stable Spring Boot Version(currently 3.1.3)
    • Add your Project Metadata
    • packaging: War (later, when you have everything running, you can change to jar)
    • Java: 17
    • Click on Add Dependencies
    • Search for "Spring for GraphQL" and add it
    • (optional)Search for "Spring Boot DevTools", add it
    • Click Generate -> download and unzip that file.
    • Open you favorite IDE und import that folder as an existing Maven Project into your workspace.
    • Try to run your Spring Boot Application to see if everything is working, in my case i have to run DemoApplication(annotated with @SpringBootApplication)
    • output: Started DemoApplication in 10.229 seconds (process running for 12.347)
  2. add the following lines to your "application.properties"
    spring.graphql.path= /graphql  
    spring.graphiql.enabled= true  
    spring.graphiql.path= /graphiql
  1. In the same folder where your "application.properties" is located you will find a folder called "graphql". Lets create a file called "query.graphqls" and add following lines to it:

    type Query{  
      people: [Person]  
    }

  1. Lets create a folder und "graphql" folder called "person" or(whatever) under that folder create a file called "personType.graphqls". My File looks like this:

    type Person{  
      id: ID!  
      name: String  
      surname: String  
    }  

  1. now lets create a class called Person, I will put all my classes in the same package:

    public class Person {
      private String id;
      private String name;
      private String surname;
      
      public Person() {  
      
      }  
      public Person(String id, String name, String surname) {  
        this();  
        setId(id);  
        setName(surname);  
        setSurname(surname);  
      }
      public String getId() {  
        return id;  
      }  
      public void setId(String id) {  
        this.id = id;  
      }  
      public String getName() {  
        return name;  
      }  
      public void setName(String name) {  
        this.name = name;  
      }  
      public String getSurname() {  
        return surname;  
      }  
      public void setSurname(String surname) {  
        this.surname = surname;  
      }  
    }

  1. Now lets create a Controller, that handles the requests, in my case I will just modify my Application Entry Class like this:

    @SpringBootApplication
    @Controller
    public class DemoApplication {
    
    public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
    }
    
    @QueryMapping
    public List<Person> people(){//make sure that this function signature and name is same with the one in query.graphqls
        
      return Arrays.asList(
                new Person("M.A.", "Muhammad","Ali"),
                new Person("M.T.", "Mike","Tython"),
                new Person("F.M.", "Floyd","Mayweather")
                );
        
      }

    }

  1. Test your endpoints: http://localhost:8080/graphql (use Postman or Curl) http://localhost:8080/graphiql (use Browser)

I hope this helps you get started!