Skip to content

Instantly share code, notes, and snippets.

@OlegIlyenko
Created August 19, 2015 08:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegIlyenko/a9c0e52540ce7090abaf to your computer and use it in GitHub Desktop.
Save OlegIlyenko/a9c0e52540ce7090abaf to your computer and use it in GitHub Desktop.
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 = {
case s: String => parseDate(s)
case _ => Left(DateCoercionViolation)
},
coerceInput = {
case ast.StringValue(s, _) => parseDate(s)
case _ => Left(DateCoercionViolation)
})
@kvc-code
Copy link

That looks just the ticket. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment