Ejemplo n.º 1
0
async def test_lifespan_middleware() -> None:
    scopes: typing.List[dict] = []

    async def app(scope: dict, receive: typing.Callable, send: typing.Callable) -> None:
        assert scope["type"] == "http"
        scopes.append(scope)

    async def lifespan(
        scope: dict, receive: typing.Callable, send: typing.Callable
    ) -> None:
        assert scope["type"] == "lifespan"
        scopes.append(scope)

    app = LifespanMiddleware(app, lifespan=lifespan)

    async def receive() -> dict:
        pass  # pragma: no cover

    async def send(message: dict) -> None:
        pass  # pragma: no cover

    await app({"type": "lifespan"}, receive, send)
    await app({"type": "http"}, receive, send)
    assert scopes == [{"type": "lifespan"}, {"type": "http"}]
Ejemplo n.º 2
0
# --- ASGI app

# Create an ASGI app using the schema, running in debug mode
# Set context with authenticated graphql client.
#ontext_value={'client': getClient()}
app = GraphQL(schema, debug=True)

# 'Lifespan' is a standalone ASGI app.
# It implements the lifespan protocol,
# and allows registering lifespan event handlers.
lifespan = Lifespan()


@lifespan.on_event("startup")
async def startup():
    print("Starting up...")
    print("... done!")


@lifespan.on_event("shutdown")
async def shutdown():
    print("Shutting down...")
    print("... done!")


# 'LifespanMiddleware' returns an ASGI app.
# It forwards lifespan requests to 'lifespan',
# and anything else goes to 'app'.
app = LifespanMiddleware(app, lifespan=lifespan)