GitHub

middleware

API reference for middleware.

Client middleware for GraphQL operations (Effect's GraphQLMiddleware).

A middleware is a tag class whose service is an Implementation: it gets each request with next (the rest of the chain) and may change the request, the result, or fail with its own error E while needing R::

class Auth(GraphQLMiddleware[TokenExpired, CurrentUser]): ...

AuthLive = layer.succeed(Auth, map_request(add_token))

Attach it to an operation with op.middleware(Auth) (and declare it on the group, class Api(GraphQLGroup[Never, Auth]), so make requires it) or to every operation of a group with class Api(GraphQLGroup[Auth]): .... Group middleware runs outside operation middleware; within a level, in attach order.

Types

TypeDefinitionDescription
AnyMiddlewaretype AnyMiddleware = type[GraphQLMiddleware[Any, Any]]

Classes

Functions

map_request

map_request[E = Never, R = Never](f) -> Implementation[E, R]

A middleware that only changes the request, for every kind of operation.

ParameterTypeDescription
fCallable[[GraphQLRequest, AnyOperation], Effect[GraphQLRequest, E, R]]

Returns Implementation[E, R]

def map_request[E = Never, R = Never](
    f: Callable[[GraphQLRequest, AnyOperation], Effect[GraphQLRequest, E, R]],
) -> Implementation[E, R]:
    """A middleware that only changes the request, for every kind of
    operation."""

    def execute(options: ExecuteOptions) -> Effect[ExecutionResult, GraphQLClientError | E, R]:
        return f(options.request, options.operation).flat_map(options.next)

    def subscribe(options: SubscribeOptions) -> Stream[ExecutionResult, GraphQLClientError | E, R]:
        return from_effect(f(options.request, options.operation)).flat_map(options.next)

    return Implementation(execute=execute, subscribe=subscribe)