From e6ce4aa2a39702264d10fe6472ca150db415bf8a Mon Sep 17 00:00:00 2001 From: kyri-petrou <67301607+kyri-petrou@users.noreply.github.com> Date: Wed, 8 Nov 2023 18:57:28 +1100 Subject: [PATCH] Fix #1989 & #1990 (#1991) --- .../caliban/schema/SchemaDerivation.scala | 8 +- .../schema/SchemaDerivationIssuesSpec.scala | 97 ++++++++++++++++++- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala index d21f578ca5..47b14939a1 100644 --- a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala @@ -99,12 +99,12 @@ trait CommonSchemaDerivation { private lazy val members = _members.toVector // Vector's .apply is O(1) vs List's O(N) - private lazy val subTypes = members.map { case (label, subTypeAnnotations, schema) => + private lazy val subTypes = members.map { (label, subTypeAnnotations, schema) => (label, schema.toType_(), subTypeAnnotations) - }.sortBy { case (label, _, _) => label }.toList + }.sortBy(_._1).toList private lazy val isEnum = subTypes.forall { (_, t, _) => - t.allFields.isEmpty && t.allInputFields.isEmpty + unpackLeafTypes(t).foldLeft(true)((acc, t) => acc && t.allFields.isEmpty && t.allInputFields.isEmpty) } private lazy val isInterface = annotations.exists { @@ -259,7 +259,7 @@ trait CommonSchemaDerivation { getDescription(annotations), subTypes.collect { case (name, __Type(_, _, description, _, _, _, _, _, _, _, _, _), annotations) => __EnumValue( - name, + getName(annotations, name), description, getDeprecatedReason(annotations).isDefined, getDeprecatedReason(annotations), diff --git a/core/src/test/scala/caliban/schema/SchemaDerivationIssuesSpec.scala b/core/src/test/scala/caliban/schema/SchemaDerivationIssuesSpec.scala index 07d7cc3356..e5c7643534 100644 --- a/core/src/test/scala/caliban/schema/SchemaDerivationIssuesSpec.scala +++ b/core/src/test/scala/caliban/schema/SchemaDerivationIssuesSpec.scala @@ -1,6 +1,6 @@ package caliban.schema -import caliban.schema.Annotations.{ GQLDescription, GQLInterface, GQLUnion } +import caliban.schema.Annotations.{ GQLDescription, GQLInterface, GQLName, GQLUnion } import zio.Chunk import zio.test.ZIOSpecDefault import zio.test._ @@ -101,6 +101,45 @@ object SchemaDerivationIssuesSpec extends ZIOSpecDefault { | param: MyInterface! |}""".stripMargin ) + }, + test("i1989") { + import i1989._ + + assertTrue( + schema == + """schema { + | query: Queries + |} + | + |union Level1 = Level3 + | + |type Level3 { + | value: Boolean! + |} + | + |type Queries { + | level: Level1! + |}""".stripMargin + ) + }, + test("i1990") { + import i1990._ + + assertTrue( + schema == + """schema { + | query: Queries + |} + | + |enum PrefixOperator { + | PrefixOperatorGreaterThan + | PrefixOperatorLessThan + |} + | + |type Queries { + | op: PrefixOperator! + |}""".stripMargin + ) } ) } @@ -200,3 +239,59 @@ private object i1977 { graphQL(RootResolver(queries)) }.render } + +private object i1989 { + sealed trait Level1 + object Level1 { + implicit val schema: Schema[Any, Level1] = Schema.gen + + sealed trait Level2 extends Level1 + object Level2 { + implicit val schema: Schema[Any, Level2] = Schema.gen + + case class Level3(value: Boolean) extends Level2 + object Level3 { + implicit val schema: Schema[Any, Level3] = Schema.gen + } + } + } + + case class Queries(level: Level1) + object Queries { + implicit val schema: Schema[Any, Queries] = Schema.gen + } + + val schema = { + val queries = Queries(level = Level1.Level2.Level3(false)) + caliban.graphQL(RootResolver(queries)) + }.render +} + +object i1990 { + @GQLName("PrefixOperator") + sealed trait Operator + object Operator { + @GQLName("PrefixOperatorGreaterThan") + case object GreaterThan extends Operator { + implicit val schema: Schema[Any, GreaterThan.type] = Schema.gen + } + + @GQLName("PrefixOperatorLessThan") + case object LessThan extends Operator { + implicit val schema: Schema[Any, LessThan.type] = Schema.gen + } + + implicit val schema: Schema[Any, Operator] = Schema.gen + } + + case class Queries(op: Operator) + + object Queries { + implicit val schema: Schema[Any, Queries] = Schema.gen + } + + val schema = { + val queries = Queries(op = Operator.LessThan) + caliban.graphQL(RootResolver(queries)) + }.render +}