Endpoint graphiql not working in Spring Boot application

26 Views Asked by At

I am working on a practice project for learning GraphQL with Spring Boot. The endpoint http://localhost:8080/graphiql?path=/graphql is giving a Whitelabel Error saying 404.

File : application.properties

spring.application.name=graphql
spring.datasource.url=jdbc:postgresql://localhost:5432/books
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.graphql.graphiql.enabled=true

File : pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.practise</groupId>
    <artifactId>graphql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>graphql</name>
    <description>Demo project for Spring Boot with GraphQL</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>4.17.0</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

File : src/main/java/com/practise/graphql/query/Query

@Component
public class Query implements GraphQLQueryResolver {
    public String firstQuery() {
        return "First query";
    }
}

File : src/main/resources/schema/query.graphqls

type Query {
    firstQuery : String
}

I tried hitting the following endpoint as well http://localhost:8080/graphiql but it didn't work. Other than above mentioned files, my code contains a sample rest controller in folder src/main/java/com/practise/graphql directory. Please help me with the issue.

1

There are 1 best solutions below

1
Doraemon On

The graphiql endpoint is available with this dependency spring-boot-starter-graphql. Keep the following dependency only in pom.xml for graphQL and enable the graphiQL in application.properties with the following line spring.graphql.graphiql.enabled=true.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

I refactored the schema in resources/graphql/schema.graphqls

type Query {
    book(id: ID) : BookResponse
    fullName(firstName : String, lastName : String) : String
}

type BookResponse {
    id : ID
    bookName : String
    author : String
    description : String
    ownerFirstName : String
    ownerSecondName : String
}

Also, added the QueryMapping for the GraphQL Query in the Controller File.

@Controller
public class BookGraphqlController {
    private BookService bookService;

    public BookGraphqlController(BookService bookService) {
        this.bookService = bookService;
    }

    @QueryMapping
    public String fullName(@Argument String firstName, @Argument  String lastName) {
        return firstName + " " + lastName;
    }

    @QueryMapping
    public GetBookResponse book(@Argument UUID id) {
        return bookService.getBookById(id);
    }
}

The above code changes made the endpoint working.

Additional note: If you want to use GraphQLQueryResolver interface, this would be available in project com.graphql-java-kickstart. So, one would need to add this dependency in their project.