Skip to content

Instantly share code, notes, and snippets.

View OlegIlyenko's full-sized avatar

ΘLΞG OlegIlyenko

View GitHub Profile
@OlegIlyenko
OlegIlyenko / OptionsExample.scala
Created June 21, 2015 12:42
Example of akka-http server with generic OPTIONS method handling
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.Http
import akka.http.scaladsl.server._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
object OptionsMethod extends App {
@OlegIlyenko
OlegIlyenko / NotNothing.scala
Created January 19, 2014 18:35
Advanced Type Constraints with Type Classes.
import scala.reflect.runtime.universe.TypeTag
import scala.annotation.implicitNotFound
@implicitNotFound("Sorry, type inference was unable to figure out the type. You need to provide it explicitly.")
trait NotNothing[T]
object NotNothing {
private val evidence: NotNothing[Any] = new Object with NotNothing[Any]
implicit def notNothingEvidence[T](implicit n: T =:= T): NotNothing[T] =
@OlegIlyenko
OlegIlyenko / DateType.scala
Created August 19, 2015 08:23
GraphQL scalar DateType implemented with sangria
case object DateCoercionViolation extends ValueCoercionViolation("Date value expected")
def parseDate(s: String) = Try(new DateTime(s, DateTimeZone.UTC)) match {
case Success(date) => Right(date)
case Failure(_) => Left(DateCoercionViolation)
}
val DateTimeType = ScalarType[DateTime]("DateTime",
coerceOutput = date => ast.StringValue(ISODateTimeFormat.dateTime().print(date)),
coerceUserInput = {
@OlegIlyenko
OlegIlyenko / ApolloTracingExtension.scala
Last active May 15, 2018 20:35
An example of the apollo tracing GraphQL extension with Sangria
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.util.concurrent.ConcurrentLinkedQueue
import sangria.ast._
import sangria.execution._
import sangria.schema.Context
import sangria.marshalling.queryAst._
import sangria.renderer.SchemaRenderer
@OlegIlyenko
OlegIlyenko / CovertJson.scala
Created April 19, 2018 19:50
useful for exception handlers
import sangria.marshalling.MarshallingUtil._
import sangria.marshalling.circe._
val marchaller: ResultMarshaller = ??? // provided in ExceptionHandler
val json: Json = ??? // your custom json
implicit val mForType = SimpleResultMarshallerForType[marchaller.Node](marchaller)
val converted: marchaller.Node = json.convertMarshaled[marchaller.Node]
$(function (global) {
/**
* This GraphiQL example illustrates how to use some of GraphiQL's props
* in order to enable reading and updating the URL parameters, making
* link sharing of queries a little bit easier.
*
* This is only one example of this kind of feature, GraphiQL exposes
* various React params to enable interesting integrations.
*/
var graphiql = null
@OlegIlyenko
OlegIlyenko / CirceJsonPath.scala
Last active March 20, 2018 00:29
Stefan Goessner JsonPath (http://goessner.net/articles/JsonPath/) implementation for Circe
// For build.sbt:
//
// libraryDependencies ++= Seq(
// "io.circe" %% "circe-core" % "0.9.2",
// "io.circe" %% "circe-parser" % "0.9.2",
// "com.jayway.jsonpath" % "json-path" % "2.3.0")
import com.jayway.jsonpath.{InvalidJsonException, JsonPathException, Configuration, JsonPath, TypeRef}
import com.jayway.jsonpath.spi.json.JsonProvider
import com.jayway.jsonpath.spi.mapper.MappingProvider
@OlegIlyenko
OlegIlyenko / renderSchemaWithLegacyDescriptions.scala
Last active December 19, 2017 17:01
Function to render the schema with legacy comment-based descriptions
import sangria.ast
import sangria.ast.AstVisitor
import sangria.schema._
import sangria.renderer.{QueryRenderer, QueryRendererConfig, SchemaFilter}
import sangria.visitor.VisitorCommand
def renderSchemaWithLegacyDescriptions(schema: Schema[_, _], filter: SchemaFilter = SchemaFilter.withoutSangriaBuiltIn, config: QueryRendererConfig = QueryRenderer.Pretty) = {
def commentDescription(node: ast.WithDescription) =
node.description.toVector.flatMap(sv ⇒ sv.value.split("\\r?\\n").toVector.map(ast.Comment(_)))
val UUIDType = ScalarAlias[UUID, String](StringType,
toScalar = _.toString,
fromScalar = idString ⇒ try Right(UUID.fromString(idString)) catch {
case _: IllegalArgumentException ⇒ Left(IDViolation)
})
case object IDViolation extends ValueCoercionViolation("Invalid ID")
class FieldMetrics(reporter: MetricsReporter)
extends Middleware[Any]
with MiddlewareAfterField[Any]
with MiddlewareErrorField[Any] {
type QueryVal = ()
type FieldVal = Long
def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = ()
def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = ()