spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.52k stars 300 forks source link

Spring graphql binds to management port when webflux is used. #968

Closed ls-urs-keller closed 4 months ago

ls-urs-keller commented 4 months ago

Graphql also binds to the management port when using webflux.

Steps to reproduce

  1. Unzip and run the demo project
  2. There is a controller both bound to Rest and Graphql
  3. There are 4 cases
    1. Rest on server.port: curl 'http://localhost:8080/test' -> OK
    2. Rest on mgmt.port: curl 'http://localhost:8081/test' -> OK returns 404
    3. Graphql on server.port: curl 'http://localhost:8080/graphql' -H 'Content-Type: application/json' --data-raw '{"query":"{test}"}' -> OK
    4. Graphql on mgmt.port: curl 'http://localhost:8081/graphql' -H 'Content-Type: application/json' --data-raw '{"query":"{test}"}' -> KO returns graphql response

The problem is only present with webflux. The same application without webflux behaves correctly: Change

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

to

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

Test project here:

demo.zip

src/main/resources/graphql/schema.graphqls

type Query {
    test: Boolean!
}

src/main/resources/application.properties

management.server.port=8081

src/main/java/com/example/demo/DemoApplication.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {

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

    /**
     * <pre>
     *      $ curl 'http://localhost:8080/test'
     *      # -> true/false
     *      # OK
     *
     *      curl 'http://localhost:8081/test'
     *      # -> 404
     *      # OK
     *
     *      curl 'http://localhost:8080/graphql' -H 'Content-Type: application/json' --data-raw '{"query":"{test}"}'
     *      # -> {"data":{"test":true/false}}
     *      # OK
     *
     *      curl 'http://localhost:8081/graphql' -H 'Content-Type: application/json' --data-raw '{"query":"{test}"}'
     *      # -> {"data":{"test":true/false}}
     *      # KO
     * </pre>
     */
    @RestController
    public static class MixedController {
        @QueryMapping
        @GetMapping("/test")
        public boolean test() {
            return false;
        }
    }
}

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.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-webflux</artifactId>
        </dependency>
    </dependencies>
</project>
bclozel commented 4 months ago

Thanks for the detailed report. I think this is a duplicate of https://github.com/spring-projects/spring-boot/issues/14012

ls-urs-keller commented 4 months ago

Thanks for the detailed report. I think this is a duplicate of spring-projects/spring-boot#14012

Is there a workaround? Or can the Graphql custom handler be fixed?

bclozel commented 4 months ago

Is there a workaround? Or can the Graphql custom handler be fixed?

Not that I'm aware of.