Skip to content

Commit 6b37995

Browse files
committed
Merge pull request #36 from inklesspen/get_node_info
Add the info parameter (ResolveInfo) to get_node() calls.
2 parents a970d99 + a79a76d commit 6b37995

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

examples/starwars_django/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Meta:
1717
model = ShipModel
1818

1919
@classmethod
20-
def get_node(cls, id):
20+
def get_node(cls, id, info):
2121
return Ship(get_ship(id))
2222

2323

@@ -33,7 +33,7 @@ class Meta:
3333
model = FactionModel
3434

3535
@classmethod
36-
def get_node(cls, id):
36+
def get_node(cls, id, info):
3737
return Faction(get_faction(id))
3838

3939

examples/starwars_relay/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Ship(relay.Node):
1111
name = graphene.String(description='The name of the ship.')
1212

1313
@classmethod
14-
def get_node(cls, id):
14+
def get_node(cls, id, info):
1515
return get_ship(id)
1616

1717

@@ -27,7 +27,7 @@ def resolve_ships(self, **args):
2727
return [get_ship(ship_id) for ship_id in self.ships]
2828

2929
@classmethod
30-
def get_node(cls, id):
30+
def get_node(cls, id, info):
3131
return get_faction(id)
3232

3333

graphene/contrib/django/tests/test_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Meta:
6666
model = Reporter
6767

6868
@classmethod
69-
def get_node(cls, id):
69+
def get_node(cls, id, info):
7070
return ReporterNode(Reporter(id=2, first_name='Cookie Monster'))
7171

7272
def resolve_articles(self, *args, **kwargs):
@@ -78,7 +78,7 @@ class Meta:
7878
model = Article
7979

8080
@classmethod
81-
def get_node(cls, id):
81+
def get_node(cls, id, info):
8282
return ArticleNode(Article(id=1, headline='Article node'))
8383

8484
class Query(graphene.ObjectType):

graphene/relay/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def id_fetcher(self, global_id, info):
9292
object_type != self.field_object_type):
9393
return
9494

95-
return object_type.get_node(_id)
95+
return object_type.get_node(_id, info)
9696

9797
def resolver(self, instance, args, info):
9898
global_id = args.get('id')

graphene/relay/tests/test_query.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from graphql.core.type import GraphQLID, GraphQLNonNull
23

34
import graphene
@@ -15,12 +16,22 @@ class MyNode(relay.Node):
1516
name = graphene.String()
1617

1718
@classmethod
18-
def get_node(cls, id):
19+
def get_node(cls, id, info):
1920
return MyNode(id=id, name='mo')
2021

2122

23+
class SpecialNode(relay.Node):
24+
value = graphene.String()
25+
26+
@classmethod
27+
def get_node(cls, id, info):
28+
value = "!!!" if info.request_context.get('is_special') else "???"
29+
return SpecialNode(id=id, value=value)
30+
31+
2232
class Query(graphene.ObjectType):
2333
my_node = relay.NodeField(MyNode)
34+
special_node = relay.NodeField(SpecialNode)
2435
all_my_nodes = relay.ConnectionField(
2536
MyNode, connection_type=MyConnection, customArg=graphene.String())
2637

@@ -79,6 +90,28 @@ def test_nodefield_query():
7990
assert result.data == expected
8091

8192

93+
@pytest.mark.parametrize('specialness,value', [(True, '!!!'), (False, '???')])
94+
def test_get_node_info(specialness, value):
95+
query = '''
96+
query ValueQuery {
97+
specialNode(id:"U3BlY2lhbE5vZGU6Mg==") {
98+
id
99+
value
100+
}
101+
}
102+
'''
103+
104+
expected = {
105+
'specialNode': {
106+
'id': 'U3BlY2lhbE5vZGU6Mg==',
107+
'value': value,
108+
},
109+
}
110+
result = schema.execute(query, request_context={'is_special': specialness})
111+
assert not result.errors
112+
assert result.data == expected
113+
114+
82115
def test_nodeidfield():
83116
id_field = MyNode._meta.fields_map['id']
84117
id_field_type = schema.T(id_field)

0 commit comments

Comments
 (0)