Once code is generated, every GraphQL type has a corresponding Scala object, and every field has a corresponding function returning a SelectionBuilder[Parent, ResultType].
Combining Fields
Use the ~ operator to combine multiple selections. This results in a tuple of the combined types.
// Returns SelectionBuilder[Character, (String, List[String])]
val selection = Character.name ~ Character.nicknames
Mapping to Case Classes
To avoid working with nested tuples, use .mapN to map selections directly into a case class:
case class CharacterView(name: String, nickname: List[String], origin: Origin)
val character: SelectionBuilder[Character, CharacterView] =
(Character.name ~ Character.nicknames ~ Character.origin).mapN(CharacterView)
Nested Selections
For fields that return object types, pass a SelectionBuilder as a block to specify which sub-fields to retrieve:
// Querying a list of characters from the RootQuery
val query: SelectionBuilder[RootQuery, List[CharacterView]] =
Query.characters {
character
}
Arguments
If a GraphQL field requires arguments, the generated Scala function will require them as well:
// Querying characters with a specific origin argument
val query = Query.characters(Origin.MARS) { character }
case class CharacterView(name: String, nickname: List[String], origin: Origin)
val character: SelectionBuilder[Character, CharacterView] =
(Character.name ~ Character.nicknames ~ Character.origin).mapN(CharacterView)
val query: SelectionBuilder[RootQuery, List[CharacterView]] =
Query.characters(Origin.MARS) {
character
}