From 069f5a38641cbcd32a3b36bc7394efd9c5bcb036 Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Sat, 11 May 2024 19:02:45 +0900 Subject: [PATCH] Add test to ensure semantic of .optional for custom schemas --- .../scala/caliban/schema/OptionalSpec.scala | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 core/src/test/scala/caliban/schema/OptionalSpec.scala diff --git a/core/src/test/scala/caliban/schema/OptionalSpec.scala b/core/src/test/scala/caliban/schema/OptionalSpec.scala new file mode 100644 index 000000000..84ae6e4f1 --- /dev/null +++ b/core/src/test/scala/caliban/schema/OptionalSpec.scala @@ -0,0 +1,57 @@ +package caliban.schema + +import caliban.{ graphQL, RootResolver } +import zio._ +import zio.test._ + +object OptionalSpec extends ZIOSpecDefault { + import caliban.schema.Schema._ + + override def spec = suite("OptionalSpec")( + test("Semantic of Schema.optional is kept consistent across various nullable/canFail combinations") { + val expected = + """schema { + | query: Query + |} + | + |type Query { + | a: String! + | b: String + | c: String + | d: String + |}""".stripMargin + + implicit def wrapperSchema[A](implicit ev: Schema[Any, A]): Schema[Any, Wrapper[A]] = + new Schema[Any, Wrapper[A]] { + @annotation.nowarn + override def optional = ev.optional + def toType(isInput: Boolean, isSubscription: Boolean) = ev.toType_(isInput, isSubscription) + def resolve(value: Wrapper[A]): Step[Any] = + ev.resolve(value.value) + } + + implicit def querySchema: Schema[Any, Query] = Schema.gen[Any, Query] + + val resolver = RootResolver( + Query( + Wrapper("a"), + Wrapper(Some("b")), + Wrapper(ZIO.succeed("c")), + Wrapper(ZIO.succeed(Some("d"))) + ) + ) + val gql = graphQL(resolver) + + assertTrue(gql.render == expected) + } + ) + + case class Wrapper[A](value: A) + + case class Query( + a: Wrapper[String], + b: Wrapper[Option[String]], + c: Wrapper[Task[String]], + d: Wrapper[Task[Option[String]]] + ) +}