powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: Router context not being available when using AppSync with async resolvers

Open bjorger opened this issue 4 months ago • 3 comments

Expected Behaviour

When accessing the router.context in a resolver, with async resolvers, it should not be an empty dict, but much rather expose the context injected with app.append_context

Current Behaviour

When using router.context in async resolvers, the router.context is an empty dict

Code snippet

def lambda_handler(event, context):
    token = extract_auth_token_from_event(event)
    logger.info("token", extra={"token": token})
    app.append_context(
        isAdmin=True,
    )
    result = app.resolve(event, context)

    return asyncio.run(result)

@router.resolver(type_name="Mutation", field_name="addPet")
async def addPet(pet: dict):
    logger.info("Handling add pet mutation")
    logger.info("Context", extra={"resolver_context": router.context}) // this shows that router.context is an empty dict.
    
    return { "name": "Peter", "type": "Cat" }

Possible Solution

No response

Steps to Reproduce

Just plug in the lambda_handler and mutation I attached above, deploy it and run the mutation via the AppSync console

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.8

Packaging format used

Lambda Layers

Debugging logs

No response

bjorger avatar Oct 02 '24 12:10 bjorger