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 6 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
28 changes: 28 additions & 0 deletions core/src/test/scala-2/caliban/Scala2SpecificSpec.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package caliban

import caliban.CalibanError.ExecutionError
import caliban.GraphQL._
import caliban.InputValue.ObjectValue
import caliban.TestUtils._
import caliban.Value.{ NullValue, StringValue }
import caliban.introspection.adt.__DeprecatedArgs
import caliban.schema.ArgBuilder
import caliban.schema.SchemaSpec.introspect
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -39,6 +43,30 @@ object Scala2SpecificSpec extends DefaultRunnableSpec {
"""{"events":[{"organizationId":7,"title":"Frida Kahlo exhibition"}],"painters":[{"name":"Claude Monet","movement":"Impressionism"}]}"""
)
)
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test is same for Scala 2 and Scala 3, you can simply add it in the scala folder, no need to duplicate it. These files are just for Scala2 or Scala3 specific features (like Scala 3 enums).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I though it had to be there for it to run for each version - I'll move the test.

test("Scala 2 buildMissing") {
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")))))
}
)
}
28 changes: 28 additions & 0 deletions core/src/test/scala-3/caliban/Scala3SpecificSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package caliban

import caliban.CalibanError.ExecutionError
import caliban.GraphQL._
import caliban.InputValue.ObjectValue
import caliban.Value.{ NullValue, StringValue }
import caliban.schema.Annotations.GQLInterface
import caliban.schema.ArgBuilder
import zio.test.Assertion._
import zio.test._
import zio.test.environment.TestEnvironment
Expand Down Expand Up @@ -69,6 +73,30 @@ object Scala3SpecificSpec extends DefaultRunnableSpec {
assertM(interpreter.flatMap(_.execute(query)).map(_.data.toString))(
equalTo("""{"item":{"a":1}}""")
)
},
test("Scala 3 buildMissing") {
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")))))
}
)
}