Skip to content

Commit 8907449

Browse files
committed
Merge pull request #177 from graphql-python/bugfixes/context-in-connectionfield
Added context to connectionfield resolver
2 parents 3093d2b + 11a5ee1 commit 8907449

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

graphene/contrib/django/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def get_manager(self):
2727
def get_queryset(self, resolved_qs, args, info):
2828
return resolved_qs
2929

30-
def from_list(self, connection_type, resolved, args, info):
30+
def from_list(self, connection_type, resolved, args, context, info):
3131
resolved_qs = maybe_queryset(resolved)
3232
qs = self.get_queryset(resolved_qs, args, info)
33-
return super(DjangoConnectionField, self).from_list(connection_type, qs, args, info)
33+
return super(DjangoConnectionField, self).from_list(connection_type, qs, args, context, info)
3434

3535

3636
class ConnectionOrListField(Field):

graphene/contrib/sqlalchemy/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ def __init__(self, *args, **kwargs):
2121
def model(self):
2222
return self.type._meta.model
2323

24-
def from_list(self, connection_type, resolved, args, info):
24+
def from_list(self, connection_type, resolved, args, context, info):
2525
if resolved is DefaultQuery:
2626
resolved = get_query(self.model, info)
2727
query = maybe_query(resolved)
28-
return super(SQLAlchemyConnectionField, self).from_list(connection_type, query, args, info)
28+
return super(SQLAlchemyConnectionField, self).from_list(connection_type, query, args, context, info)
2929

3030

3131
class ConnectionOrListField(Field):

graphene/relay/fields.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..core.fields import Field
66
from ..core.types.definitions import NonNull
77
from ..core.types.scalars import ID, Int, String
8-
from ..utils import with_context
8+
from ..utils.wrap_resolver_function import has_context, with_context
99

1010

1111
class ConnectionField(Field):
@@ -26,16 +26,23 @@ def __init__(self, type, resolver=None, description='',
2626
self.connection_type = connection_type
2727
self.edge_type = edge_type
2828

29-
def resolver(self, instance, args, info):
29+
@with_context
30+
def resolver(self, instance, args, context, info):
3031
schema = info.schema.graphene_schema
3132
connection_type = self.get_type(schema)
32-
resolved = super(ConnectionField, self).resolver(instance, args, info)
33+
34+
resolver = super(ConnectionField, self).resolver
35+
if has_context(resolver):
36+
resolved = super(ConnectionField, self).resolver(instance, args, context, info)
37+
else:
38+
resolved = super(ConnectionField, self).resolver(instance, args, info)
39+
3340
if isinstance(resolved, connection_type):
3441
return resolved
35-
return self.from_list(connection_type, resolved, args, info)
42+
return self.from_list(connection_type, resolved, args, context, info)
3643

37-
def from_list(self, connection_type, resolved, args, info):
38-
return connection_type.from_list(resolved, args, info)
44+
def from_list(self, connection_type, resolved, args, context, info):
45+
return connection_type.from_list(resolved, args, context, info)
3946

4047
def get_connection_type(self, node):
4148
connection_type = self.connection_type or node.get_connection_type()

graphene/relay/tests/test_query.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,58 @@ class Query(graphene.ObjectType):
3737
all_my_nodes = relay.ConnectionField(
3838
MyNode, connection_type=MyConnection, customArg=graphene.String())
3939

40+
context_nodes = relay.ConnectionField(
41+
MyNode, connection_type=MyConnection, customArg=graphene.String())
42+
4043
def resolve_all_my_nodes(self, args, info):
4144
custom_arg = args.get('customArg')
4245
assert custom_arg == "1"
4346
return [MyNode(name='my')]
4447

48+
@with_context
49+
def resolve_context_nodes(self, args, context, info):
50+
custom_arg = args.get('customArg')
51+
assert custom_arg == "1"
52+
return [MyNode(name='my')]
53+
4554
schema.query = Query
4655

4756

4857
def test_nodefield_query():
58+
query = '''
59+
query RebelsShipsQuery {
60+
contextNodes (customArg:"1") {
61+
edges {
62+
node {
63+
name
64+
}
65+
},
66+
myCustomField
67+
pageInfo {
68+
hasNextPage
69+
}
70+
}
71+
}
72+
'''
73+
expected = {
74+
'contextNodes': {
75+
'edges': [{
76+
'node': {
77+
'name': 'my'
78+
}
79+
}],
80+
'myCustomField': 'Custom',
81+
'pageInfo': {
82+
'hasNextPage': False,
83+
}
84+
}
85+
}
86+
result = schema.execute(query)
87+
assert not result.errors
88+
assert result.data == expected
89+
90+
91+
def test_connectionfield_context_query():
4992
query = '''
5093
query RebelsShipsQuery {
5194
myNode(id:"TXlOb2RlOjE=") {

graphene/relay/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def for_node(cls, node, edge_type=None):
8787
{'edge_type': edge_type, 'edges': edges})
8888

8989
@classmethod
90-
def from_list(cls, iterable, args, info):
90+
def from_list(cls, iterable, args, context, info):
9191
assert isinstance(
9292
iterable, Iterable), 'Resolved value from the connection field have to be iterable'
9393
connection = connection_from_list(

0 commit comments

Comments
 (0)