import structlog
from graphene import ResolveInfo

logger = structlog.get_logger(__name__)


class GrapheneErrorLoggingMiddleware(object):
    # https://jerrynsh.com/how-to-monitor-python-graphql-api-with-sentry/
    def resolve(self, next, root, info: ResolveInfo, **args):
        try:
            return next(root, info, **args)
        except Exception as error:
            logger.error(
                "GraphQL error", label="graphql_error", info=info, exc_info=error
            )
            raise error
