Problem with spring boot graphql. Request /graphql results with 404
Asked Answered
T

5

8

I'm trying to run simplest graphql example. I created application with spring initializer and only added graphql dependencies. My build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')

    compile 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.3.1'
    compile 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.3.1'
    compile 'com.graphql-java-kickstart:voyager-spring-boot-starter:5.3.1'
}

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

When I run the project and hit the endpoint /graphql it returns 404. What is missing in my configuration?

Tawny answered 6/12, 2018 at 9:41 Comment(4)
Did you check the port you're requesting on? And does Spring log anything when receiving the request?Preordain
Yes, I checked the port. 404 means that server received the request.Tawny
That's true, my bad. And this graphql package you have in your dependencies is supposed to expose the endpoint /graphql automatically? I see it's in your deps, but there's no further config regarding graphql api.Preordain
Docs are quite messy, cant find any minimal working example here github.com/graphql-java-kickstart/graphql-spring-boot. I must have been missed sth important.Tawny
P
11

The docs (https://github.com/graphql-java-kickstart/graphql-spring-boot#enable-graphql-servlet) say:

The servlet becomes accessible at /graphql if graphql-spring-boot-starter added as a dependency to a boot application and a GraphQLSchema bean is present in the application.

...and the minimum example it links to looks like this:

@SpringBootApplication
public class ApplicationBootConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationBootConfiguration.class, args);
    }

    @Bean
    GraphQLSchema schema() {
        return GraphQLSchema.newSchema()
            .query(GraphQLObjectType.newObject()
                .name("query")
                .field(field -> field
                    .name("test")
                    .type(Scalars.GraphQLString)
                    .dataFetcher(environment -> "response")
                )
                .build())
            .build();
    }
}

So you're missing a Graphql schema to be used. It says if there is one, the API endpoint will be exposed automatically.
Good luck!

Preordain answered 6/12, 2018 at 10:50 Comment(1)
does not work, throws another error on startup saying that you need a bean of type GraphQLTomaso
E
3

I had a equal problem using graphiql and Swagger The page graphiql got me erros 404 on browser console. The problem was that cannot get the js library from vendor resource.

On class SwaggerConfig extends WebMvcConfigurationSupport

I did:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
//This is for graphiql works
registry.addResourceHandler("/vendor/**")
      .addResourceLocations("classpath:/static/vendor/");

}

Enneahedron answered 14/8, 2020 at 15:21 Comment(0)
D
0

In my case, the following steps resolved the issue:

  1. Create a directory named src/main/resources/graphql.
  2. Within this directory, add a new file named anyname.graphqls.
  3. Add the following schema to the file:
type Query { 
    test: String 
}

It was challenging to find a solution, as the absence of the directory and file didn't trigger any errors. Instead, accessing /graphql resulted in a 404 error.

Daina answered 13/10, 2023 at 21:18 Comment(0)
F
0

I had this issue with a very simple GraphQL application that I had made based on an article, when I tested the http://localhost:8989/graphql with Postman I got 404 NOT_FOUND error, in my case the problem was that I was testing the API with a REST POST request call in the Postman, you need to create a new GraphQL request by clicking on New button in Postman and then if your application has been launched, automatically accessible API's is displayed, see the below screenshot: enter image description here

Falster answered 5/2, 2024 at 18:14 Comment(0)
A
0

When calling a spring boot service ensure to pass "content-type: 'application/json'" header. The following:

 curl  -X POST --data '{<query-here>}' http://localhost:8080/graphql

will fail with a 404 error, while:

  curl -H "content-type: application/json"  -X POST --data '{<query-here>}' http://localhost:8080/graphql

should work.

Appal answered 15/7, 2024 at 14:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.