Skip to content

Commit

Permalink
Improve interface field description (#1976)
Browse files Browse the repository at this point in the history
* Improve interface field description

* Fix Scala 2.12 and make ordering of interface fields deterministic
  • Loading branch information
kyri-petrou authored Nov 6, 2023
1 parent 6a7a56a commit e086cb9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
5 changes: 4 additions & 1 deletion core/src/main/scala-2/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,13 @@ trait CommonSchemaDerivation[R] {
.collect { case (_, list) =>
Types
.unify(list)
.flatMap(t => list.headOption.map(_.copy(`type` = () => t)))
.flatMap(t =>
list.headOption.map(_.copy(description = Types.extractCommonDescription(list), `type` = () => t))
)
}
.flatten
.toList
.sortBy(_.name)

makeInterface(
Some(getName(ctx)),
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,13 @@ trait CommonSchemaDerivation {
case (_, list) if list.lengthCompare(impl.size) == 0 =>
Types
.unify(list)
.flatMap(t => list.headOption.map(_.copy(`type` = () => t)))
.flatMap(t =>
list.headOption.map(_.copy(description = Types.extractCommonDescription(list), `type` = () => t))
)
}
.flatten
.toList
.sortBy(_.name)

makeInterface(
Some(getName(annotations, info)),
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/caliban/schema/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ object Types {
case _ => None
}

def extractCommonDescription(l: List[__Field]): Option[String] =
l.map(_.description).distinct match {
case desc :: Nil => desc
case _ => None
}

@tailrec
def same(t1: __Type, t2: __Type): Boolean =
if (t1.kind == t2.kind && t1.ofType.nonEmpty)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package caliban.schema

import caliban.schema.Annotations.{ GQLInterface, GQLUnion }
import caliban.schema.Annotations.{ GQLDescription, GQLInterface, GQLUnion }
import zio.Chunk
import zio.test.ZIOSpecDefault
import zio.test._
Expand All @@ -11,11 +11,6 @@ object SchemaDerivationIssuesSpec extends ZIOSpecDefault {
test("i1972 & i1973") {
import i1972_i1973._

val schema = {
val queries = Queries(param = Variable("temp"))
graphQL(RootResolver(queries))
}.render

assertTrue(
schema ==
"""schema {
Expand Down Expand Up @@ -72,6 +67,40 @@ object SchemaDerivationIssuesSpec extends ZIOSpecDefault {
| param: ASTParameter!
|}""".stripMargin
)
},
test("i1951") {
import i1951._

assertTrue(
schema ==
"""schema {
| query: Queries
|}
|
|interface MyInterface {
| "a"
| first: String!
| second: String!
|}
|
|type Bar implements MyInterface {
| "a"
| first: String!
| "c"
| second: String!
|}
|
|type Foo implements MyInterface {
| "a"
| first: String!
| "b"
| second: String!
|}
|
|type Queries {
| param: MyInterface!
|}""".stripMargin
)
}
)
}
Expand Down Expand Up @@ -113,7 +142,25 @@ private object i1972_i1973 {
val schema = {
val queries = Queries(param = Variable("temp"))
graphQL(RootResolver(queries))
}.render
}

private object i1951 {
import Schema.auto._

@GQLInterface
sealed trait MyInterface
object MyInterface {
case class Foo(@GQLDescription("a") first: String, @GQLDescription("b") second: String) extends MyInterface
case class Bar(@GQLDescription("a") first: String, @GQLDescription("c") second: String) extends MyInterface
}

case class Queries(param: MyInterface)

val schema = {
val queries = Queries(param = MyInterface.Foo("a", "b"))
graphQL(RootResolver(queries))
}.render
}

private object i1977 {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/caliban/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object SchemaSpec extends ZIOSpecDefault {
)
},
test("interface only take fields that return the same type") {
assertTrue(introspect[MyInterface].fields(__DeprecatedArgs()).toList.flatten.map(_.name) == List("c2", "c1"))
assertTrue(introspect[MyInterface].fields(__DeprecatedArgs()).toList.flatten.map(_.name) == List("c1", "c2"))
},
test("enum-like sealed traits annotated with GQLUnion") {
assert(introspect[EnumLikeUnion])(
Expand Down

0 comments on commit e086cb9

Please sign in to comment.