Skip to content

Instantly share code, notes, and snippets.

@BjRo
Created February 10, 2017 16:56
Show Gist options
  • Save BjRo/4155fe0232c8cdb3a9b331a893d43fe0 to your computer and use it in GitHub Desktop.
Save BjRo/4155fe0232c8cdb3a9b331a893d43fe0 to your computer and use it in GitHub Desktop.
union types in sangria
object ProfileRef {
val inactiveDefinition = ObjectType(
"InactiveProfileRef",
"An inactive profile",
fields = fields[UserContext, JsObject](
Field("id", IntType, None, resolve = ctx => (ctx.value \ "id").as[Int])
)
)
val activeDefinition = ObjectType(
"ActiveProfileRef",
"An active profile",
fields = fields[UserContext, JsObject](
Field("id", IntType, None, resolve = ctx => (ctx.value \ "id").as[Int])
)
)
val definition = UnionType(
"ProfileRef",
Some("The profile of a user"),
List(inactiveDefinition, activeDefinition)
)
}
object SchemaDefinition {
val ID = Argument("id", IntType, description = "id of the character")
val Query = ObjectType(
"Query", fields[UserContext, Unit](
Field("profileRef", ProfileRef.definition,
arguments = List(ID),
resolve = (ctx) {
JsObject(Seq(
"id" -> JsNumber(ctx.arg(ID))
))
})
))
val schema = Schema(Query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment