GraphQL
Sharing models
One Kotlin class for REST, validation and GraphQL.
Because the schema can be generated from your classes, one class can serve every layer:
data class NewProduct(
@field:NotBlank val name: String,
@field:Min(0) val price: Long,
)
@Controller("/products") class ProductsController(private val svc: ProductService) {
@Post fun create(@Body input: NewProduct) = svc.create(input) // REST: validated, documented in OpenAPI
}
@Resolver class ProductsResolver(private val svc: ProductService) {
@Mutation fun addProduct(input: NewProduct) = svc.create(input) // GraphQL: the same class, the same validation
}There is no separate mapped-types layer (PartialType, PickType): in Kotlin, write a second data class or use copy. For a partial update make the properties nullable.