-
Notifications
You must be signed in to change notification settings - Fork 5
Support for GraphQL subscriptions
In the articles "Overview"
and "Abstract types", we looked in detail at the execution of GraphQL queries
in the generated DSL. Execution of GraphQL mutations in the generated DSL is exactly the same as the execution of
GraphQL queries (we just need to use context mutation function instead of query function). It remains to deal with
GraphQL subscriptions. Let's define a GraphQL schema:
type Subscription {
filmCreated: Film!
}
type Film {
title: String!
}According to this schema, we can subscribe to new films:
subscription {
filmCreated {
title
}
}To receive JSON messages that look like this:
{
"data": {
"filmCreated": {
"title": "First"
}
}
}Let's take a look at the context interface generated by the schema:
public interface ExampleContext {
public suspend fun query(__projection: QueryProjection.() -> Unit): Query
public suspend fun mutation(__projection: MutationProjection.() -> Unit): Mutation
public fun subscription(__projection: SubscriptionProjection.() -> Unit): ExampleSubscriber<Subscription>
}
public fun interface ExampleSubscriber<T> {
public suspend fun subscribe(block: suspend ExampleReceiver<T>.() -> Unit): Unit
}
@ExampleDSL
public fun interface ExampleReceiver<out T> {
public suspend fun receive(): T
}The semantics of the subscription function is different from the semantics of the query and mutation functions.
While the query and mutation functions take a projection argument to build a query and return the result of the
query execution, the subscription function takes a projection but returns a ExampleSubscriber interface. The
"subscriber" interface allows us to create a long-lived session to listen for incoming messages. The session lifetime is
the same as the execution time of the subscribe function in the ExampleSubscriber interface. When we enter
the subscribe function, a session is created, and when we exit it, the session is destroyed.
The subscribe function is executed in scope of ExampleReceiver interface, and we can call the receive function to
receive the next message. Messages are usually listened to in an infinite loop with a call to the receive function
inside the loop. Let's try this: