Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Last active September 18, 2016 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefiorini/a245c6c2129b725b0ba7797109c7cffe to your computer and use it in GitHub Desktop.
Save joefiorini/a245c6c2129b725b0ba7797109c7cffe to your computer and use it in GitHub Desktop.
scalaVersion := "2.11.8"
scalacOptions ++= Seq(
"-feature",
"-target:jvm-1.8",
"-encoding", "UTF-8",
"-unchecked",
"-deprecation",
"-Xfuture",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard",
"-Ywarn-unused"
)
package fitwell.api
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model.StatusCodes._
import scala.concurrent.ExecutionContextExecutor
import sangria.parser.QueryParser
import sangria.execution.Executor
import sangria.ast.Document
import sangria.execution.{ErrorWithResolver, QueryAnalysisError, Executor}
import sangria.marshalling.circe._
import io.circe.{JsonObject, Json}
import de.heikoseeberger.akkahttpcirce.CirceSupport._
import scala.util.{Success, Failure}
import fitwell.api.graphql.GraphQLRequest
import fitwell.api.schema.{Schema, _}
trait Routes {
implicit val dispatcher: ExecutionContextExecutor
import io.circe.generic.auto._
val routes = {
path("ping") {
complete("PONG!")
} ~
path("graphql") {
decodeRequest {
entity(as[GraphQLRequest]) { request =>
QueryParser.parse(request.query) match {
case Success(document: Document) =>
val variables = request.variables match {
case Some(vars) => vars
case None => Json.fromJsonObject(JsonObject.empty)
}
complete(Executor.execute(Schema.adminSchema, document,
root = new QueryObject(),
variables = variables,
operationName = request.operationName)
.map(OK → _)
.recover {
case error: QueryAnalysisError ⇒ BadRequest → error.resolveError
case error: ErrorWithResolver ⇒ InternalServerError → error.resolveError
})
case Failure(error) ⇒
complete(BadRequest -> JsonObject.fromMap(Map("error" -> Json.fromString(error.getMessage))))
}
}
}
} ~
path("graphiql") {
getFromResource("graphiql.html")
}
}
}
package fitwell.api.schema
import sangria.ast
import sangria.macros.derive._
case class Greeter(message: String)
import sangria.schema.{Schema}
import sangria.marshalling.circe._
class QueryObject {
@GraphQLField
def greet: String = "doo"
}
object AdminSchema {
// implicit val GreeterType = deriveObjectType[Unit, Greeter](
// ObjectTypeName("Greeter"),
// ObjectTypeDescription("One who says Hi")
// )
val Query = deriveObjectType[Unit, QueryObject](
ObjectTypeName("Root"),
ObjectTypeDescription("The root of the queries")
)
val adminSchema = Schema(Query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment