Skip to content

Rustavil/example-spring-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring GraphQL Kotlin schema inspection problem

GraphQL schema and Kotlin controller optional argument mismatch

If the GraphQL schema defines an argument as optional (nullable), but the corresponding Kotlin controller method parameter is required (non-nullable), a mismatch occurs. This can lead to runtime errors if the client omits the argument, since Kotlin expects a value. Spring GraphQL does not warn about this mismatch during inspection, so it is important to ensure that the nullability in the schema matches the Kotlin method signature.

If the GraphQL scheme is

type Query {
  greet(name: String!, title: String): String!
}

and the Kotlin controller is defined as:

@Controller
class QueryGreetController {

    @QueryMapping
    fun greet(
        @Argument name: String,
        @Argument title: String, // Intentionally non-nullable to demonstrate GraphQL optional argument problem
    ): String = when {
        title.isNotBlank() -> "Hello, $title $name!"
        else -> "Hello, $name!"
    }
}

then the GraphQL schema inspection will not report any issues

GraphQL schema inspection:
	Unmapped fields: {}
	Unmapped registrations: {}
	Unmapped arguments: {}
	Skipped types: []

GraphQL testing

Greeting GraphQL query Curl example:

curl -X POST -H "Content-Type: application/json" -d '{ "query": "{ greet(name: \"World\") }" }' http://localhost:8080/graphql

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages