strawberry
strawberry copied to clipboard
In a custom decorator, accessing the 'info' keyword argument
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
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?