mangum icon indicating copy to clipboard operation
mangum copied to clipboard

Silencing scope error for ariadne

Open Lilja opened this issue 3 years ago • 1 comments

Hi! Thank you for creating this framework. I'm currently using ariadne to manage a graphql lambda.

For every execution I get a stack trace:

ERROR:mangum.lifespan:Exception in 'lifespan' protocol.
Traceback (most recent call last):
  File "/Users/lilja/Library/Caches/pypoetry/virtualenvs/backend-dmp__8h8-py3.8/lib/python3.8/site-packages/mangum/protocols/lifespan.py", line 93, in run
    await self.app({"type": "lifespan"}, self.receive, self.send)
  File "/Users/lilja/Library/Caches/pypoetry/virtualenvs/backend-dmp__8h8-py3.8/lib/python3.8/site-packages/ariadne/asgi.py", line 94, in __call__
    raise ValueError("Unknown scope type: %r" % (scope["type"],))
ValueError: Unknown scope type: 'lifespan'

However, the lambda/mangum executes as the rest of the code as usual and I have correct graphql output.

I'm not up-to-speed with asgi stuff and don't really know what this means, is there a way to silence this error? Should I create a github issue with the other framework in question?

raised error in question: https://github.com/mirumee/ariadne/blob/master/ariadne/asgi.py#L113

How I use mangum

async def setup() -> GraphQL:
    session = ClientSession()
    schema_file = os.environ.get("SCHEMA_FILE", "../schema.graphql")

    async with session:
        engine = await setup_graphql(session, schema_file)
        return GraphQL(engine, debug=production)


def lambda_handler(event: Dict[str, Any], context: Any):
    graphql_app = asyncio.get_event_loop().run_until_complete(setup())
    handler_fn = Mangum(graphql_app)
    return add_cors_headers(handler_fn(event, context))


def add_cors_headers(ret_value: Dict[str, Dict]) -> Dict[str, Dict]:
    ret_value["headers"]["Access-Control-Allow-Origin"] = "*"

    return ret_value

Lilja avatar Apr 14 '21 15:04 Lilja

Hello, thanks for the issue report!

This is due to the default lifespan behavior of Mangum. You can set the Mangum(graphql_app, lifespan="off") setting to explicitly disable lifespan to avoid this, though Mangum should probably be catching exceptions generally in these cases instead of raising.

jordaneremieff avatar Apr 15 '21 15:04 jordaneremieff