strawberry-graphql/strawberry

`Annotated` + `strawberry.lazy` + deferred annotations results in `UnresolvedFieldTypeError: Could not resolve the type of ...` errors

Closed this issue ยท 2 comments

huonw commented

Describe the Bug

When using Annotated[..., strawberry.lazy(...)], one seems to get spurious errors in a file with from __future__ import annotations:

from __future__ import annotations
import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: Annotated["datetime", strawberry.lazy("datetime")]

schema = strawberry.Schema(Query)

Running that like python example.py with strawberry 0.277.3 gives errors like:

Traceback (most recent call last):
  File "/tmp/tmp.VSRhsBYbmq/example.py", line 9, in <module>
    schema = strawberry.Schema(Query)
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema.py", line 162, in __init__
    raise error.__cause__ from None
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/graphql/type/definition.py", line 808, in fields
    fields = resolve_thunk(self._fields)
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 525, in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 382, in get_graphql_fields
    return _get_thunk_mapping(
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 132, in _get_thunk_mapping
    raise UnresolvedFieldTypeError(type_definition, field)
strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'a'. Check that the class is accessible from the global module scope.

Standalone reproducer (run like bash ./script.sh):

# Setup:
cd $(mktemp -d)

python -m venv .venv

.venv/bin/python -m pip install graphql-core==3.2.3 python-dateutil==2.9.0.post0 six==1.16.0 typing_extensions==4.11.0 strawberry-graphql==0.227.3

cat > example.py <<EOF
from __future__ import annotations
import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: Annotated["datetime", strawberry.lazy("datetime")]

schema = strawberry.Schema(Query)
EOF

# BUG: strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'a'. Check that the class is accessible from the global module scope.
.venv/bin/python example.py

System Information

  • Operating system: macOS
  • Strawberry version (if applicable): 0.227.3

Additional Context

Thank you for strawberry! ๐Ÿ“

This seems a bit similar to:

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
huonw commented

Ah, the same applies to "manually" deferred annotations (i.e. an explicit string):

import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: """Annotated["datetime", strawberry.lazy("datetime")]"""

schema = strawberry.Schema(Query)

I think we'll fix this next week with @bellini666 ๐Ÿ˜Š