Created
January 24, 2018 12:14
-
-
Save gandalftheFFFFFF/44784526fd8a621657e1687c55e582f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import SchemaDefinition.TestClass | |
import akka.actor.ActorSystem | |
import sangria.macros.derive._ | |
import sangria.schema._ | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | |
import akka.http.scaladsl.model.StatusCodes._ | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server._ | |
import akka.stream.ActorMaterializer | |
import sangria.execution.deferred.DeferredResolver | |
import sangria.execution.{ErrorWithResolver, Executor, QueryAnalysisError} | |
import sangria.marshalling.sprayJson._ | |
import sangria.parser.QueryParser | |
import spray.json._ | |
import scala.util.{Failure, Success} | |
object Server extends App { | |
implicit val system = ActorSystem("sangria-server") | |
implicit val materializer = ActorMaterializer() | |
import system.dispatcher | |
val route: Route = | |
(post & path("graphql")) { | |
entity(as[JsValue]) { requestJson ⇒ | |
val JsObject(fields) = requestJson | |
val JsString(query) = fields("query") | |
val operation = fields.get("operationName") collect { | |
case JsString(op) ⇒ op | |
} | |
val vars = fields.get("variables") match { | |
case Some(obj: JsObject) ⇒ obj | |
case _ ⇒ JsObject.empty | |
} | |
QueryParser.parse(query) match { | |
// query parsed successfully, time to execute it! | |
case Success(queryAst) ⇒ | |
complete(Executor.execute(SchemaDefinition.TestSchema, queryAst, new Data, | |
variables = vars) | |
.map(OK → _) | |
.recover { | |
case error: QueryAnalysisError ⇒ BadRequest → error.resolveError | |
case error: ErrorWithResolver ⇒ InternalServerError → error.resolveError | |
}) | |
// can't parse GraphQL query, return error | |
case Failure(error) ⇒ | |
complete(BadRequest, JsObject("error" → JsString(error.getMessage))) | |
} | |
} | |
} ~ | |
get { | |
getFromResource("graphiql.html") | |
} | |
Http().bindAndHandle(route, "0.0.0.0", sys.props.get("http.port").fold(8080)(_.toInt)) | |
} | |
object SchemaDefinition { | |
import spray.json._ | |
import spray.json.DefaultJsonProtocol._ | |
implicit val implicitititit = DefaultJsonProtocol.jsonFormat1(TestClass) | |
case class TestClass(name: String) | |
val TestObject = deriveObjectType[Unit, TestClass]() | |
val TestInputObject = deriveInputObjectType[TestClass]( | |
RenameInputField("name", "newName")) | |
val TestInputObject2 = InputObjectType | |
val TestArg = Argument("testArg", TestInputObject) | |
val Query = ObjectType( | |
"Query", fields[Data, Unit]( | |
Field("testquery", StringType, | |
arguments = Nil, | |
resolve = (ctx) ⇒ ctx.ctx.getTestClassData | |
))) | |
val Mutation = ObjectType( | |
"Mutation", fields[Data, Unit]( | |
Field("mutate", StringType, | |
arguments = TestArg :: Nil, | |
resolve = c ⇒ c.ctx.test(c.arg(TestArg)) | |
))) | |
val TestSchema = Schema(Query, Some(Mutation)) | |
} | |
class Data { | |
def getTestClassData: String = "Testing..." | |
def test(tc: TestClass): String = tc.toString.replaceAll("n", "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment