Skip to content

Fix reverse o2o relation resolution #664

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

Closed
wants to merge 1 commit into from
Closed
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_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def resolve_reporter(self, info, **args):
"""
expected = {
"reporter": {"lastName": "ABA"},
"_debug": {
"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]
},
"_debug": {"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]},
}
schema = graphene.Schema(query=Query)
result = schema.execute(
Expand Down
60 changes: 60 additions & 0 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,66 @@ def resolve_reporter(self, info):
assert result.data == expected


def test_should_query_onetoone_fields():
film = Film(id=1)
film_details = FilmDetails(id=1, film=film)

class FilmNode(DjangoObjectType):
class Meta:
model = Film
interfaces = (Node,)

class FilmDetailsNode(DjangoObjectType):
class Meta:
model = FilmDetails
interfaces = (Node,)

class Query(graphene.ObjectType):
film = graphene.Field(FilmNode)
film_details = graphene.Field(FilmDetailsNode)

def resolve_film(root, info):
return film

def resolve_film_details(root, info):
return film_details

query = """
query FilmQuery {
filmDetails {
id
film {
id
}
}
film {
id
details {
id
}
}
}
"""
expected = {
"filmDetails": {
"id": "RmlsbURldGFpbHNOb2RlOjE=",
"film": {
"id": "RmlsbU5vZGU6MQ=="
}
},
"film": {
"id": "RmlsbU5vZGU6MQ==",
"details": {
"id": "RmlsbURldGFpbHNOb2RlOjE="
}
},
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_query_connectionfields():
class ReporterType(DjangoObjectType):
class Meta:
Expand Down
3 changes: 2 additions & 1 deletion graphene_django/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def get_reverse_fields(model, local_field_names):
if name in local_field_names:
continue

related = getattr(attr, "rel", None)
# "rel" for FK and M2M relations and "related" for O2O Relations
related = getattr(attr, "rel", None) or getattr(attr, "related", None)
if isinstance(related, models.ManyToOneRel):
yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
Expand Down