From 67b21cb36f9ed5319a3eb15cb438cbc3762b2299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Roberto=20Meza=20Cabrera?= Date: Sun, 9 Jun 2019 14:08:31 -0500 Subject: [PATCH 1/3] Revert "Drop old Django compatibility code" This reverts commit 6acd917cf7076397009d0ba77901f4c1c8e190fe. --- graphene_django/utils/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphene_django/utils/utils.py b/graphene_django/utils/utils.py index 02c47eec7..c2a3b0903 100644 --- a/graphene_django/utils/utils.py +++ b/graphene_django/utils/utils.py @@ -18,7 +18,8 @@ def get_reverse_fields(model, local_field_names): if name in local_field_names: continue - related = getattr(attr, "rel", None) + # Django =>1.9 uses 'rel', django <1.9 uses 'related' + 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: From ce6e6dd6e1668631f35c464f4f96818046437c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Roberto=20Meza=20Cabrera?= Date: Sun, 9 Jun 2019 14:15:46 -0500 Subject: [PATCH 2/3] Fixes O2O relations --- graphene_django/utils/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/utils/utils.py b/graphene_django/utils/utils.py index c2a3b0903..b8aaba0a8 100644 --- a/graphene_django/utils/utils.py +++ b/graphene_django/utils/utils.py @@ -18,7 +18,7 @@ def get_reverse_fields(model, local_field_names): if name in local_field_names: continue - # Django =>1.9 uses 'rel', django <1.9 uses 'related' + # "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) From 94602c77c6ebae74c418222291d6b2ca108a2f4d Mon Sep 17 00:00:00 2001 From: mvanlonden Date: Sun, 9 Jun 2019 12:41:04 -0700 Subject: [PATCH 3/3] add reverse relation one to one query test --- graphene_django/debug/tests/test_query.py | 4 +- graphene_django/tests/test_query.py | 56 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/graphene_django/debug/tests/test_query.py b/graphene_django/debug/tests/test_query.py index 592899b5c..af6971554 100644 --- a/graphene_django/debug/tests/test_query.py +++ b/graphene_django/debug/tests/test_query.py @@ -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( diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 58f46c726..36fad9b3a 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -226,6 +226,62 @@ 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: