-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve caliban (scala) examples (#1494)
- Loading branch information
1 parent
310e3d9
commit 4480aae
Showing
2 changed files
with
24 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
--- | ||
name: Caliban | ||
description: Functional GraphQL library for Scala, with client code generation and type-safe queries. | ||
description: Caliban is a functional library for building GraphQL servers and clients in Scala. It offers with client code generation and type-safe queries. | ||
url: https://ghostdogpr.github.io/caliban/ | ||
github: ghostdogpr/caliban | ||
--- | ||
|
||
An example of defining a GraphQL query and running it with `caliban`: | ||
|
||
```scala | ||
// define your query using Scala | ||
val query: SelectionBuilder[RootQuery, List[CharacterView]] = | ||
Query.characters { | ||
(Character.name ~ Character.nicknames ~ Character.origin) | ||
.mapN(CharacterView) | ||
} | ||
|
||
import sttp.client3._ | ||
// run the query and get the result already parsed into a case class | ||
val result = query.toRequest(uri"http://someUrl").send(HttpClientSyncBackend()).body | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,26 @@ | ||
--- | ||
name: Caliban | ||
description: Caliban is a purely functional library for building GraphQL servers and clients in Scala | ||
description: Caliban is a functional library for building GraphQL servers and clients in Scala. It offers minimal boilerplate and excellent interoperability. | ||
url: https://ghostdogpr.github.io/caliban/ | ||
github: ghostdogpr/caliban | ||
--- | ||
|
||
An example of a GraphQL schema and query with `caliban`: | ||
An example of a simple GraphQL schema and query with `caliban`: | ||
|
||
```scala | ||
import caliban.GraphQL.graphQL | ||
import caliban.RootResolver | ||
|
||
case class Character(name: String, age: Int) | ||
|
||
def getCharacters(): List[Character] = ??? | ||
import caliban._ | ||
import caliban.schema.Schema.auto._ | ||
|
||
// schema | ||
case class Queries(characters: List[Character]) | ||
case class Query(hello: String) | ||
|
||
// resolver | ||
val queries = Queries(getCharacters) | ||
val resolver = RootResolver(Query("Hello world!")) | ||
|
||
val api = graphQL(RootResolver(queries)) | ||
val api = graphQL(resolver) | ||
|
||
for { | ||
interpreter <- api.interpreter | ||
result <- interpreter.execute("{ characters { name } }") | ||
result <- interpreter.execute("{ hello }") | ||
} yield result | ||
``` |