scalapb / zio-grpc

ScalaPB meets ZIO: write purely functional gRPC services and clients using ZIO
Apache License 2.0
257 stars 81 forks source link

`scoped` does not take `R` anymore for creation of metadata #598

Closed domdorn closed 3 months ago

domdorn commented 4 months ago

Hi!

I'm trying to upgrade vom 0.6.0-test8 to 0.6.1.

In 0.6.0-test8 I was able to provide a ZIO[R, Nothing, SafeMetadata] to the scoped client which allows me to pass in context specific correlationIds for every request in this way:

      grpcClient <- GetCalculationAttributesServiceClient.scoped[CorrelationId](
        channel,
        zio.ZIO.succeed(io.grpc.CallOptions.DEFAULT),
        for {
          corr <- ZIO.service[CorrelationId]
          m = {
            val m = new Metadata()
            authMetadata.foreach { case (key, value) => m.put(key, value) }
            m.put(correlationIdHeaderMetadataKey, corr.id.toString)
            m
          }
          meta <- SafeMetadata.fromMetadata(m)
        } yield meta
      )

In 0.6.1 the R of scoped (in this case CorrelationId) is removed. How can I now specify the CorrelationId on a per-request basis?

thesamet commented 4 months ago

Hi @domdorn , I'm currently ooo and unable to look closely into the issue. I'll be able to look further in about 3-4 weeks. In the meantime one way to accomplish it is to use the method withMetadataZIO to pass metadata on each call. Possibly you can use zio functions to wrap the service in a way that extracts data from the environment

thesamet commented 3 months ago

The short answer is that when constructing the client you need to have the dependency (CorrelationId) resolved. This can be accomplished by pulling the CorrelationId out of the environment the line before corr <- ZIO.service[CorrelationId], so you don't need that in the inner for-loop. Hope this helps. Take a look at the code here: https://github.com/scalapb/zio-grpc/blob/f4725c431969afe7b0ce99d9a12acda858397e68/examples/helloworld/src/main/scala/zio_grpc/examples/helloworld/HelloWorldClientMetadata.scala#L55-L56 - and if more specific help is needed feel free to provide a minimal reproducible example of what you are trying to do that used to previously work.