sangria-graphql / sangria-ion   2.0.1

Apache License 2.0 Website GitHub

Sangria Amazon Ion marshalling

Scala versions: 3.x 2.13 2.12 2.11

Sangria Amazon Ion marshalling.

Continuous Integration Coverage Status Maven Central License Join the chat at https://gitter.im/sangria-graphql/sangria

SBT Configuration:

libraryDependencies += "org.sangria-graphql" %% "sangria-ion" % "<latest version>"

Example

Here is small example of how you can use it, which also demonstrates custom scalar values that are natively supported by Ion.

First let's define the scalar values:

val dateFormat = new SimpleDateFormat("yyyy-MM-dd")

case object DateCoercionViolation extends ValueCoercionViolation("Date value expected")
case object BinaryCoercionViolation extends ValueCoercionViolation("Binary data is not supported as input")

def parseDate(s: String) = Try(dateFormat.parse(s)) match {
  case Success(d)  Right(d)
  case Failure(error)  Left(DateCoercionViolation)
}

val DateType = ScalarType[Date]("Date",
  coerceOutput = (d, caps) 
    if (caps.contains(DateSupport)) d
    else dateFormat.format(d),
  coerceUserInput = {
    case s: String  parseDate(s)
    case _  Left(DateCoercionViolation)
  },
  coerceInput = {
    case ast.StringValue(s, _)  parseDate(s)
    case _  Left(DateCoercionViolation)
  })

val BlobType = ScalarType[Array[Byte]]("Blob",
  coerceOutput = (d, _)  d,
  coerceUserInput = _  Left(BinaryCoercionViolation),
  coerceInput = _  Left(BinaryCoercionViolation))

val ClobType = ScalarType[Array[Byte]]("Clob",
  coerceOutput = (d, _)  d,
  coerceUserInput = _  Left(BinaryCoercionViolation),
  coerceInput = _  Left(BinaryCoercionViolation),
  scalarInfo = Set(IonClobScalar))

Please notice that Date type produces java.util.Date only when this capability is supported by the marshaller. Otherwise it produces a String alternative. Clob type also instructs marshaller to to use Ion clob type instead of blob for a byte array.

In order to use Ion marshalling, you also need an implicit instance of IonSystem in scope:

import sangria.marshalling.ion._

implicit val ionSystem = IonSystemBuilder.standard().build()

val result: Future[IonValue] = Executor.execute(schema, query)

Now you should be able to write IonValue to a binary or a text format.

License

sangria-ion is licensed under Apache License, Version 2.0.