Skip to content

Instantly share code, notes, and snippets.

@ghostdogpr
Last active December 30, 2023 23:21
Show Gist options
  • Save ghostdogpr/e34ee0528a3764fbd031b66c7f5dc29f to your computer and use it in GitHub Desktop.
Save ghostdogpr/e34ee0528a3764fbd031b66c7f5dc29f to your computer and use it in GitHub Desktop.
//> using dep com.github.ghostdogpr::caliban-quick:2.5.0
// 1. Defining the schema
enum Origin {
case EARTH, MARS, BELT
}
case class Character(name: String, nicknames: List[String], origin: Origin)
case class CharactersArgs(origin: Option[Origin])
case class CharacterArgs(name: String)
case class Query(
characters: CharactersArgs => List[Character],
character: CharacterArgs => Option[Character]
)
import caliban.*
import caliban.schema.ArgBuilder.auto.*
import caliban.schema.Schema.auto.*
println(render[Query])
// 2. Some data and business logic for our example
val sampleCharacters = List(
Character("James Holden", List("Jim", "Hoss"), Origin.EARTH),
Character("Naomi Nagata", Nil, Origin.BELT),
Character("Amos Burton", Nil, Origin.EARTH),
Character("Alex Kamal", Nil, Origin.MARS),
Character("Chrisjen Avasarala", Nil, Origin.EARTH),
Character("Josephus Miller", List("Joe"), Origin.BELT),
Character("Roberta Draper", List("Bobbie", "Gunny"), Origin.MARS)
)
def charactersByOrigin(origin: Option[Origin]): List[Character] =
sampleCharacters.filter(c => origin.forall(_ == c.origin))
def characterByName(name: String): Option[Character] =
sampleCharacters.find(_.name == name)
// 3. Creating the resolver
val queryResolver =
Query(
characters = args => charactersByOrigin(args.origin),
character = args => characterByName(args.name)
)
// 4. Turn the resolver into an API
val api = graphQL(RootResolver(queryResolver))
// 5. Start a server for our API
import caliban.quick.*
api.unsafe.runServer(8088, "/api/graphql")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment