Skip to content

Commit

Permalink
Fix #1989 & #1990 (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Nov 8, 2023
1 parent be0244a commit e6ce4aa
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
8 changes: 4 additions & 4 deletions core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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
)
}
)
}
Expand Down Expand Up @@ -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
}

0 comments on commit e6ce4aa

Please sign in to comment.