GraphQL
Generating SDL
Code-first: write Kotlin classes and let Kest write the schema.
There are two ways to define a GraphQL API:
| Style | You write | Good for |
|---|---|---|
| Schema-first | the SDL by hand, then resolvers | designing the API as a contract first, a team that thinks in schema |
| Code-first | Kotlin classes and resolvers; the schema is generated | one 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 useKest 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
| Kotlin | GraphQL |
|---|---|
String, Int, Boolean, Double | String!, Int!, Boolean!, Float! |
T? | nullable (no !) |
a property or argument named id | ID |
Long, Instant, LocalDate, BigDecimal, UUID, Map | the ready-made scalars Long, DateTime, Date, BigDecimal, UUID, JSON |
List<T> | [T!]! |
enum class | enum |
| data class used as a result | type |
| the same data class used as an argument | input, named ...Input |
Page<T> | TPage with items, page, size, total, pages, hasNext, hasPrevious |
a Publisher<T> returned by @Subscription | the 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
)| Annotation | On | Effect |
|---|---|---|
@GqlType(name, description) | a class | rename the type, add a description |
@GqlField(description, deprecated) | a property | describe it; mark it @deprecated(reason: ...) |
@GqlIgnore | a property | leave it out |
@GqlInterface(implementations, description) | an interface | a GraphQL interface |
@GqlUnion(members, description) | a class or interface | a 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 | WriterAn 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
typeand aninput, 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.