Skip to content

Commit

Permalink
Adhere to specification for schema extension (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
niodice authored Dec 12, 2024
1 parent 9b88896 commit f072518
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/caliban/rendering/DocumentRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ object DocumentRenderer extends Renderer[Document] {
definition match {
case SchemaDefinition(directives, query, mutation, subscription, description) =>
val hasTypes = query.nonEmpty || mutation.nonEmpty || subscription.nonEmpty
val isExtension = directives.nonEmpty && !hasTypes
val isExtension =
(!hasTypes && directives.nonEmpty) || (query.isEmpty && (mutation.nonEmpty || subscription.nonEmpty))
var first = true

def renderOp(name: String, op: Option[String]): Unit =
Expand Down
30 changes: 29 additions & 1 deletion core/src/test/scala/caliban/RenderingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import caliban.schema.Annotations.GQLOneOfInput
import caliban.schema.Schema.auto._
import caliban.schema.ArgBuilder.auto._
import caliban.schema.{ ArgBuilder, Schema }
import zio.stream.ZStream
import zio.{ IO, ZIO }
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -101,14 +102,41 @@ object RenderingSpec extends ZIOSpecDefault {
assertTrue(graphQL(InvalidSchemas.resolverEmpty).render.trim == "")
},
test(
"it should render a schema extension with schema directives even if no queries, mutations, or subscription"
"it should render a schema extension with directives only"
) {
val renderedType =
graphQL(InvalidSchemas.resolverEmpty, schemaDirectives = List(SchemaDirectives.Link)).render.trim
assertTrue(
renderedType == """extend schema @link(url: "https://example.com", import: ["@key", {name: "@provides", as: "@self"}])"""
)
},
test("it should render a schema extension with directives and a mutation") {
val resolver = RootResolver(
Option.empty[Unit],
Some(MutationIO(_ => ZIO.unit)),
Option.empty[Unit]
)
val renderedType = graphQL(resolver, schemaDirectives = List(SchemaDirectives.Link)).render.trim
assertTrue(renderedType.startsWith("extend schema"))
},
test("it should render a schema extension with directives and a subscription") {
val resolver = RootResolver(
Option.empty[Unit],
Option.empty[Unit],
Some(SubscriptionIO(ZStream.empty))
)
val renderedType = graphQL(resolver, schemaDirectives = List(SchemaDirectives.Link)).render.trim
assertTrue(renderedType.startsWith("extend schema"))
},
test("it should render a schema extension with a subscription and mutation but no directives") {
val resolver = RootResolver(
Option.empty[Unit],
Some(MutationIO(_ => ZIO.unit)),
Some(SubscriptionIO(ZStream.empty))
)
val renderedType = graphQL(resolver).render.trim
assertTrue(renderedType.startsWith("extend schema"))
},
test("it should render object arguments in type directives") {
val testType = __Type(
__TypeKind.OBJECT,
Expand Down

0 comments on commit f072518

Please sign in to comment.