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.
The
graphiqlendpoint is available with this dependencyspring-boot-starter-graphql. Keep the following dependency only in pom.xml for graphQL and enable the graphiQL inapplication.propertieswith the following linespring.graphql.graphiql.enabled=true.I refactored the schema in
resources/graphql/schema.graphqlsAlso, added the
QueryMappingfor the GraphQL Query in theControllerFile.The above code changes made the endpoint working.
Additional note: If you want to use
GraphQLQueryResolverinterface, this would be available in projectcom.graphql-java-kickstart. So, one would need to add this dependency in their project.