Skip to content

Commit f072518

Browse files
authored
Adhere to specification for schema extension (#2433)
1 parent 9b88896 commit f072518

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

core/src/main/scala/caliban/rendering/DocumentRenderer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ object DocumentRenderer extends Renderer[Document] {
327327
definition match {
328328
case SchemaDefinition(directives, query, mutation, subscription, description) =>
329329
val hasTypes = query.nonEmpty || mutation.nonEmpty || subscription.nonEmpty
330-
val isExtension = directives.nonEmpty && !hasTypes
330+
val isExtension =
331+
(!hasTypes && directives.nonEmpty) || (query.isEmpty && (mutation.nonEmpty || subscription.nonEmpty))
331332
var first = true
332333

333334
def renderOp(name: String, op: Option[String]): Unit =

core/src/test/scala/caliban/RenderingSpec.scala

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import caliban.schema.Annotations.GQLOneOfInput
1717
import caliban.schema.Schema.auto._
1818
import caliban.schema.ArgBuilder.auto._
1919
import caliban.schema.{ ArgBuilder, Schema }
20+
import zio.stream.ZStream
2021
import zio.{ IO, ZIO }
2122
import zio.test.Assertion._
2223
import zio.test._
@@ -101,14 +102,41 @@ object RenderingSpec extends ZIOSpecDefault {
101102
assertTrue(graphQL(InvalidSchemas.resolverEmpty).render.trim == "")
102103
},
103104
test(
104-
"it should render a schema extension with schema directives even if no queries, mutations, or subscription"
105+
"it should render a schema extension with directives only"
105106
) {
106107
val renderedType =
107108
graphQL(InvalidSchemas.resolverEmpty, schemaDirectives = List(SchemaDirectives.Link)).render.trim
108109
assertTrue(
109110
renderedType == """extend schema @link(url: "https://example.com", import: ["@key", {name: "@provides", as: "@self"}])"""
110111
)
111112
},
113+
test("it should render a schema extension with directives and a mutation") {
114+
val resolver = RootResolver(
115+
Option.empty[Unit],
116+
Some(MutationIO(_ => ZIO.unit)),
117+
Option.empty[Unit]
118+
)
119+
val renderedType = graphQL(resolver, schemaDirectives = List(SchemaDirectives.Link)).render.trim
120+
assertTrue(renderedType.startsWith("extend schema"))
121+
},
122+
test("it should render a schema extension with directives and a subscription") {
123+
val resolver = RootResolver(
124+
Option.empty[Unit],
125+
Option.empty[Unit],
126+
Some(SubscriptionIO(ZStream.empty))
127+
)
128+
val renderedType = graphQL(resolver, schemaDirectives = List(SchemaDirectives.Link)).render.trim
129+
assertTrue(renderedType.startsWith("extend schema"))
130+
},
131+
test("it should render a schema extension with a subscription and mutation but no directives") {
132+
val resolver = RootResolver(
133+
Option.empty[Unit],
134+
Some(MutationIO(_ => ZIO.unit)),
135+
Some(SubscriptionIO(ZStream.empty))
136+
)
137+
val renderedType = graphQL(resolver).render.trim
138+
assertTrue(renderedType.startsWith("extend schema"))
139+
},
112140
test("it should render object arguments in type directives") {
113141
val testType = __Type(
114142
__TypeKind.OBJECT,

0 commit comments

Comments
 (0)