If pydantic model contains Enum field this enum definition is not registered in GraphQL Schema
Running following code raises error:
import typing
from enum import Enum
import strawberry
from pydantic import BaseModel
class ModelTypeEnum(str, Enum):
type_1 = "type_1"
type_2 = "type_2"
class PydanticModel(BaseModel):
name: str
model_type: typing.Optional[ModelTypeEnum]
@strawberry.experimental.pydantic.type(model=PydanticModel, all_fields=True)
class StrawberryModel:
pass
@strawberry.type
class Query:
models: typing.List[StrawberryModel]
def get_models():
return []
@strawberry.type
class Query:
models: typing.List[StrawberryModel] = strawberry.field(resolver=get_models)
if __name__ == '__main__':
schema = strawberry.Schema(query=Query)
Traceback (most recent call last):
File "C:\Users\merlinus1\PycharmProjects\test_strawbery\main.py", line 38, in <module>
schema = strawberry.Schema(query=Query)
File "C:\Users\merlinus1\AppData\Local\pypoetry\Cache\virtualenvs\test-strawbery-ZCOgkqdC-py3.9\lib\site-packages\strawberry\schema\schema.py", line 84, in __init__
self._schema = GraphQLSchema(
File "C:\Users\merlinus1\AppData\Local\pypoetry\Cache\virtualenvs\test-strawbery-ZCOgkqdC-py3.9\lib\site-packages\graphql\type\schema.py", line 208, in __init__
collect_referenced_types(query)
File "C:\Users\merlinus1\AppData\Local\pypoetry\Cache\virtualenvs\test-strawbery-ZCOgkqdC-py3.9\lib\site-packages\graphql\type\schema.py", line 423, in collect_referenced_types
collect_referenced_types(field.type)
File "C:\Users\merlinus1\AppData\Local\pypoetry\Cache\virtualenvs\test-strawbery-ZCOgkqdC-py3.9\lib\site-packages\graphql\type\schema.py", line 422, in collect_referenced_types
for field in named_type.fields.values():
File "C:\Program Files\Python39\lib\functools.py", line 993, in __get__
val = self.func(instance)
File "C:\Users\merlinus1\AppData\Local\pypoetry\Cache\virtualenvs\test-strawbery-ZCOgkqdC-py3.9\lib\site-packages\graphql\type\definition.py", line 737, in fields
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
TypeError: StrawberryModel fields cannot be resolved. _enum_definition
Manually registering enum fixes error:
strawberry.enum(ModelTypeEnum)
But maybe enum has to be auto registered? Probably in replace_pydantic_types method
if issubclass(type_, Enum):
strawberry.enum(type_)
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