Skip to content

Instantly share code, notes, and snippets.

@erikdstock
Created July 22, 2020 16:46
Show Gist options
  • Save erikdstock/400087f377da26f7f5dbeb59bf3f6f1f to your computer and use it in GitHub Desktop.
Save erikdstock/400087f377da26f7f5dbeb59bf3f6f1f to your computer and use it in GitHub Desktop.
Scala, Sangria, lazies and implicits
trait CartTypes {
implicit val ec: ExecutionContextExecutor
val itemType: ObjectType[RequestServices, Item]
lazy val cartType: ObjectType[Unit, Cart] = ObjectType[RequestServices, Cart](
/* ... */
fields = Field(
"items",
ListType(ItemType),
resolve = c => c.ctx.getItems(c.value.id)
)
)
}
trait ItemTypes {
implicit val cartType: ObjectType[RequestServices, ShoppingCart]
lazy val itemType = deriveObjectType[RequestServices, Item]()
}
case class Schema()(implicit val ec: ExecutionContextExecutor)
extends CartTypes
with ItemTypes {
val QueryType = ObjectType(
"Query",
fields[RequestServices, Unit]( /* the types are used */ )
)
}
@erikdstock
Copy link
Author

My impression from the above is that

For CartTypes and ItemTypes, the lazy vals are due to interdependence on each other
val itemType: ObjectType[RequestServices, Item] means the member is necessary to be implemented in the concrete Schema implementation (straightforward)
implicit val cartType: ObjectType[RequestServices, ShoppingCart]does the same but adds an additional flag to the compiler to make this val available within the deriveObjectType macro. This is because we don't refer to it directly as with an ObjectType literal. The ItemTypes is allowed to make its implemented cartType implicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment