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 unsafeRunServer method #2050

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 46 additions & 1 deletion adapters/quick/src/main/scala/caliban/quick/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package caliban

import caliban.Configurator.ExecutionConfiguration
import zio._
import zio.http._
import zio.{ RIO, Trace, ZIO }

package object quick {

Expand Down Expand Up @@ -68,6 +68,51 @@ package object quick {
trace: Trace
): ZIO[R, CalibanError.ValidationError, RequestHandler[R, Response]] =
gql.interpreter.map(QuickAdapter(_).configure(config).handler)

/**
* Unsafe API which allows running the server impurely
*/
def unsafe: UnsafeApi[R] = gql.interpreterEither.fold(throw _, new UnsafeApi(_))
}

final class UnsafeApi[R](
interpreter: GraphQLInterpreter[R, Any],
executionConfig: ExecutionConfiguration = ExecutionConfiguration()
) {

/**
* Convenience method for impurely running the server.
*
* Useful for scripts / demos / showing off Caliban to your colleagues etc.
*
* Note that in order to call this method, you need to provide all dependencies required by the GraphQL environment. You can do that by passing a `ZLayer` to [[provideLayer]].
*/
def runServer(
port: Int = 8080,
apiPath: String = "/graphql",
graphiqlPath: Option[String] = Some("/graphiql"),
uploadPath: Option[String] = None
)(implicit trace: Trace, ev: R =:= Any): Unit = {
implicit val tag: Tag[R] = ev.substituteContra(Tag[Any])
val bootstrap = ev.substituteContra(ZLayer.empty)
val run: RIO[R, Nothing] =
QuickAdapter(interpreter)
.configure(executionConfig)
.runServer(port, apiPath, graphiqlPath, uploadPath)

ZIOApp(run, bootstrap).main(Array.empty)
}

def provideLayer(layer: ZLayer[Any, Any, R]): UnsafeApi[Any] =
new UnsafeApi(interpreter.provideLayer(layer))

def provideSomeLayer[R0](layer: ZLayer[R0, Any, R])(implicit tag: Tag[R]): UnsafeApi[R0] =
new UnsafeApi(interpreter.provideSomeLayer(layer))

def configure(cfg: ExecutionConfiguration): UnsafeApi[R] =
new UnsafeApi(interpreter, cfg)

def configureWith(cfg: ExecutionConfiguration => ExecutionConfiguration): UnsafeApi[R] =
new UnsafeApi(interpreter, cfg(executionConfig))
}
}
8 changes: 3 additions & 5 deletions core/src/main/scala/caliban/GraphQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ trait GraphQL[-R] { self =>
* adding some middleware around the query execution.
* Fails with a [[caliban.CalibanError.ValidationError]] if the schema is invalid.
*/
final def interpreter(implicit trace: Trace): IO[ValidationError, GraphQLInterpreter[R, CalibanError]] = {
val i = cachedInterpreter
ZIO.fromEither(i)
}
final def interpreter(implicit trace: Trace): IO[ValidationError, GraphQLInterpreter[R, CalibanError]] =
ZIO.fromEither(interpreterEither)

private lazy val cachedInterpreter =
private[caliban] final lazy val interpreterEither =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: I'm actually thinking we should change the interpreter method to return an Either instead. I think it can be quite annoying to users to have to deal with the ZIO effect unnecessarily

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't an Either more annoying? With ZIO you can just call it in your current for-comprehension, with Either I feel it will make people have to call ZIO.fromEither.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll simplify things a lot for non-zio apps. I think this is the only method required for wiring the api that returns a ZIO effect

Validator.validateSchemaEither(schemaBuilder).map { schema =>
new GraphQLInterpreter[R, CalibanError] {
private val rootType =
Expand Down