strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓

Home Page:https://strawberry.rocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

make to_pydantic function recursive

chrisemke opened this issue · comments

Today when you have a type created from a pydantic model, you can convert it back to pydantic, but not it's fields

Describe the Bug

I made this example using sqlmodel models

from pydantic import PositiveInt
from sqlmodel import (
    Field,
    SQLModel,
)
from strawberry import auto
from strawberry.experimental.pydantic import input as pydantic_input


class City(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    ibge: PositiveInt = Field(unique=True)
    name: str
    ddd: int | None = None

class AddressBase(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    zipcode: PositiveInt | None = None
    neighborhood: str | None = None
    complement: str | None = None
    # coordinates: Coordinates | None = None

@pydantic_input(model=City)
class CityInput:
    ibge: auto
    name: auto
    ddd: auto


@pydantic_input(model=AddressBase)
class AddressInput:
    zipcode: auto
    city: CityInput | None = None

address_input = AddressInput(city=CityInput(ibge=1111, name='str'))
print(address_input)
print(type(address_input.to_pydantic()))
print(address_input.to_pydantic())
print(type(address_input.city.to_pydantic()))
print(address_input.city.to_pydantic())

result:

AddressInput(city=CityInput(ibge=1111, name='str', ddd=None), zipcode=None)
<class '__main__.AddressBase'>
id=None zipcode=None neighborhood=None complement=None
<class '__main__.City'>
id=None ibge=1111 name='str' ddd=None

notice: when I print the whole address_input, it doesn't convert the field city, it's just dropped out

System Information

  • Operating system: linux
  • Strawberry version (if applicable): any

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