Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot-starter-graphql: getting 404 for /graphql endpoint

I am using the new Spring for Graphql in my Spring Boot application. My problem is, that the server always responds 404 when making a POST request to the /graphql endpoint.

These are my gradle dependencies

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.flywaydb:flyway-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'

I followed the steps in this tutorial to create the controllers (formerly known as resolvers, I actually changed all my old Graphql resolvers to the new @QueryMapping and @MutationMapping annotations)

This is the code for my StepController

@Slf4j
@Controller
@RequiredArgsConstructor
public class StepController {
  private final StepService stepService;
  private final TokenService tokenService;

  @QueryMapping
  public ArrayList<Step> getSteps(String templateId) {
    return stepService.getSteps(templateId);
  }

  @MutationMapping
  public Step createStep(StepInput input, DataFetchingEnvironment env) {

    return stepService.createStep(input, tokenService.getUserId(env));
  }
}

I am also using WebSecurity in my project, changed my deprecated WebSecurityConfigurerAdapter to the more modern way of doing this by creating a Bean inside a SecurityConfig.class

@Configuration
public class SecurityConfiguration {

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.cors()
        .configurationSource(request -> {
          var cors = new CorsConfiguration();
          cors.setAllowedOrigins(List.of("http://localhost:4200",     "electron://altair"));
          cors.setAllowedMethods(List.of("GET", "POST", "DELETE", "OPTIONS"));
          cors.setAllowedHeaders(List.of("*"));
          cors.setAllowCredentials(true);
          return cors;
        })
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/graphql").permitAll()
        .antMatchers(HttpMethod.GET, "/stomp/**").permitAll()
        .anyRequest().authenticated()
        .and()
        // this disables session creation on Spring Security
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    return http.build();
  }
}

My GraphQl Schema is structured in resources/graphql in a folder structure, I had no problem with this before I switched to the new Spring Boot Graphql

enter image description here

UPDATE To be on the safe side: I deleted the whole folder structure and replaced it with one schema.graphqls file that has just the Login mutation and a query that is never used, just to avoid the Query needed compiler error

type Mutation {
  login(email: String!, password: String!): Login!
}

type Login {
  token: String
}

// unused, just to make the compiler happy
type Query {
  getLogin: Login
}

application.properties

spring.banner.location=classpath:logo.txt
spring.main.banner-mode=console
spring.output.ansi.enabled=ALWAYS
spring.main.allow-bean-definition-overriding=true

spring.mail.host=mail.blablabla
spring.mail.port=587
spring.mail.username=no-reply@blablabla
spring.mail.password=blablalba
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=false
spring.profiles.include=prod,dev

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.hibernate.ddl-auto=validate

spring.servlet.multipart.max-file-size=512KB

I hope I provided enough information as to how my project is set up. I would really appreciate any help that can solve this issue. I searched the whole evening yesterday, finally now posting this question here. I hope someone can help me. Thanks

like image 908
chriszichrisz Avatar asked Oct 17 '25 01:10

chriszichrisz


1 Answers

If your method attribute does not match the name of your schema method attribute, it will 404.

For example, if you have:

@QueryMapping
public Login getSteps(@Argument String tId) {
  return stepService.getSteps(tId);
}

If your schema specifies:

type Query {
  getSteps(String templateId): Login
}

You will just get a 404 until you make the schema naming match all over. Tedious, perhaps, but magical!

The correct mapping would be:

@QueryMapping
public Login getSteps(@Argument String templateId) {
  return stepService.getSteps(templateId);
}
like image 87
sanimalp Avatar answered Oct 20 '25 13:10

sanimalp