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

Improve interface field description #1976

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -275,10 +275,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,5 +1,6 @@
package caliban.schema

import caliban.schema.Annotations.{ GQLDescription, GQLInterface }
import zio.Chunk
import zio.test.ZIOSpecDefault
import zio.test._
Expand All @@ -10,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 All @@ -41,6 +37,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 @@ -81,5 +111,23 @@ 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
}
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