My custom resolver for fields doesn't work with @strawberry.experimental.pydantic.type
I've got:
graphql.error.graphql_error.GraphQLError: Expected value of type 'User' but got: {'id': 1, 'first_name': 'John', 'last_name': 'Doe', 'email': '[email protected]'}
But it works as expected with regular @strawberry.type
β no errors
This works:
@strawberry.type
class User:
"""User type."""
id: int
first_name: str
last_name: str
email: str
@strawberry.type
class UsersResponse:
"""Users response type."""
users: list[User]
... but this doesn't:
class UserModel(BaseModel):
"""User model."""
id: int
first_name: str
last_name: str
email: str
@strawberry.experimental.pydantic.type(model=UserModel, all_fields=True)
class User:
"""User."""
@strawberry.type
class UsersResponse:
"""Users response type."""
users: list[User]
My schema:
def fields_resolver(source: Any, field_python_name: str):
"""
Fields resolver
Add support for resolving both Dict and Strawberry fields.
"""
try:
source._type_definition
except AttributeError:
# Resolve Dict
return getitem(source, field_python_name)
# Resolve Strawberry
return getattr(source, field_python_name)
@strawberry.type
class Query:
"""GraphQL query."""
# Users
getUsers = strawberry.field(get_users_query)
schema = strawberry.Schema(
query=Query,
config=StrawberryConfig(
auto_camel_case=True,
default_resolver=fields_resolver,
),
)
My resolver:
async def get_users_query() -> UsersResponse:
"""Get users query."""
response = await UserAPI().retrieve_user_collection()
user_list: list[dict] = response.json()
return UsersResponse(users=user_list)
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