Search code examples
spring-bootgraphqlspring-graphql

Kotlin + Spring + GraphQL : 405 METHOD_NOT_ALLOWED


I am building a simple example with Spring boot + GrapQL

package com.example.graphql

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.graphql.data.method.annotation.QueryMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class GraphqlApplication

fun main(args: Array<String>) {
    runApplication<GraphqlApplication>(*args)
}

@RestController
class MyQueryController {

    @QueryMapping
    fun message(): Message = Message("1","some-text")

}

data class Message(var id: String, var text: String) {
}

then I got the schema under src/main/resources/graphql/schema.graphqls withe the following

type Query {
    message: Message
}
type Message {
    id: String
    text: String
}

When I try to go to http://localhost:8080/graphql I got [95221568-1] Completed 405 METHOD_NOT_ALLOWED, headers={masked}

Any clue ?

this are dependencies in the pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    ....
    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.kotlin</groupId>
            <artifactId>reactor-kotlin-extensions</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-coroutines-reactor</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    ....

Solution

  • As explained by @d.j.brown in the comments, Spring for GraphQL does not support GET queries on the "/graphql" endpoint, even though it can be supported by other implementations. Only POST HTTP requests are supported.

    If you'd like to explore your API, you can enable the graphiql UI by enabling it in the configuration properties with spring.graphql.graphiql.enabled=true and browsing http://localhost:8080/graphiql. From there, you'll be able to craft and send GraphQL queries using this playground tool.