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

Add unsafeRunServer method #2050

merged 4 commits into from
Dec 24, 2023

Conversation

kyri-petrou
Copy link
Collaborator

No description provided.

@@ -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?


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

@kyri-petrou kyri-petrou merged commit 903ff46 into series/2.x Dec 24, 2023
10 checks passed
@kyri-petrou kyri-petrou deleted the unsafe-run-server branch December 24, 2023 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants