Kest

Search

Search the documentation

GraphQL

Generating SDL

Code-first: write Kotlin classes and let Kest write the schema.

There are two ways to define a GraphQL API:

StyleYou writeGood for
Schema-firstthe SDL by hand, then resolversdesigning the API as a contract first, a team that thinks in schema
Code-firstKotlin classes and resolvers; the schema is generatedone source of truth, no schema/code drift, REST and GraphQL sharing classes

Both use the same resolvers. To go code-first, leave out the schema:

data class Product(val id: String, val name: String, val price: Long?)
data class NewProduct(val name: String, val price: Long)
 
@Resolver
class CatalogResolver(private val catalog: Catalog) {
    @Query fun product(id: String): Product? = catalog.find(id)
    @Query fun products(tag: String?): List<Product> = catalog.byTag(tag)
    @Mutation fun addProduct(input: NewProduct): Product = catalog.add(input)
    @ResolveField("Product") fun reviews(product: Product): List<Review> = catalog.reviews(product.id)
}
 
useGraphQL { }          // no schema given: it is generated from the resolvers and the classes they use

Kest produces:

scalar Long
 
type Query { product(id: ID!): Product  products(tag: String): [Product!]! }
type Mutation { addProduct(input: NewProductInput!): Product! }
type Product { id: ID!  name: String!  price: Long  reviews: [Review!]! }
input NewProductInput { name: String!  price: Long! }

Print it any time to review or commit it: println(SdlGenerator.generate(container)).

How Kotlin maps to GraphQL

KotlinGraphQL
String, Int, Boolean, DoubleString!, Int!, Boolean!, Float!
T?nullable (no !)
a property or argument named idID
Long, Instant, LocalDate, BigDecimal, UUID, Mapthe ready-made scalars Long, DateTime, Date, BigDecimal, UUID, JSON
List<T>[T!]!
enum classenum
data class used as a resulttype
the same data class used as an argumentinput, named ...Input
Page<T>TPage with items, page, size, total, pages, hasNext, hasPrevious
a Publisher<T> returned by @Subscriptionthe field's type is T

Describing and shaping the schema

Annotations (all optional) refine what is generated:

@GqlType(description = "A product in the catalogue")
data class Product(
    val id: String,
    val name: String,
    @GqlField(description = "Price in cents", deprecated = "use priceMinor") val price: Long?,
    @GqlIgnore val internalCost: Long,                       // never appears in the schema
)
AnnotationOnEffect
@GqlType(name, description)a classrename the type, add a description
@GqlField(description, deprecated)a propertydescribe it; mark it @deprecated(reason: ...)
@GqlIgnorea propertyleave it out
@GqlInterface(implementations, description)an interfacea GraphQL interface
@GqlUnion(members, description)a class or interfacea GraphQL union

Descriptions show up in the explorer and in tools, and deprecated warns clients before you remove a field.

Interfaces and unions

@GqlInterface(implementations = [Dog::class, Cat::class], description = "Something with a name")
interface Pet { val name: String }
 
data class Dog(override val name: String, val barks: Boolean) : Pet
data class Cat(override val name: String, val lives: Int) : Pet
 
@GqlUnion sealed interface SearchResult                      // sealed: the subclasses ARE the members
data class Book(val title: String) : SearchResult
@GqlType(name = "Writer") data class Author(val name: String) : SearchResult
 
@Resolver class ZooResolver {
    @Query fun pets(): List<Pet> = listOf(Dog("Rex", true), Cat("Tom", 9))
    @Query fun search(q: String): List<SearchResult> = ...
}

Generates:

"Something with a name"
interface Pet { name: String! }
type Dog implements Pet { name: String!  barks: Boolean! }
type Cat implements Pet { name: String!  lives: Int! }
union SearchResult = Book | Writer

An interface needs its implementations listed (or to be sealed) so they are in the schema even when no query returns them directly. At run time Kest finds an object's type from its class (or its @GqlType name).

Limits and when to go schema-first

  • Argument names and property names are used as they are, so renaming a field is not supported (rename the Kotlin property).
  • Directives, and descriptions on arguments, are not generated: write the SDL when you need them.
  • A class that is both returned and taken as an argument becomes a type and an input, which is correct but doubles it: use a separate input class when they differ.

Both styles can be mixed during a migration: a schema given in schema = ... is used as is, and the generator is used only when it is blank.

Read more