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

Fix withAdditionalTypes #1175

Merged
merged 3 commits into from
Nov 25, 2021
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
9 changes: 5 additions & 4 deletions core/src/main/scala/caliban/schema/RootSchemaBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ case class RootSchemaBuilder[-R](
)

def types: List[__Type] = {
val empty = additionalTypes
(query.map(_.opType).fold(empty)(collectTypes(_)) ++
mutation.map(_.opType).fold(empty)(collectTypes(_)) ++
subscription.map(_.opType).fold(empty)(collectTypes(_)))
val init = additionalTypes.foldLeft(List.empty[__Type]) { case (acc, t) => collectTypes(t, acc) }
(init ++
query.map(_.opType).fold(List.empty[__Type])(collectTypes(_, init)) ++
mutation.map(_.opType).fold(List.empty[__Type])(collectTypes(_, init)) ++
subscription.map(_.opType).fold(List.empty[__Type])(collectTypes(_, init)))
.groupBy(t => (t.name, t.kind, t.origin))
.flatMap(_._2.headOption)
.toList
Expand Down
34 changes: 34 additions & 0 deletions core/src/test/scala/caliban/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ object SchemaSpec extends DefaultRunnableSpec {
| a: String!
|}""".stripMargin
assertTrue(gql.render == expected)
},
test("Pass interface to withAdditionalTypes") {
@GQLInterface
sealed trait Interface

case class A(s: String) extends Interface
case class B(s: String) extends Interface

case class Query(a: A, b: B)

val interfaceType = Schema.gen[Any, Interface].toType_()

val gql = graphQL(RootResolver(Query(A("a"), B("b")))).withAdditionalTypes(List(interfaceType))
val expected = """schema {
| query: Query
|}
|
|interface Interface {
| s: String!
|}
|
|type A implements Interface {
| s: String!
|}
|
|type B implements Interface {
| s: String!
|}
|
|type Query {
| a: A!
| b: B!
|}""".stripMargin
assertTrue(gql.render == expected)
}
)

Expand Down