Skip to content

Commit

Permalink
fix(remote-schema): properly resolve interface types (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
frekw authored Aug 30, 2021
1 parent b12cfe2 commit ff9a1e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tools/src/main/scala/caliban/tools/RemoteSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,18 @@ object RemoteSchema {
private def toInterfaceType(
definition: Definition.TypeSystemDefinition.TypeDefinition.InterfaceTypeDefinition,
definitions: List[Definition.TypeSystemDefinition.TypeDefinition]
): __Type =
): __Type = {
val implementations = definitions.collect {
case t @ ObjectTypeDefinition(description, name, implements, directives, fields)
if implements.map(_.name).toSet.contains(definition.name) =>
toObjectType(t, definitions)
}

__Type(
kind = __TypeKind.INTERFACE,
name = Some(definition.name),
description = definition.description,
possibleTypes = Some(implementations),
fields = (args: __DeprecatedArgs) =>
if (definition.fields.nonEmpty)
Some(
Expand All @@ -162,6 +169,7 @@ object RemoteSchema {
else None,
directives = toDirectives(definition.directives)
)
}

private def toInputValue(
definition: Definition.TypeSystemDefinition.TypeDefinition.InputValueDefinition,
Expand Down
35 changes: 35 additions & 0 deletions tools/src/test/scala/caliban/tools/RemoteSchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import caliban.schema._
import zio._
import zio.test.Assertion._
import zio.test._
import schema.Annotations._
import caliban.Macros.gqldoc

object RemoteSchemaSpec extends DefaultRunnableSpec {
sealed trait EnumType extends Product with Serializable
Expand Down Expand Up @@ -57,6 +59,39 @@ object RemoteSchemaSpec extends DefaultRunnableSpec {
sdl = api.render
remoteSDL = remoteAPI.render
} yield assert(remoteSDL)(equalTo(sdl))
},
testM("properly resolves interface types") {
@GQLInterface
sealed trait Node

sealed trait Viewer
case class User(id: String, email: String) extends Node with Viewer
case class Superuser(id: String) extends Node with Viewer

case class Queries(
whoAmI: Node = User("1", "[email protected]")
)

val api = graphQL(RootResolver(Queries()))
val query = gqldoc("""
query {
whoAmI {
...on User {
email
}
...on Node {
id
}
}
}""")

for {
introspected <- SchemaLoader.fromCaliban(api).load
remoteSchema <- ZIO.fromOption(RemoteSchema.parseRemoteSchema(introspected))
remoteAPI <- ZIO.effectTotal(fromRemoteSchema(remoteSchema))
interpreter <- remoteAPI.interpreter
res <- interpreter.check(query)
} yield assert(res)(isUnit)
}
)

Expand Down

0 comments on commit ff9a1e1

Please sign in to comment.