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

Add renderSchema helper #1877

Merged
merged 1 commit into from
Sep 9, 2023
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
47 changes: 45 additions & 2 deletions core/src/main/scala/caliban/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import caliban.execution.Feature
import caliban.introspection.adt.__Directive
import caliban.parsing.adt.Directive
import caliban.parsing.SourceMapper
import caliban.parsing.adt.Definition.TypeSystemDefinition.SchemaDefinition
import caliban.parsing.adt.{ Directive, Document }
import caliban.rendering.DocumentRenderer
import caliban.schema.Types.collectTypes
import caliban.schema._
Expand Down Expand Up @@ -34,7 +37,7 @@ package object caliban {
)
val wrappers: List[Wrapper[R]] = Nil
val additionalDirectives: List[__Directive] = directives
val features = Set.empty
val features: Set[Feature] = Set.empty
}

/**
Expand All @@ -49,4 +52,44 @@ package object caliban {
*/
def renderWith[R, T](implicit schema: Schema[R, T]): String =
DocumentRenderer.typesRenderer.render(collectTypes(schema.toType_(), Nil))

/**
* Returns a string that renders the given schema into the GraphQL SDL.
*/
def renderSchema[Q, M, S](
directives: List[__Directive] = Nil,
schemaDirectives: List[Directive] = Nil,
schemaDescription: Option[String] = None
)(implicit querySchema: Schema[Any, Q], mutationSchema: Schema[Any, M], subscriptionSchema: Schema[Any, S]): String =
renderSchemaWith[Any, Q, M, S](directives, schemaDirectives, schemaDescription)

/**
* Returns a string that renders the given schema into the GraphQL SDL.
* This variant of the method allows specifying the environment type when it's not `Any`.
*/
def renderSchemaWith[R, Q, M, S](
directives: List[__Directive] = Nil,
schemaDirectives: List[Directive] = Nil,
schemaDescription: Option[String] = None
)(implicit querySchema: Schema[R, Q], mutationSchema: Schema[R, M], subscriptionSchema: Schema[R, S]): String = {
val hasQuery = querySchema.toType_().allFields.nonEmpty
val hasMutation = mutationSchema.toType_().allFields.nonEmpty
val hasSubscription = subscriptionSchema.toType_().allFields.nonEmpty
DocumentRenderer.render(
Document(
SchemaDefinition(
schemaDirectives,
if (hasQuery) querySchema.toType_().name else None,
if (hasMutation) mutationSchema.toType_().name else None,
if (hasSubscription) subscriptionSchema.toType_().name else None,
schemaDescription
) ::
(if (hasQuery) collectTypes(querySchema.toType_()).flatMap(_.toTypeDefinition) else Nil) ++
(if (hasMutation) collectTypes(mutationSchema.toType_()).flatMap(_.toTypeDefinition) else Nil) ++
(if (hasSubscription) collectTypes(subscriptionSchema.toType_()).flatMap(_.toTypeDefinition) else Nil) ++
directives.map(_.toDirectiveDefinition),
SourceMapper.empty
)
)
}
}
8 changes: 8 additions & 0 deletions core/src/test/scala/caliban/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ object SchemaSpec extends ZIOSpecDefault {
object schema extends GenericSchema[Env]
import schema.auto._
assertTrue(caliban.renderWith[Env, EnvironmentSchema].nonEmpty)
},
test("renderSchema can be called with a Schema where R is any") {
assertTrue(caliban.renderSchema[EffectfulFieldSchema, Unit, Unit]().nonEmpty)
},
test("renderSchema can be called with a Schema where R is not any") {
object schema extends GenericSchema[Env]
import schema.auto._
assertTrue(caliban.renderSchemaWith[Env, EnvironmentSchema, Unit, Unit]().nonEmpty)
}
)

Expand Down