Skip to content

Bringing Akka-HTTP, Circe and GraphQL (with Sangria) together!

License

Notifications You must be signed in to change notification settings

Sebruck/akka-http-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Akka HTTP ➕ Sangria GraphQL ➕ circe = 💖

This library allows to easily create a Sangria GraphQL server based on Akka HTTP and circe.

Supported Scala Versions

The library is published for scala 2.12 and 2.13.

Features

  • executing GraphQL queries via GET & POST
  • Anti CORS measures:
    • Decline mutations via GET
    • POST requests accept only content types application/json and application/graphql
  • Serve playground.html (if configured) on GET with Accepts: text/html

Usage

Add the following dependency to your sbt project

"com.github.sebruck" %% "akka-http-graphql" % "0.2.0"
  import akka.actor.ActorSystem
  import akka.http.scaladsl.Http
  import akka.stream.ActorMaterializer
  import com.sebruck.akka.http.graphql.GraphEndpoint
  import sangria.execution.deferred.DeferredResolver
  import sangria.schema._

  import scala.concurrent.ExecutionContextExecutor
  import scala.util.{Failure, Success}

  implicit val actorSystem: ActorSystem     = ActorSystem()
  implicit val mat: ActorMaterializer       = ActorMaterializer()
  implicit val ec: ExecutionContextExecutor = actorSystem.dispatcher

  // Define Schema
  val Query = ObjectType(
    "Query",
    fields[Unit, Unit](
      Field("test", StringType, resolve = _ => "Hello!")
    )
  )

  val MySchema = Schema(Query)

  // Initialise Akka Http Endpoint
  val endpoint = GraphEndpoint(schema = MySchema,
                               context = (),
                               deferredResolver = DeferredResolver.empty,
                               graphQLPath = "graphql",
                               graphQLPlaygroundResourcePath = Some("playground.html"))

  // Start server
  Http().bindAndHandle(endpoint.route, "127.0.0.1", 8080).onComplete {
    case Success(binding) =>
      println(s"Bound to $binding")
    case Failure(exception) => throw exception
  }

If you have the GraphQL Playground in your resources directory, you can now open http://localhost:8080/graphql in your browser and play with your freshly created GraphQL API!

Contributing

Contributions are very welcome!

About

Bringing Akka-HTTP, Circe and GraphQL (with Sangria) together!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published