Skip to content

Commit

Permalink
Enforce NonNull for returned related Sets and their content (#690)
Browse files Browse the repository at this point in the history
* Enforce NonNull for returned related Sets and their content. #448

* Run format.

* Remove duplicate assertion
  • Loading branch information
jkimbo authored Jun 25, 2019
1 parent e2e496f commit 54cc6a4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ def dynamic_type():

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)

Expand Down
3 changes: 2 additions & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

class DjangoListField(Field):
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
def model(self):
Expand Down
16 changes: 12 additions & 4 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from django.db import models
from django.utils.translation import ugettext_lazy as _
from graphene import NonNull
from py.test import raises

import graphene
Expand Down Expand Up @@ -234,8 +235,12 @@ class Meta:
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field)
assert isinstance(dynamic_field.type, graphene.List)
assert dynamic_field.type.of_type == A
# A NonNull List of NonNull A ([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():
Expand All @@ -262,8 +267,11 @@ class Meta:
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field)
assert isinstance(dynamic_field.type, graphene.List)
assert dynamic_field.type.of_type == A
# a NonNull List of NonNull A ([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():
Expand Down
2 changes: 1 addition & 1 deletion graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_schema_representation():
firstName: String!
lastName: String!
email: String!
pets: [Reporter]
pets: [Reporter!]!
aChoice: ReporterAChoice!
reporterType: ReporterReporterType
articles(before: String, after: String, first: Int, last: Int): ArticleConnection
Expand Down

0 comments on commit 54cc6a4

Please sign in to comment.