Skip to content

Enforce NonNull for returned related Sets and their content #690

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

Merged
merged 3 commits into from
Jun 25, 2019
Merged
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
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