-
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add akka-http adapter * Add akka-http rest sample * Remove ws related stuff * Cleanup akkahttp dependencies * Move http4s Example * Reuse example data from package * Update CI and compile new akkahttp module * Make function private * Rename * Fix Example Apps
- Loading branch information
1 parent
3754c33
commit c007126
Showing
5 changed files
with
136 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package caliban | ||
|
||
import akka.http.scaladsl.model.MediaTypes.`application/json` | ||
import akka.http.scaladsl.model.{HttpEntity, HttpResponse} | ||
import akka.http.scaladsl.server.Route | ||
import caliban.Value.NullValue | ||
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport | ||
import io.circe.syntax._ | ||
import zio.{Runtime, URIO} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object AkkaHttpAdapter extends FailFastCirceSupport { | ||
|
||
|
||
private def execute[R, Q, M, S, E]( | ||
interpreter: GraphQL[R, Q, M, S, E], | ||
query: GraphQLRequest | ||
): URIO[R, GraphQLResponse[E]] = | ||
interpreter.execute(query.query, query.operationName, query.variables.getOrElse(Map())) | ||
|
||
def makeHttpService[R, Q, M, S, E](interpreter: GraphQL[R, Q, M, S, E])(implicit ec: ExecutionContext, runtime: Runtime[R]): Route = { | ||
import akka.http.scaladsl.server.Directives._ | ||
post { | ||
entity(as[GraphQLRequest]) { request => | ||
complete({ | ||
runtime.unsafeRunToFuture( | ||
execute(interpreter, request) | ||
.foldCause(cause => GraphQLResponse(NullValue, cause.defects).asJson, _.asJson) | ||
.map(gqlResult => HttpResponse(200, entity = HttpEntity(`application/json`, gqlResult.toString())) )) | ||
.future | ||
}) | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package caliban.akkahttp | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.stream.ActorMaterializer | ||
import caliban.ExampleData.{Character, CharacterArgs, CharactersArgs, Role, sampleCharacters} | ||
import caliban.GraphQL.graphQL | ||
import caliban.schema.Annotations.{GQLDeprecated, GQLDescription} | ||
import caliban.schema.GenericSchema | ||
import caliban.{AkkaHttpAdapter, ExampleService, RootResolver} | ||
import zio.clock.Clock | ||
import zio.console.Console | ||
import zio.stream.ZStream | ||
import zio.{DefaultRuntime, URIO} | ||
|
||
import scala.io.StdIn | ||
|
||
object ExampleApp extends App with GenericSchema[Console with Clock] { | ||
|
||
implicit val system = ActorSystem() | ||
implicit val materializer = ActorMaterializer() | ||
implicit val executionContext = system.dispatcher | ||
implicit val defaultRuntime = new DefaultRuntime {} | ||
|
||
implicit val roleSchema = gen[Role] | ||
implicit val characterSchema = gen[Character] | ||
implicit val characterArgsSchema = gen[CharacterArgs] | ||
implicit val charactersArgsSchema = gen[CharactersArgs] | ||
|
||
case class Queries( | ||
@GQLDescription("Return all characters from a given origin") | ||
characters: CharactersArgs => URIO[Console, List[Character]], | ||
@GQLDeprecated("Use `characters`") | ||
character: CharacterArgs => URIO[Console, Option[Character]] | ||
) | ||
case class Mutations(deleteCharacter: CharacterArgs => URIO[Console, Boolean]) | ||
case class Subscriptions(characterDeleted: ZStream[Console, Nothing, String]) | ||
|
||
val interpreter = defaultRuntime.unsafeRun(ExampleService.make(sampleCharacters).map(service => { | ||
graphQL( | ||
RootResolver( | ||
Queries( | ||
args => service.getCharacters(args.origin), | ||
args => service.findCharacter(args.name) | ||
), | ||
Mutations(args => service.deleteCharacter(args.name)), | ||
Subscriptions(service.deletedEvents))) | ||
})) | ||
|
||
/** | ||
* curl -X POST \ | ||
* http://localhost:8080/api/graphql \ | ||
* -H 'Host: localhost:8080' \ | ||
* -H 'Content-Type: application/json' \ | ||
* -d '{ | ||
* "query": "query { characters { name }}" | ||
* }' | ||
*/ | ||
|
||
val route = | ||
path("api" / "graphql") { | ||
AkkaHttpAdapter.makeHttpService(interpreter) | ||
} ~ path("graphiql") { | ||
getFromResource("graphiql.html") | ||
} | ||
|
||
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) | ||
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...") | ||
StdIn.readLine() | ||
bindingFuture | ||
.flatMap(_.unbind()) | ||
.onComplete(_ => system.terminate()) | ||
|
||
} |
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