Skip to content

Instantly share code, notes, and snippets.

View OlegIlyenko's full-sized avatar

ΘLΞG OlegIlyenko

View GitHub Profile
import sangria.schema._
import sangria.ast
import sangria.macros._
import sangria.execution._
case class MyContext(originalQuery: ast.Document) {
def loadEntity(id: String, typeHints: Set[String]): Option[Identifiable] = {
println(typeHints)
None // your entity loagic goes here
}
@OlegIlyenko
OlegIlyenko / EndToEndGraphQL.scala
Created December 3, 2015 16:29
An example of GraphQL execution with Sangria which uses GraphQL input object notation for variables and execution results (no JSON is involved at any point)
import sangria.marshalling.queryAst._
import sangria.macros._
import sangria.ast
val query =
graphql"""
query MyQuery($$humanId: String!, $$episode: Episode!) {
hero(episode: $$episode) {
id, name, appearsIn
}
@OlegIlyenko
OlegIlyenko / GraphQLObjectNotation.scala
Created December 3, 2015 16:21
An example of using standalone GraphQL input object parsing and formatting with Sangria
import sangria.renderer.QueryRenderer
import sangria.macros._
import sangria.ast
val parsed: ast.Value =
graphqlInput"""
{
id: "1234345"
version: 2 # changed 2 times
@OlegIlyenko
OlegIlyenko / Event-stream based GraphQL subscriptions.md
Last active July 4, 2024 07:31
Event-stream based GraphQL subscriptions for real-time updates

In this gist I would like to describe an idea for GraphQL subscriptions. It was inspired by conversations about subscriptions in the GraphQL slack channel and different GH issues, like #89 and #411.

Conceptual Model

At the moment GraphQL allows 2 types of queries:

  • query
  • mutation

Reference implementation also adds the third type: subscription. It does not have any semantics yet, so here I would like to propose one possible semantics interpretation and the reasoning behind it.

@OlegIlyenko
OlegIlyenko / build.sbt
Last active September 21, 2015 18:08
SBT maven central publishing setup (after the setup just execute `sbt publishSigned`)
// In your project
// General stuff like name, description, version
// Publishing
publishMavenStyle := true
publishArtifact in Test := false
pomIncludeRepository := (_ => false)
publishTo := Some(
@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 / 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 / Parboiled2PredicateParser.scala
Created June 25, 2014 23:43
Parboiled2 Predicate Parser
import scala.annotation.switch
import org.parboiled2._
import scala.io.Source
import scala.util.{Failure, Success}
class PredicateParser(val input: ParserInput) extends Parser with CommonRules {
def And = Keyword("and")
def Or = Keyword("or")
def Not = Keyword("not")
// First you need to create module with actor system and actor bindings in it:
// IMPORTANT: `Actor` bindings should always be providers (bound with `toProvider` method,
// IMPORTANT: which will create new instances each time it gets injected)
implicit val module = new Module {
bind [ActorSystem] to ActorSystem("MySystem")
bind [GreetPrinter] toProvider new GreetPrinter
binding toProvider new GenericPrinter
bind [PrintStream] to Console.out
@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] =