diff --git a/src/content/code/code.md b/src/content/code/code.md index 3383c51bf1..a53c95c929 100644 --- a/src/content/code/code.md +++ b/src/content/code/code.md @@ -586,6 +586,55 @@ val query = graphql"{ hello }" Executor.execute(schema, query) map println ``` +#### [Caliban](https://ghostdogpr.github.io/caliban/) ([github](https://github.com/ghostdogpr/caliban)): Caliban is a purely functional library for building GraphQL servers and clients in Scala + +An example of a GraphQL schema and query with \`caliban\`: + +```scala +// Define a case class for our Schema +case class Character(name: String, age: Int) + +def getCharacters(): List[Character] = ??? +def getCharacter(name: String): Option[Character] = ??? + +// The schema is derived from our case class [[Character]] +// We use [[CharacterName]] to name our arguments. +case class CharacterName(name: String) +// Our Query schema is just a case class +case class Queries(characters: List[Character], + character: CharacterName => Option[Character]) +// We build our Query schema by providing resolvers that satisfy our Queries case class +val queries = Queries(getCharacters, args => getCharacter(args.name)) + +import caliban.GraphQL.graphQL +import caliban.RootResolver + +// Our first line of Caliban! pass your `queries` val to the RootResolver +// and give that to a function `graphQL` that returns a graphQL api. +val api = graphQL(RootResolver(queries)) + +// In order to process requests, you need to turn your API into an interpreter +for { + interpreter <- api.interpreter +} yield interpreter + +case class GraphQLResponse[+E](data: ResponseValue, errors: List[E]) + +// Here is an example of querying your API with a console app. +val query = """ + { + characters { + name + } + }""" + +for { + result <- interpreter.execute(query) + _ <- zio.console.putStrLn(result.data.toString) +} yield () + +``` + ### OCaml / Reason #### [ocaml-graphql-server](https://github.com/andreas/ocaml-graphql-server): GraphQL server library for OCaml and Reason @@ -604,6 +653,7 @@ Executor.execute(schema, query) map println - [Swift / Objective-C iOS](#swift-objective-c-ios) - [Python](#python-1) - [R](#r) +- [Scala](#scala) ### C# / .NET @@ -655,6 +705,10 @@ Executor.execute(schema, query) map println - [graphql-kotlin](https://github.com/ExpediaGroup/graphql-kotlin/): A set of GraphQL libraries that includes a lightweight, typesafe GraphQL HTTP client. +### Scala + + - [Caliban](https://ghostdogpr.github.io/caliban/): Functional GraphQL library for Scala, with client code generation and type-safe queries. + ### Swift / Objective-C iOS - [Apollo iOS](https://www.apollographql.com/docs/ios/) ([github](https://github.com/apollographql/apollo-ios)): A GraphQL client for iOS that returns results as query-specific Swift types, and integrates with Xcode to show your Swift source and GraphQL side by side, with inline validation errors.