Skip to content

Commit

Permalink
Fix withAdditionalTypes (ghostdogpr#1175)
Browse files Browse the repository at this point in the history
* Fix withAdditionalTypes

* Add test

* Simplify FAQ
  • Loading branch information
ghostdogpr authored and Fluxx committed Jan 13, 2022
1 parent c92528a commit 4ca9b8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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
4 changes: 2 additions & 2 deletions vuepress/docs/faq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ case class B(s: String) extends Interface

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

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

val api = graphQL(RootResolver(Query(A("a"), B("b")))).withAdditionalTypes(List(interfaceSchema.toType_()))
val api = graphQL(RootResolver(Query(A("a"), B("b")))).withAdditionalTypes(List(interfaceType))
```

0 comments on commit 4ca9b8c

Please sign in to comment.