strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

In a custom decorator, accessing the 'info' keyword argument

Open jeemyeong opened this issue 2 years ago • 3 comments

import logging
from functools import wraps

logger = logging.getLogger(__name__)


def logging_strawberry_resolver(func):
    @wraps(func)
    def inner(*args, **kwargs):
        info = kwargs.get("info")
        if info is None:
            raise ValueError("info must be passed as a keyword argument")
        variable_values = info.variable_values
        logger.info(f"Executing {info.field_name}", extra={"variable_values": variable_values})
        return func(*args, **kwargs)

    return inner

The provided code is currently functioning well. However, is it possible to simplify the way we access the 'info' keyword argument in the decorator as shown below?

import logging
from functools import wraps

from strawberry.types import Info

logger = logging.getLogger(__name__)


def logging_strawberry_resolver(func):
    @wraps(func)
    def inner(info: Info, *args, **kwargs):
        variable_values = info.variable_values
        logger.info(f"Executing {info.field_name}", extra={"variable_values": variable_values})
        return func(*args, **kwargs)

    return inner

jeemyeong avatar Oct 15 '23 22:10 jeemyeong

Hi, feels like this should already work. Are you getting an error with your second version and might it be because the info parameter is not passed to func?

DoctorJohn avatar Nov 10 '23 05:11 DoctorJohn