strawberry
strawberry copied to clipboard
from __future__ import annotations incompatible with @strawberry.experimental.pydantic.type
Describe the Bug
Strawberry doesn't resolve string type annotations. It works without from __future__ import annotations
.
#!/usr/bin/env python
from __future__ import annotations
import strawberry
import pydantic
class PBook(pydantic.BaseModel):
title: str
@strawberry.experimental.pydantic.type(model=PBook)
class Book:
title: strawberry.auto
def get_book():
return Book(title="The Great Gatsby", author="F. Scott Fitzgerald", foo=1)
@strawberry.type
class Query:
book: Book = strawberry.field(resolver=get_book)
if __name__ == "__main__":
schema = strawberry.Schema(query=Query)
print(schema.as_str())
Traceback (most recent call last):
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/definition.py", line 800, in fields
fields = resolve_thunk(self._fields)
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
return thunk() if callable(thunk) else thunk
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 395, in <lambda>
fields=lambda: self.get_graphql_fields(object_type),
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 292, in get_graphql_fields
return self._get_thunk_mapping(
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 286, in _get_thunk_mapping
thunk_mapping[name_converter(f)] = field_converter(f)
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 215, in from_field
field_type = cast(GraphQLOutputType, self.from_maybe_optional(field.type))
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 551, in from_maybe_optional
return GraphQLNonNull(self.from_type(type_))
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema_converter.py", line 583, in from_type
raise TypeError(f"Unexpected type '{type_}'")
TypeError: Unexpected type 'typing.Annotated[typing.Any, <auto>]'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 27, in <module>
File "/home/app/Utvikling/github/strawberry-graphql/strawberry/strawberry/schema/schema.py", line 99, in __init__
self._schema = GraphQLSchema(
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/schema.py", line 224, in __init__
collect_referenced_types(query)
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/schema.py", line 433, in collect_referenced_types
collect_referenced_types(field.type)
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/schema.py", line 432, in collect_referenced_types
for field in named_type.fields.values():
File "/usr/lib64/python3.10/functools.py", line 981, in __get__
val = self.func(instance)
File "/home/app/.cache/pypoetry/virtualenvs/strawberry-graphql-UktstgS7-py3.10/lib/python3.10/site-packages/graphql/type/definition.py", line 803, in fields
raise cls(f"{self.name} fields cannot be resolved. {error}") from error
TypeError: Book fields cannot be resolved. Unexpected type 'typing.Annotated[typing.Any, <auto>]'
System Information
- Operating system: Fedora 36
- Strawberry version (if applicable): 0.117.1
The strawberry equivalent of this should fix the error.
diff --git a/strawberry/experimental/pydantic/object_type.py b/strawberry/experimental/pydantic/object_type.py
index 4b57c29..04e0099 100644
--- a/strawberry/experimental/pydantic/object_type.py
+++ b/strawberry/experimental/pydantic/object_type.py
@@ -130,6 +130,8 @@ def type(
)
existing_fields = getattr(cls, "__annotations__", {})
+ import pydantic.typing
+ existing_fields = pydantic.typing.resolve_annotations(existing_fields, cls.__module__)
# these are the fields that matched a field name in the pydantic model
# and should copy their alias from the pydantic model
fields_set = original_fields_set.union(
hey @taasan , could you try out this fix here https://github.com/strawberry-graphql/strawberry/pull/1994
pip install strawberry-graphql==0.116.5.dev.1657033844
It works with 0.116.5.dev.1657033844
This is now fixed