Skip to content

Commit

Permalink
#72 akka http without WS (#109)
Browse files Browse the repository at this point in the history
* 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
jona7o authored and ghostdogpr committed Dec 20, 2019
1 parent 3754c33 commit c007126
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- checkout
- restore_cache:
key: sbtcache
- run: sbt ++2.12.10! coreJVM/test http4s/compile examples/compile catsInteropJVM/compile benchmarks/compile
- run: sbt ++2.12.10! coreJVM/test http4s/compile akkaHttp/compile examples/compile catsInteropJVM/compile benchmarks/compile
- save_cache:
key: sbtcache
paths:
Expand All @@ -35,7 +35,7 @@ jobs:
- checkout
- restore_cache:
key: sbtcache
- run: sbt ++2.13.1! coreJVM/test http4s/compile examples/compile catsInteropJVM/compile
- run: sbt ++2.13.1! coreJVM/test http4s/compile akkaHttp/compile examples/compile catsInteropJVM/compile
- save_cache:
key: sbtcache
paths:
Expand Down
36 changes: 36 additions & 0 deletions akka-http/src/main/scala/caliban/AkkaHttpAdapter.scala
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
})
}
}
}
}
20 changes: 19 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,29 @@ lazy val http4s = project
)
.dependsOn(coreJVM)

lazy val akkaHttp = project
.in(file("akka-http"))
.settings(name := "caliban-akka-http")
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % "10.1.11",
"com.typesafe.akka" %% "akka-stream" % "2.5.26",
"de.heikoseeberger" %% "akka-http-circe" % "1.29.1",
"io.circe" %% "circe-parser" % "0.12.3",
compilerPlugin(
("org.typelevel" %% "kind-projector" % "0.11.0")
.cross(CrossVersion.full)
)
)
)
.dependsOn(coreJVM)

lazy val examples = project
.in(file("examples"))
.settings(commonSettings)
.settings(skip in publish := true)
.dependsOn(http4s, catsInteropJVM)
.dependsOn(akkaHttp, http4s, catsInteropJVM)

lazy val benchmarks = project
.in(file("benchmarks"))
Expand Down
75 changes: 75 additions & 0 deletions examples/src/main/scala/caliban/akkahttp/ExampleApp.scala
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())

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package caliban
package caliban.http4s

import caliban.ExampleData._
import caliban.GraphQL._
import caliban.execution.QueryAnalyzer._
import caliban.schema.Annotations.{ GQLDeprecated, GQLDescription }
import caliban.schema.Annotations.{GQLDeprecated, GQLDescription}
import caliban.schema.GenericSchema
import caliban.{CalibanError, ExampleService, GraphQL, Http4sAdapter, RootResolver}
import cats.data.Kleisli
import cats.effect.Blocker
import org.http4s.StaticFile
Expand All @@ -15,7 +16,7 @@ import org.http4s.server.middleware.CORS
import zio._
import zio.blocking.Blocking
import zio.clock.Clock
import zio.console.{ putStrLn, Console }
import zio.console.{Console, putStrLn}
import zio.interop.catz._
import zio.stream.ZStream

Expand Down

0 comments on commit c007126

Please sign in to comment.