Skip to content

Commit 8c7ca74

Browse files
authored
Merge pull request #673 from jkimbo/relay-connection-required
Fix bug when setting a Relay ConnectionField to be required
2 parents 0971a05 + c25bcb3 commit 8c7ca74

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

graphene/relay/connection.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def __init__(self, type, *args, **kwargs):
9999
def type(self):
100100
type = super(IterableConnectionField, self).type
101101
connection_type = type
102-
if is_node(type):
102+
if isinstance(type, NonNull):
103+
connection_type = type.of_type
104+
105+
if is_node(connection_type):
103106
raise Exception(
104107
"ConnectionField's now need a explicit ConnectionType for Nodes.\n"
105108
"Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections"
@@ -108,7 +111,7 @@ def type(self):
108111
assert issubclass(connection_type, Connection), (
109112
'{} type have to be a subclass of Connection. Received "{}".'
110113
).format(self.__class__.__name__, connection_type)
111-
return connection_type
114+
return type
112115

113116
@classmethod
114117
def resolve_connection(cls, connection_type, args, resolved):
@@ -133,6 +136,9 @@ def resolve_connection(cls, connection_type, args, resolved):
133136
def connection_resolver(cls, resolver, connection_type, root, info, **args):
134137
resolved = resolver(root, info, **args)
135138

139+
if isinstance(connection_type, NonNull):
140+
connection_type = connection_type.of_type
141+
136142
on_resolve = partial(cls.resolve_connection, connection_type, args)
137143
if is_thenable(resolved):
138144
return Promise.resolve(resolved).then(on_resolve)

graphene/relay/tests/test_connection.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from ...types import Argument, Field, Int, List, NonNull, ObjectType, String
3+
from ...types import Argument, Field, Int, List, NonNull, ObjectType, String, Schema
44
from ..connection import Connection, ConnectionField, PageInfo
55
from ..node import Node
66

@@ -155,3 +155,23 @@ class Meta:
155155
'last': Argument(Int),
156156
'extra': Argument(String),
157157
}
158+
159+
160+
def test_connectionfield_required():
161+
class MyObjectConnection(Connection):
162+
163+
class Meta:
164+
node = MyObject
165+
166+
class Query(ObjectType):
167+
test_connection = ConnectionField(MyObjectConnection, required=True)
168+
169+
def resolve_test_connection(root, info, **args):
170+
return []
171+
172+
schema = Schema(query=Query)
173+
executed = schema.execute(
174+
'{ testConnection { edges { cursor } } }'
175+
)
176+
assert not executed.errors
177+
assert executed.data == {'testConnection': {'edges': []}}

0 commit comments

Comments
 (0)