Skip to content

Commit

Permalink
Add interpreterUnsafe method to GraphQL (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Mar 13, 2024
1 parent e2c4177 commit d231389
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/scala/caliban/GraphQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import caliban.validation.Validator
import caliban.wrappers.Wrapper
import caliban.wrappers.Wrapper._
import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{ Exit, IO, Trace, URIO, ZIO }
import zio.{ IO, Trace, URIO, ZIO }

/**
* A `GraphQL[-R]` represents a GraphQL API whose execution requires a ZIO environment of type `R`.
Expand Down Expand Up @@ -64,12 +64,21 @@ trait GraphQL[-R] { self =>
final def interpreter(implicit trace: Trace): IO[ValidationError, GraphQLInterpreter[R, CalibanError]] =
ZIO.fromEither(interpreterEither)

/**
* Impure variant of [[interpreterEither]] which throws schema validation errors. Useful for cases that the
* schema is known to be valid or when we're not planning on handling the error (e.g., during app startup)
*/
@throws[CalibanError.ValidationError]("if the schema is invalid")
final def interpreterUnsafe: GraphQLInterpreter[R, CalibanError] =
interpreterEither.fold(throw _, identity)

/**
* Creates an interpreter from your API. A GraphQLInterpreter is a wrapper around your API that allows
* adding some middleware around the query execution.
* Returns a `Left` containing the schema validation error if the schema is invalid.
*
* @see [[interpreter]] for a ZIO variant of this method that makes it easier to embed in ZIO applications
* @see [[interpreterUnsafe]] of an unsafe variant that will throw validation errors
*/
final lazy val interpreterEither: Either[ValidationError, GraphQLInterpreter[R, CalibanError]] =
Validator.validateSchemaEither(schemaBuilder).map { schema =>
Expand Down

0 comments on commit d231389

Please sign in to comment.