<commit_before>package controllers

import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import graphql.GraphQL
import sangria.renderer.SchemaRenderer

object GraphQLController {
  val Routes: Route =
    path("graphql") {
      get {
        complete(HttpEntity("GET /graphql"))
      } ~
      post {
        complete(HttpEntity("POST /graphql"))
      }
    } ~
    (path("schema") & get) {
      complete(SchemaRenderer.renderSchema(GraphQL.Schema))
    }
}<commit_msg>Added executing graphql queries for GET and POST endpoints.<commit_after>package controllers

import sangria.ast.Document
import sangria.execution.{ErrorWithResolver, Executor, QueryAnalysisError}
import sangria.parser.{QueryParser, SyntaxError}
import sangria.renderer.SchemaRenderer
import sangria.marshalling.sprayJson._
import spray.json._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.marshalling.{ToResponseMarshallable, ToResponseMarshaller}
import akka.http.scaladsl.server.Route
import graphql.GraphQL
import javax.inject.Inject

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

object GraphQLController {

  @Inject implicit val ec: ExecutionContext = null

  val Routes: Route =
    path("graphql") {
      get {
        parameters('query, 'operation.?) { (query, operation) ⇒
          handleQuery(query, operation)
        }
      } ~
      post {
        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
          }

          handleQuery(query, operation, vars)
        }
      }
    } ~
    (path("schema") & get) {
      complete(SchemaRenderer.renderSchema(GraphQL.Schema))
    }

  @Inject
  private def handleQuery(query: String, operation: Option[String], variables: JsObject = JsObject.empty) = {
    QueryParser.parse(query) match {
      case Success(queryAst) =>
        complete(executeQuery(queryAst, operation, variables)
          .map(OK → _)
          .recover {
            case error: QueryAnalysisError ⇒ BadRequest → error.resolveError
            case error: ErrorWithResolver ⇒ InternalServerError → error.resolveError
          })
      case Failure(e: SyntaxError) => complete(BadRequest)
      case Failure(_) => complete(InternalServerError)
    }
  }

  private def executeQuery(queryAst: Document, operation: Option[String], variables: JsObject = JsObject.empty) = {
    Executor.execute(
      GraphQL.Schema,
      queryAst,
      (),
      (),
      operation,
      variables
    )
  }
}
{'fraction_non_alphanumeric': 0.10372608257804633, 'fraction_numerical': 0.0, 'mean_word_length': 3.0324763193504736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5389918', 'n_tokens_mistral': 879, 'n_tokens_neox': 864, 'n_words': 180}