I want to do something like this:
def need_session(func):
@wraps(func)
def wrapper(*args, **kwargs):
session = SessionLocal()
try:
func(session, *args, **kwargs)
finally:
session.close()
I'm using the wraps
function, because I'm using Strawberry, which depends on argument types.
As you can see, func
is given an extra session
argument.
How can I make this work?
If I try to run a GraphQL server using Strawberry that has functions that are decorated using the decorator above, it gives me this error:
TypeError: Mutation fields cannot be resolved. Unexpected type '<class 'sqlalchemy.orm.session.Session'>'
If I change the decorator to this:
def need_session(func):
@wraps(func)
def wrapper(*args, **kwargs):
session = SessionLocal()
kwargs['session'] = session
try:
func(*args, **kwargs)
finally:
session.close()
And change the decorated function's argument list to:
def some_func(some_arg: SomeClass, **kwargs):
...
I still get this error:
strawberry.exceptions.MissingArgumentsAnnotationsError: Missing annotation for argument "kwargs" in field "login_user", did you forget to add it?