Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:18
Show Gist options
  • Select an option

  • Save dacr/893277b50e771fae565c65ae30f7fbba to your computer and use it in GitHub Desktop.

Select an option

Save dacr/893277b50e771fae565c65ae30f7fbba to your computer and use it in GitHub Desktop.
ZIO learning - graphql API / published by https://github.com/dacr/code-examples-manager #1359058d-f8c3-4a04-bdea-5b520c5de8f8/a1f8779770b33cf81357cc3bfa45dfd583a1b18c
// summary : ZIO learning - graphql API
// keywords : scala, zio, learning, pure-functional
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 1359058d-f8c3-4a04-bdea-5b520c5de8f8
// created-on : 2021-04-03T15:31:41Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// Functional GraphQL library for Scala : https://ghostdogpr.github.io/caliban/
// inspired from https://ghostdogpr.github.io/caliban/docs/#a-simple-example
// ---------------------
//> using scala "3.4.2"
//> using dep "com.github.ghostdogpr::caliban:2.0.0"
// ---------------------
import zio.*
import caliban.GraphQL.graphQL
import caliban.RootResolver
// =========================================================================================
object Embedded extends ZIOAppDefault {
// =========================================================================================
object ASimpleExample {
case class Character(firstName: String, lastName: String, age: Int, gender: String)
val characters = List(
Character("sarah", "connor", 34, "f"),
Character("john", "connor", 12, "m"),
Character("kyle", "reese", 24, "m"),
Character("ginger", "ventura", 33, "f"),
Character("miles", "dyson", 42, "m")
)
def getCharacters: List[Character] = characters
def getCharacter(name: String): Option[Character] = characters.find(_.firstName == name)
}
// =========================================================================================
object Schema {
import ASimpleExample.{Character, getCharacter, getCharacters}
// schema
case class CharacterName(name: String)
case class Queries(
characters: List[Character],
character: CharacterName => Option[Character]
)
// resolver
val queries = Queries(getCharacters, args => getCharacter(args.name))
}
import Schema.queries
val api = graphQL(RootResolver(queries))
// -----------------------------------------------------------
val query = """ { characters { firstName lastName } }"""
//val query = """ { character(name: "john") { firstName age } }"""
override def run =
for {
_ <- Console.printLine("Testing caliban...")
interpreter <- api.interpreter
_ <- Console.printLine("Executing query...")
result <- interpreter.execute(query)
_ <- Console.printLine(result.data.toString)
} yield ()
}
Embedded.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment