logo

ahql

Akka Http Query Library, minimal GraphQL client and server exposing as a set of akka-http utilities.

Setup

Latest Version: 0.2.4

"io.github.d-exclaimation" %% "ahql" % latestVersion

Usage/Examples

Server Example

Using a Server instance

object Main extends SprayJsonSupport {
  implicit val system: ActorSystem[Nothing] =
    ActorSystem(Behaviors.empty, "--")

  val gqlServer: Ahql.Server[Context, Unit] =
    Ahql.createServer[Context, Unit](schema, (),
      httpMethodStrategy = HttpMethodStrategy.queryOnlyGet
    )

  val route: Route = path("graphql") {
    optionalHeaderValueByName("Authorization") { auth =>
      val context = Context(auth)
      gqlServer.applyMiddleware(context)
    }
  }
}

Using a shorthand

object Main extends SprayJsonSupport {
  implicit val system: ActorSystem[Nothing] =
    ActorSystem(Behaviors.empty, "--")

  val route: Route = path("graphql") {
    optionalHeaderValueByName("Authorization") { auth =>
      val context = Context(auth)
      Ahql.applyMiddleware[Context, Unit](schema, context, (),
        httpMethodStrategy = HttpMethodStrategy.queryOnlyGet
      )
    }
  }
}

Both will gave out two endpoints

POST: ".../graphql"
GET: ".../graphql"
Client Example

Using a Client instance

object Main extends SprayJsonSupport {
  implicit val system: ActorSystem[Nothing] =
    ActorSystem(Behaviors.empty, "--")

  val gqlClient: Ahql.Client =
    Ahql.createClient("http://localhost:4000/graphql")

  val query: ast.Document =
    graphql"""
    query {
      someField {
        nested1
        nested2
      }
    }
  """

  val GqlResponse(data, errors) = gqlClient
    .fetch(query = query,
      headers = headers.Authorization("Bearer token") :: Nil
    )
  // data: Option[JsObject]
  // errors: Option[Vector[JsObject]]
}

Using a shorthand

object Main extends SprayJsonSupport {
  implicit val system: ActorSystem[Nothing] =
    ActorSystem(Behaviors.empty, "--")

  val query: ast.Document =
    graphql"""
    query {
      someField {
        nested1
        nested2
      }
    }
  """

  val GqlResponse(data, errors) = Ahql
    .fetch(
      endpoint = "http://localhost:4000/graphql",
      query = query,
      headers = headers.Authorization("Bearer token") :: Nil
    )
  // data: Option[JsObject]
  // errors: Option[Vector[JsObject]]
}

Feedback

If you have any feedback, feel free to reach out through the issues tab or through my Twitter @d_exclaimation.