From a79a76d3b952c8d29a543eae1a320d6568c79350 Mon Sep 17 00:00:00 2001 From: Jon Rosebaugh Date: Tue, 17 Nov 2015 23:43:20 -0500 Subject: [PATCH] Add the info parameter (ResolveInfo) to get_node() calls. This allows request_context (or any other ResolveInfo data) to be used while getting nodes. For example, some data might need to be hidden based on the user's authorization; you would use info.request_context for this. Fixes #34. --- examples/starwars_django/schema.py | 4 +-- examples/starwars_relay/schema.py | 4 +-- graphene/contrib/django/tests/test_query.py | 4 +-- graphene/relay/fields.py | 2 +- graphene/relay/tests/test_query.py | 35 ++++++++++++++++++++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/examples/starwars_django/schema.py b/examples/starwars_django/schema.py index 7f267fed..501ccce6 100644 --- a/examples/starwars_django/schema.py +++ b/examples/starwars_django/schema.py @@ -17,7 +17,7 @@ class Ship(DjangoNode): model = ShipModel @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return Ship(get_ship(id)) @@ -33,7 +33,7 @@ class Faction(DjangoNode): model = FactionModel @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return Faction(get_faction(id)) diff --git a/examples/starwars_relay/schema.py b/examples/starwars_relay/schema.py index da31a6f7..f2c33834 100644 --- a/examples/starwars_relay/schema.py +++ b/examples/starwars_relay/schema.py @@ -11,7 +11,7 @@ class Ship(relay.Node): name = graphene.String(description='The name of the ship.') @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return get_ship(id) @@ -27,7 +27,7 @@ class Faction(relay.Node): return [get_ship(ship_id) for ship_id in self.ships] @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return get_faction(id) diff --git a/graphene/contrib/django/tests/test_query.py b/graphene/contrib/django/tests/test_query.py index a8e8229b..090c8695 100644 --- a/graphene/contrib/django/tests/test_query.py +++ b/graphene/contrib/django/tests/test_query.py @@ -66,7 +66,7 @@ def test_should_node(): model = Reporter @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return ReporterNode(Reporter(id=2, first_name='Cookie Monster')) def resolve_articles(self, *args, **kwargs): @@ -78,7 +78,7 @@ def test_should_node(): model = Article @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return ArticleNode(Article(id=1, headline='Article node')) class Query(graphene.ObjectType): diff --git a/graphene/relay/fields.py b/graphene/relay/fields.py index b3f6d328..58e0e8bd 100644 --- a/graphene/relay/fields.py +++ b/graphene/relay/fields.py @@ -92,7 +92,7 @@ class NodeField(Field): object_type != self.field_object_type): return - return object_type.get_node(_id) + return object_type.get_node(_id, info) def resolver(self, instance, args, info): global_id = args.get('id') diff --git a/graphene/relay/tests/test_query.py b/graphene/relay/tests/test_query.py index 2dec950c..cfe5502e 100644 --- a/graphene/relay/tests/test_query.py +++ b/graphene/relay/tests/test_query.py @@ -1,3 +1,4 @@ +import pytest from graphql.core.type import GraphQLID, GraphQLNonNull import graphene @@ -15,12 +16,22 @@ class MyNode(relay.Node): name = graphene.String() @classmethod - def get_node(cls, id): + def get_node(cls, id, info): return MyNode(id=id, name='mo') +class SpecialNode(relay.Node): + value = graphene.String() + + @classmethod + def get_node(cls, id, info): + value = "!!!" if info.request_context.get('is_special') else "???" + return SpecialNode(id=id, value=value) + + class Query(graphene.ObjectType): my_node = relay.NodeField(MyNode) + special_node = relay.NodeField(SpecialNode) all_my_nodes = relay.ConnectionField( MyNode, connection_type=MyConnection, customArg=graphene.String()) @@ -79,6 +90,28 @@ def test_nodefield_query(): assert result.data == expected +@pytest.mark.parametrize('specialness,value', [(True, '!!!'), (False, '???')]) +def test_get_node_info(specialness, value): + query = ''' + query ValueQuery { + specialNode(id:"U3BlY2lhbE5vZGU6Mg==") { + id + value + } + } + ''' + + expected = { + 'specialNode': { + 'id': 'U3BlY2lhbE5vZGU6Mg==', + 'value': value, + }, + } + result = schema.execute(query, request_context={'is_special': specialness}) + assert not result.errors + assert result.data == expected + + def test_nodeidfield(): id_field = MyNode._meta.fields_map['id'] id_field_type = schema.T(id_field)