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: Properly resolve interface types in RemoteSchema #1018

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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