From d10a5806ce7198ab7723c4196cb2ebf678e59c85 Mon Sep 17 00:00:00 2001 From: "melvanlonden@canvasmedical.com" Date: Fri, 4 Jan 2019 14:13:02 -0800 Subject: [PATCH] make django connection nonnull unless enforce first or last --- graphene_django/fields.py | 16 +++++++++++----- graphene_django/filter/fields.py | 2 +- graphene_django/tests/test_converter.py | 2 +- graphene_django/tests/test_query.py | 4 +--- graphene_django/tests/test_types.py | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 1ecce45..881a2e7 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -4,7 +4,7 @@ from django.db.models.query import QuerySet from promise import Promise -from graphene.types import Field, List +from graphene.types import Field, List, NonNull from graphene.relay import ConnectionField, PageInfo from graphql_relay.connection.arrayconnection import connection_from_list_slice @@ -41,8 +41,8 @@ class DjangoConnectionField(ConnectionField): super(DjangoConnectionField, self).__init__(*args, **kwargs) @property - def type(self): - from .types import DjangoObjectType + def connection_type(self): + from graphene_django.types import DjangoObjectType _type = super(ConnectionField, self).type assert issubclass( @@ -53,9 +53,15 @@ class DjangoConnectionField(ConnectionField): ) return _type._meta.connection + @property + def type(self): + if self.enforce_first_or_last: + return self.connection_type + return NonNull(self.connection_type) + @property def node_type(self): - return self.type._meta.node + return self.connection_type._meta.node @property def model(self): @@ -146,7 +152,7 @@ class DjangoConnectionField(ConnectionField): return partial( self.connection_resolver, parent_resolver, - self.type, + self.connection_type, self.get_manager(), self.max_limit, self.enforce_first_or_last, diff --git a/graphene_django/filter/fields.py b/graphene_django/filter/fields.py index cb42543..8005895 100644 --- a/graphene_django/filter/fields.py +++ b/graphene_django/filter/fields.py @@ -110,7 +110,7 @@ class DjangoFilterConnectionField(DjangoConnectionField): return partial( self.connection_resolver, parent_resolver, - self.type, + self.connection_type, self.get_manager(), self.max_limit, self.enforce_first_or_last, diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index 5dc0184..38c809d 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -233,7 +233,7 @@ def test_should_manytomany_convert_connectionorlist_connection(): assert isinstance(graphene_field, graphene.Dynamic) dynamic_field = graphene_field.get_type() assert isinstance(dynamic_field, ConnectionField) - assert dynamic_field.type == A._meta.connection + assert dynamic_field.connection_type == A._meta.connection def test_should_manytoone_convert_connectionorlist(): diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 1716034..e8a4de6 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -638,15 +638,13 @@ def test_should_error_if_last_is_greater_than_max(): } """ - expected = {"allReporters": None} - result = schema.execute(query) assert len(result.errors) == 1 assert str(result.errors[0]) == ( "Requesting 101 records on the `allReporters` connection " "exceeds the `last` limit of 100 records." ) - assert result.data == expected + assert result.data == None graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 8a8643b..c32cb39 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -168,7 +168,7 @@ type Reporter { pets: [Reporter] aChoice: ReporterAChoice! reporterType: ReporterReporterType - articles(before: String, after: String, first: Int, last: Int): ArticleConnection + articles(before: String, after: String, first: Int, last: Int): ArticleConnection! } enum ReporterAChoice {