Created
February 10, 2017 16:56
-
-
Save BjRo/4155fe0232c8cdb3a9b331a893d43fe0 to your computer and use it in GitHub Desktop.
union types in sangria
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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