Created
July 22, 2020 16:46
-
-
Save erikdstock/400087f377da26f7f5dbeb59bf3f6f1f to your computer and use it in GitHub Desktop.
Scala, Sangria, lazies and implicits
This file contains hidden or 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
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 */ ) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.