github ghostdogpr/caliban v2.2.0

latest releases: v2.9.0, v2.8.1, v2.8.0...
18 months ago

Release Notes

This release brings a few important changes, including a refactor of request interceptors, support for the @defer directive and support for Scala Native in caliban-client 🚀

Love this project? I am now on Github Sponsors. A good way to give back and encourage future developments! ❤️

New Adapter API

Request interceptors and configuration options such as skipValidation or enableIntrospection have been modified to be more powerful: you can now eliminate part of ZIO environment with request interceptors, and you can modify execution configuration dynamically.

These improvements require a small change in existing code. When calling makeHttpService, makeHttpUploadService or makeWebSocketService, you now need to wrap your interpreter into (respectively) an HttpInterpreter, HttpUploadInterpreter or WebSocketInterpreter.

// before
ZHttpAdapter.makeHttpService(interpreter)
// after
ZHttpAdapter.makeHttpService(HttpInterpreter(interpreter))

With these wrapper classes come 2 powerful methods:

  • configure takes a Configurator[R] which is an alias for URIO[R & Scope, Unit].
    It allows configuring the interpreter by running an effect that will run for each request and that can modify the configuration of the running fiber. Built-in configurators such as setSkipValidation, setEnableIntrospection and setQueryExecution are available in the Configurator object and let you dynamically change the configuration of the interpreter.
  • intercept takes an Interceptor[-R1, +R] which is an alias for ZLayer[R1 & ServerRequest, TapirResponse, R].
    It is basically a more powerful version of configure that gives you access to the incoming request (ServerRequest) and lets you modify the environment of the interpreter (from R to R1). A typical use case would be to extract an authentication token from the request and eliminate the authentication requirement from the environment if the token is valid. See an example here. You can also use this to change the configuration based on the incoming request (e.g. allow introspection only when a valid token is present).
val interpreter: GraphQLInterpreter[AuthToken, CalibanError] = ???
// turn our GraphQL interpreter into an HttpInterpreter
val noAuthInterpreter: HttpInterpreter[AuthToken, CalibanError] = HttpInterpreter(interpreter)
// define authentication logic (from a ServerRequest, fail or build an AuthToken)
val auth: ZLayer[ServerRequest, TapirResponse, AuthToken] = ???
// pass our interceptor to eliminate the AuthToken requirement from the environment
val authInterpreter: HttpUploadInterpreter[Any, CalibanError] = httpInterpreter.intercept(auth)
// get our route for Akka Http
val route = AkkaHttpAdapter.makeHttpService(authInterpreter)

This change was done in #1707 by @ghostdogpr

Other changes

Server

Client

Adapters

Don't miss a new caliban release

NewReleases is sending notifications on new releases.