Enforce NonNull for returned related Sets and their content (#690)

* Enforce NonNull for returned related Sets and their content. https://github.com/graphql-python/graphene-django/issues/448

* Run format.

* Remove duplicate assertion
This commit is contained in:
Jonathan Kim 2019-06-25 16:30:30 +01:00 committed by GitHub
parent e2e496f505
commit 54cc6a4b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 7 deletions

View File

@ -198,7 +198,11 @@ def convert_field_to_list_or_connection(field, registry=None):
return DjangoConnectionField(_type, description=description) return DjangoConnectionField(_type, description=description)
return DjangoListField(_type, description=description) return DjangoListField(
_type,
required=True, # A Set is always returned, never None.
description=description,
)
return Dynamic(dynamic_type) return Dynamic(dynamic_type)

View File

@ -15,7 +15,8 @@ from .utils import maybe_queryset
class DjangoListField(Field): class DjangoListField(Field):
def __init__(self, _type, *args, **kwargs): def __init__(self, _type, *args, **kwargs):
super(DjangoListField, self).__init__(List(_type), *args, **kwargs) # Django would never return a Set of None vvvvvvv
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs)
@property @property
def model(self): def model(self):

View File

@ -1,6 +1,7 @@
import pytest import pytest
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from graphene import NonNull
from py.test import raises from py.test import raises
import graphene import graphene
@ -234,8 +235,12 @@ def test_should_manytomany_convert_connectionorlist_list():
assert isinstance(graphene_field, graphene.Dynamic) assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type() dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field) assert isinstance(dynamic_field, graphene.Field)
assert isinstance(dynamic_field.type, graphene.List) # A NonNull List of NonNull A ([A!]!)
assert dynamic_field.type.of_type == A # https://github.com/graphql-python/graphene-django/issues/448
assert isinstance(dynamic_field.type, NonNull)
assert isinstance(dynamic_field.type.of_type, graphene.List)
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
assert dynamic_field.type.of_type.of_type.of_type == A
def test_should_manytomany_convert_connectionorlist_connection(): def test_should_manytomany_convert_connectionorlist_connection():
@ -262,8 +267,11 @@ def test_should_manytoone_convert_connectionorlist():
assert isinstance(graphene_field, graphene.Dynamic) assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type() dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field) assert isinstance(dynamic_field, graphene.Field)
assert isinstance(dynamic_field.type, graphene.List) # a NonNull List of NonNull A ([A!]!)
assert dynamic_field.type.of_type == A assert isinstance(dynamic_field.type, NonNull)
assert isinstance(dynamic_field.type.of_type, graphene.List)
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
assert dynamic_field.type.of_type.of_type.of_type == A
def test_should_onetoone_reverse_convert_model(): def test_should_onetoone_reverse_convert_model():

View File

@ -170,7 +170,7 @@ type Reporter {
firstName: String! firstName: String!
lastName: String! lastName: String!
email: String! email: String!
pets: [Reporter] pets: [Reporter!]!
aChoice: ReporterAChoice! aChoice: ReporterAChoice!
reporterType: ReporterReporterType reporterType: ReporterReporterType
articles(before: String, after: String, first: Int, last: Int): ArticleConnection articles(before: String, after: String, first: Int, last: Int): ArticleConnection