mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-24 02:24:10 +03:00
fix: foreign key nullable and custom resolver (#1446)
* fix: nullable one to one relation * fix: makefile
This commit is contained in:
parent
9a773b9d7b
commit
db34d2e815
2
Makefile
2
Makefile
|
@ -10,7 +10,7 @@ dev-setup:
|
||||||
|
|
||||||
.PHONY: tests ## Run unit tests
|
.PHONY: tests ## Run unit tests
|
||||||
tests:
|
tests:
|
||||||
py.test graphene_django --cov=graphene_django -vv
|
PYTHONPATH=. py.test graphene_django --cov=graphene_django -vv
|
||||||
|
|
||||||
.PHONY: format ## Format code
|
.PHONY: format ## Format code
|
||||||
format:
|
format:
|
||||||
|
|
|
@ -302,12 +302,15 @@ def convert_onetoone_field_to_djangomodel(field, registry=None):
|
||||||
reversed_field_name = root.__class__._meta.get_field(
|
reversed_field_name = root.__class__._meta.get_field(
|
||||||
field_name
|
field_name
|
||||||
).remote_field.name
|
).remote_field.name
|
||||||
return _type.get_queryset(
|
try:
|
||||||
_type._meta.model.objects.filter(
|
return _type.get_queryset(
|
||||||
**{reversed_field_name: root.pk}
|
_type._meta.model.objects.filter(
|
||||||
),
|
**{reversed_field_name: root.pk}
|
||||||
info,
|
),
|
||||||
).get()
|
info,
|
||||||
|
).get()
|
||||||
|
except _type._meta.model.DoesNotExist:
|
||||||
|
return None
|
||||||
|
|
||||||
return custom_resolver
|
return custom_resolver
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,11 @@ class Pet(models.Model):
|
||||||
class FilmDetails(models.Model):
|
class FilmDetails(models.Model):
|
||||||
location = models.CharField(max_length=30)
|
location = models.CharField(max_length=30)
|
||||||
film = models.OneToOneField(
|
film = models.OneToOneField(
|
||||||
"Film", on_delete=models.CASCADE, related_name="details"
|
"Film",
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name="details",
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2062,3 +2062,74 @@ def test_should_query_nullable_foreign_key():
|
||||||
assert result.data["person"] == {
|
assert result.data["person"] == {
|
||||||
"pets": [{"name": "Jane's dog"}],
|
"pets": [{"name": "Jane's dog"}],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_query_nullable_one_to_one_relation_with_custom_resolver():
|
||||||
|
class FilmType(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = Film
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_queryset(cls, queryset, info):
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
class FilmDetailsType(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = FilmDetails
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_queryset(cls, queryset, info):
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
film = graphene.Field(FilmType, genre=graphene.String(required=True))
|
||||||
|
film_details = graphene.Field(
|
||||||
|
FilmDetailsType, location=graphene.String(required=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
def resolve_film(self, info, genre):
|
||||||
|
return Film.objects.filter(genre=genre).first()
|
||||||
|
|
||||||
|
def resolve_film_details(self, info, location):
|
||||||
|
return FilmDetails.objects.filter(location=location).first()
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
|
||||||
|
Film.objects.create(genre="do")
|
||||||
|
FilmDetails.objects.create(location="London")
|
||||||
|
|
||||||
|
query_film = """
|
||||||
|
query getFilm($genre: String!) {
|
||||||
|
film(genre: $genre) {
|
||||||
|
genre
|
||||||
|
details {
|
||||||
|
location
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
query_film_details = """
|
||||||
|
query getFilmDetails($location: String!) {
|
||||||
|
filmDetails(location: $location) {
|
||||||
|
location
|
||||||
|
film {
|
||||||
|
genre
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = schema.execute(query_film, variables={"genre": "do"})
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data["film"] == {
|
||||||
|
"genre": "DO",
|
||||||
|
"details": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = schema.execute(query_film_details, variables={"location": "London"})
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data["filmDetails"] == {
|
||||||
|
"location": "London",
|
||||||
|
"film": None,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user