/graphql and /graphiql endpoints returning 404

27 Views Asked by At

I am learning graphql and facing some issue in the local with hello-world level project code. Can you please help me on it.

Given the below details, I am able to build and run the application without any errors. But when I hit the urls localhost:8080/graphiql(in browser) or localhost:8080/graphql(in AltAir plugin), I am getting 404 as reponse (Whitelabel error page). Can you please help me on this. Thanks in advance.

Details

  1. I am using Java 17 as runtime.
  2. Spring boot version : 3.2.1
  3. Maven dependencies.
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>5.2.4</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
  1. Query Class
@Component  
public class Query implements GraphQLQueryResolver {
    public String firstName() {
        return "Sankar";
    }  
}
  1. Graphql schema file (query.graphqls)
type Query{
    firstName : String
}
1

There are 1 best solutions below

1
On

I guess it is caused by incorrect dependencies for GraphQL support.

I used Spring Initializr and it provides the following dependency in the pom.xml of generated project:

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

Please, try to remove com.graphql-java dependencies and use the above instead or better generate a new pom.xml using Initializr for your project.

And one more thing to check for graphiql endpoint: in order to make it work, add spring.graphql.graphiql.enabled: true into application.yml of the project.