Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for distinguishing explicit and implicit null in ArgBuilders #929

Merged
merged 8 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait ArgBuilderDerivation {
input match {
case InputValue.ObjectValue(fields) =>
val label = p.annotations.collectFirst { case GQLName(name) => name }.getOrElse(p.label)
p.typeclass.build(fields.getOrElse(label, NullValue))
fields.get(label).fold(p.typeclass.buildMissing)(p.typeclass.build)
case value => p.typeclass.build(value)
}
}
Expand Down
14 changes: 6 additions & 8 deletions core/src/main/scala-3/caliban/schema/ArgBuilderDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ trait ArgBuilderDerivation {
new ArgBuilder[A] {
def build(input: InputValue): Either[ExecutionError, A] = {
fields.map { (label, _, builder) =>
val newInput =
input match {
case InputValue.ObjectValue(fields) =>
val finalLabel = annotations.getOrElse(label, Nil).collectFirst { case GQLName(name) => name }.getOrElse(label)
fields.getOrElse(finalLabel, NullValue)
case value => value
}
builder.build(newInput)
input match {
case InputValue.ObjectValue(fields) =>
val finalLabel = annotations.getOrElse(label, Nil).collectFirst { case GQLName(name) => name }.getOrElse(label)
fields.get(finalLabel).fold(builder.buildMissing)(builder.build)
case value => builder.build(value)
}
}.foldRight[Either[ExecutionError, Tuple]](Right(EmptyTuple)) { case (item, acc) =>
item match {
case error: Left[ExecutionError, Any] => error.asInstanceOf[Left[ExecutionError, Tuple]]
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/caliban/schema/ArgBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ trait ArgBuilder[T] { self =>
*/
def build(input: InputValue): Either[ExecutionError, T]

/**
* Builds a value of type `T` from a missing input value.
* By default, this delegates to [[build]], passing it NullValue.
* Fails with an [[caliban.CalibanError.ExecutionError]] if it was impossible to build the value.
*/
def buildMissing: Either[ExecutionError, T] = build(NullValue)

/**
* Builds a new `ArgBuilder` of `A` from an existing `ArgBuilder` of `T` and a function from `T` to `A`.
* @param f a function from `T` to `A`.
Expand Down
32 changes: 30 additions & 2 deletions core/src/test/scala/caliban/schema/ArgBuilderSpec.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package caliban.schema

import java.time.{ Instant, LocalDate, LocalDateTime, OffsetDateTime, OffsetTime, ZoneOffset, ZonedDateTime }

import caliban.Value.{ IntValue, StringValue }
import caliban.Value.{ IntValue, NullValue, StringValue }
import zio.test._
import Assertion._
import caliban.CalibanError.ExecutionError
import caliban.InputValue
import caliban.InputValue.ObjectValue

object ArgBuilderSpec extends DefaultRunnableSpec {
def spec = suite("ArgBuilder")(
Expand Down Expand Up @@ -56,6 +58,32 @@ object ArgBuilderSpec extends DefaultRunnableSpec {
isRight(equalTo(ZonedDateTime.ofInstant(Instant.ofEpochMilli(100), ZoneOffset.UTC)))
)
)
),
suite("buildMissing")(
test("works with derived case class ArgBuilders") {
sealed abstract class Nullable[+T]
case class SomeNullable[+T](t: T) extends Nullable[T]
case object NullNullable extends Nullable[Nothing]
case object MissingNullable extends Nullable[Nothing]

implicit def nullableArgBuilder[A](implicit ev: ArgBuilder[A]): ArgBuilder[Nullable[A]] =
new ArgBuilder[Nullable[A]] {
def build(input: InputValue): Either[ExecutionError, Nullable[A]] = input match {
case NullValue => Right(NullNullable)
case _ => ev.build(input).map(SomeNullable(_))
}

override def buildMissing: Either[ExecutionError, Nullable[A]] = Right(MissingNullable)
}

case class Wrapper(a: Nullable[String])

val deriviedAB = implicitly[ArgBuilder[Wrapper]]

assert(deriviedAB.build(ObjectValue(Map())))(equalTo(Right(Wrapper(MissingNullable)))) &&
assert(deriviedAB.build(ObjectValue(Map("a" -> NullValue))))(equalTo(Right(Wrapper(NullNullable)))) &&
assert(deriviedAB.build(ObjectValue(Map("a" -> StringValue("x")))))(equalTo(Right(Wrapper(SomeNullable("x")))))
}
)
)
}