Skip to content

Commit

Permalink
Merge pull request #177 from graphql-python/bugfixes/context-in-conne…
Browse files Browse the repository at this point in the history
…ctionfield

Added context to connectionfield resolver
  • Loading branch information
syrusakbary committed May 21, 2016
2 parents 3093d2b + 11a5ee1 commit 8907449
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions graphene/contrib/django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def get_manager(self):
def get_queryset(self, resolved_qs, args, info):
return resolved_qs

def from_list(self, connection_type, resolved, args, info):
def from_list(self, connection_type, resolved, args, context, info):
resolved_qs = maybe_queryset(resolved)
qs = self.get_queryset(resolved_qs, args, info)
return super(DjangoConnectionField, self).from_list(connection_type, qs, args, info)
return super(DjangoConnectionField, self).from_list(connection_type, qs, args, context, info)


class ConnectionOrListField(Field):
Expand Down
4 changes: 2 additions & 2 deletions graphene/contrib/sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def __init__(self, *args, **kwargs):
def model(self):
return self.type._meta.model

def from_list(self, connection_type, resolved, args, info):
def from_list(self, connection_type, resolved, args, context, info):
if resolved is DefaultQuery:
resolved = get_query(self.model, info)
query = maybe_query(resolved)
return super(SQLAlchemyConnectionField, self).from_list(connection_type, query, args, info)
return super(SQLAlchemyConnectionField, self).from_list(connection_type, query, args, context, info)


class ConnectionOrListField(Field):
Expand Down
19 changes: 13 additions & 6 deletions graphene/relay/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..core.fields import Field
from ..core.types.definitions import NonNull
from ..core.types.scalars import ID, Int, String
from ..utils import with_context
from ..utils.wrap_resolver_function import has_context, with_context


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

def resolver(self, instance, args, info):
@with_context
def resolver(self, instance, args, context, info):
schema = info.schema.graphene_schema
connection_type = self.get_type(schema)
resolved = super(ConnectionField, self).resolver(instance, args, info)

resolver = super(ConnectionField, self).resolver
if has_context(resolver):
resolved = super(ConnectionField, self).resolver(instance, args, context, info)
else:
resolved = super(ConnectionField, self).resolver(instance, args, info)

if isinstance(resolved, connection_type):
return resolved
return self.from_list(connection_type, resolved, args, info)
return self.from_list(connection_type, resolved, args, context, info)

def from_list(self, connection_type, resolved, args, info):
return connection_type.from_list(resolved, args, info)
def from_list(self, connection_type, resolved, args, context, info):
return connection_type.from_list(resolved, args, context, info)

def get_connection_type(self, node):
connection_type = self.connection_type or node.get_connection_type()
Expand Down
43 changes: 43 additions & 0 deletions graphene/relay/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,58 @@ class Query(graphene.ObjectType):
all_my_nodes = relay.ConnectionField(
MyNode, connection_type=MyConnection, customArg=graphene.String())

context_nodes = relay.ConnectionField(
MyNode, connection_type=MyConnection, customArg=graphene.String())

def resolve_all_my_nodes(self, args, info):
custom_arg = args.get('customArg')
assert custom_arg == "1"
return [MyNode(name='my')]

@with_context
def resolve_context_nodes(self, args, context, info):
custom_arg = args.get('customArg')
assert custom_arg == "1"
return [MyNode(name='my')]

schema.query = Query


def test_nodefield_query():
query = '''
query RebelsShipsQuery {
contextNodes (customArg:"1") {
edges {
node {
name
}
},
myCustomField
pageInfo {
hasNextPage
}
}
}
'''
expected = {
'contextNodes': {
'edges': [{
'node': {
'name': 'my'
}
}],
'myCustomField': 'Custom',
'pageInfo': {
'hasNextPage': False,
}
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_connectionfield_context_query():
query = '''
query RebelsShipsQuery {
myNode(id:"TXlOb2RlOjE=") {
Expand Down
2 changes: 1 addition & 1 deletion graphene/relay/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def for_node(cls, node, edge_type=None):
{'edge_type': edge_type, 'edges': edges})

@classmethod
def from_list(cls, iterable, args, info):
def from_list(cls, iterable, args, context, info):
assert isinstance(
iterable, Iterable), 'Resolved value from the connection field have to be iterable'
connection = connection_from_list(
Expand Down

0 comments on commit 8907449

Please sign in to comment.