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

Utilize ZIO / ZQuery eager constructors and accessors wherever possible #2297

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions adapters/quick/src/main/scala/caliban/QuickRequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.stream.{ UStream, ZPipeline, ZStream }

import java.nio.charset.StandardCharsets.UTF_8
import scala.util.Try
import scala.util.control.NonFatal

final private class QuickRequestHandler[R](
Expand All @@ -41,20 +42,23 @@ final private class QuickRequestHandler[R](
def configureWebSocket[R1](config: quick.WebSocketConfig[R1]): QuickRequestHandler[R & R1] =
new QuickRequestHandler[R & R1](interpreter, config)

def handleHttpRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
def handleHttpRequest(request: Request)(implicit trace: Trace): URIO[R, Response] = ZIO.suspendSucceed {
transformHttpRequest(request)
.flatMap(executeRequest(request.method, _))
.foldZIO(
Exit.succeed,
resp => Exit.succeed(transformResponse(request, resp))
)
}

def handleUploadRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
def handleUploadRequest(request: Request)(implicit trace: Trace): URIO[R, Response] = ZIO.suspendSucceed {
transformUploadRequest(request).flatMap { case (req, fileHandle) =>
executeRequest(request.method, req)
.map(transformResponse(request, _))
.provideSomeLayer[R](fileHandle)
}.merge
executeRequest(request.method, req).provideSomeLayer[R](fileHandle)
}.foldZIO(
Exit.succeed,
v => Exit.succeed(transformResponse(request, v))
)
}

def handleWebSocketRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
Response.fromSocketApp {
Expand Down Expand Up @@ -129,10 +133,10 @@ final private class QuickRequestHandler[R](
partsMap: Map[String, FormField],
key: String
)(implicit jsonValueCodec: JsonValueCodec[A]): IO[Response, A] =
ZIO
Exit
.fromOption(partsMap.get(key))
.flatMap(_.asChunk)
.flatMap(v => ZIO.attempt(readFromArray[A](v.toArray)))
.flatMap(v => Exit.fromTry(Try(readFromArray[A](v.toArray))))
.orElseFail(Response.badRequest)

def parsePath(path: String): List[PathValue] = path.split('.').toList.map(PathValue.parse)
Expand Down
Loading
Loading