Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested Pydantic object models #110

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions graphene_pydantic/inputobjecttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ def resolve_placeholders(cls):
model=target_type.model,
)
fields_to_update[name] = graphene_field
meta.registry.register_object_field(
cls, name, pydantic_field, model=target_type.model
)
meta.registry.register_object_field(cls, name, pydantic_field)
# update the graphene side of things
meta.fields.update(fields_to_update)
24 changes: 19 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@
@session
@parametrize(
"pydantic",
("2.0", "2.1", "2.2", "2.3", "2.4"),
(
(2, 0),
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(2, 7),
(2, 8),
(2, 9),
(2, 10),
),
)
@parametrize("graphene", ("2.1.8", "2.1.9", "3.0", "3.1", "3.2", "3.3"))
@parametrize("graphene", ("2.1.8", "2.1.9", "3.0", "3.1", "3.2", "3.3", "3.4"))
def tests(session, pydantic, graphene):
if sys.version_info > (3, 10) and pydantic in ("1.7", "1.8"):
if sys.version_info > (3, 10) and pydantic in ((1, 7), (1, 8)):
return session.skip()
if sys.version_info > (3, 10) and graphene <= "3":
# Graphene 2.x doesn't support Python 3.11
return session.skip()
session.install(f"pydantic=={pydantic}")
if sys.version_info > (3, 11) and pydantic < (2, 9):
return session.skip()
pydantic_version_string = ".".join([str(n) for n in pydantic])
session.install(f"pydantic=={pydantic_version_string}")
session.install(f"graphene=={graphene}")
session.install("pytest", "pytest-cov", ".")
session.run(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ def test_register_object_field_and_get_for_graphene_field():
field = r.get_object_field_for_graphene_field(GraphFoo, "name")
assert field is not None
assert field.annotation == str


def test_register_object_field_nested_model():
"""https://github.com/graphql-python/graphene-pydantic/issues/104"""

class A(BaseModel):
x: str | None = None
y: list["A"] | None = None

class P_A(PydanticInputObjectType):
class Meta:
model = A

try:
P_A.resolve_placeholders()
except TypeError as e:
assert False, f"'10 / 5' raised an exception: {e}"
Loading