If I want to access a foreign key in a resolver, I get this error since version 0.46.2:
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
Here is my implementation:
# book.py
async def resolve_books(info: Info) -> book_types.BooksResult:
try:
user = info.context.get("request").consumer.scope.get("user")
result = await sync_to_async(services.book.all)(user=user)
return book_types.Books(books=result)
except PermissionDenied as err:
return PermissionProblem(code=err.code, message=err.message)
@strawberry.type
class BookQueries:
books: book_types.BooksResult = strawberry.field(resolver=resolve_books)
# book_types.py
from core.graphql.author import author_resolver
BooksResult = Annotated[
Union[Books, PermissionProblem],
strawberry.union("BooksResult"),
]
@strawberry_django.type(
model=models.Book, filters=BookFilter, order=BookOrder, pagination=True
)
class Book:
id: auto
name: auto
author: AuthorResult = strawberry.field(resolver=author_resolver)
# author.py
def author_resolver(root, info: Info) -> author_types.AuthorResult:
id_ = root.author_id
return resolve_author(id=id_, info=info)
async def resolve_author(id: uuid.UUID, info: Info) -> author_types.AuthorResult:
try:
user = info.context.get("request").consumer.scope.get("user")
result = await sync_to_async(services.author.get)(
user=user,
id=id,
)
return result
except PermissionDenied as err:
return PermissionProblem(code=err.code, message=err.message, object_id=id)
except DoesNotExist as err:
return DoesNotExistProblem(code=err.code, message=err.message, object_id=id)
The problem is the call root.author_id
in def author_resolver
. In version 0.46.2 the author_id
was already loaded at this point. This is why there was no error message.
What is the best way to change my implementation so that it works again with the current version?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too