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
Changes from 2 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
18 changes: 17 additions & 1 deletion adapters/quick/src/main/scala/caliban/quick/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package caliban

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

package object quick {

Expand Down Expand Up @@ -70,4 +70,20 @@ package object quick {
gql.interpreter.map(QuickAdapter(_).configure(config).handler)
}

implicit class GraphqlAnyServerOps(val gql: GraphQL[Any]) extends AnyVal {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How would the user do a provide for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@paulpdaniels Just to make sure I understood your question correctly, do you mean if they have a GraphQL[R] where R is not Any?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I think we only have provide for the interpreter or for the resulting ZIO but on this case you can't specify a graphql wide environment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps I'm wrong, but the way I see it is that if you're already propagating services via the ZIO environment, you're already familiar enough with the ZIO ecosystem to go for the normal object Main extends ZIOAppDefault way.

I don't think we should see this method as a substitute for it, but rather as a way for users that either never used ZIO before or just wanna prototype something without without too much fluff get started quickly. Having said that, in case we do think it's good to give this option to users, we can do something along these lines:

    def unsafeRunServer(
      port: Int = 8080,
      apiPath: String = "/graphql",
      graphiqlPath: Option[String] = Some("/graphiql"),
      uploadPath: Option[String] = None
    )(implicit trace: Trace, ev: R =:= Any): Unit =
      ZIOAppDefault(
        gql
          .runServer(port, apiPath, graphiqlPath, uploadPath)
          .provideEnvironment(ev.liftContra(ZEnvironment.empty))
      )
        .main(Array.empty)

    def unsafeRunServerR(
      env: ZEnvironment[R],
      port: Int = 8080,
      apiPath: String = "/graphql",
      graphiqlPath: Option[String] = Some("/graphiql"),
      uploadPath: Option[String] = None
    )(implicit trace: Trace, ev: R <:< Any): Unit =
      ZIOAppDefault(
        gql
          .runServer(port, apiPath, graphiqlPath, uploadPath)
          .provideEnvironment(env)
      )
        .main(Array.empty)

Copy link
Owner

Choose a reason for hiding this comment

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

I agree if they use zio environment, they shouldn't need this. For demo purposes I think using zio environment is complex enough to show a full zio solution.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think if you just went one level up to use ZIOApp instead it has an apply method that accepts a bootstrap method too

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we could take the wait and see approach, Im just wondering about the progression that each step gets taught. If we include a way to provide the layer you can teach Layers as a progression instead of first having to teach, well now we need to use this main class syntax before we can do the thing we really want which is adding service dependencies

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 tried a couple of things, and ended up with introducing a .unsafe syntax method. That allows to do the following: gql.unsafe.provideLayer(???).runServer(...).

In case we don't need to eliminate the environment, we can run it unsafely as gql.unsafe.runServer(...)

@ghostdogpr @paulpdaniels are you OK with these changes?


/**
* Convenience method for impurely running the server.
*
* Useful for scripts / demos / showing off Caliban to your colleagues etc.
*/
def unsafeRunServer(
port: Int = 8080,
apiPath: String = "/graphql",
graphiqlPath: Option[String] = Some("/graphiql"),
uploadPath: Option[String] = None
)(implicit trace: Trace): Unit =
ZIOAppDefault(gql.runServer(port, apiPath, graphiqlPath, uploadPath)).main(Array.empty)
}

}