Last active
May 25, 2024 10:18
-
-
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/4d9465b218d53fe3486d813d87450d2d056bdc9f
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
// summary : ZIO learning - graphql API | |
// keywords : scala, zio, learning, pure-functional | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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