Skip to content

Commit 23bb52a

Browse files
allen-munschjkimbo
andauthored
Add a helpful message to when a global_id fails to parse. (#1074)
* Add a helpful message to when a global_id fails to parse. * Update test_node to have errors on test_node_query_incorrect_id * Black the node.py file * Remove func wrapper used in debugging get_resolver partial * Update node.py * Expand error messages Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
1 parent ad0b3a5 commit 23bb52a

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

graphene/relay/node.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,24 @@ def node_resolver(cls, only_type, root, info, id):
9090
def get_node_from_global_id(cls, info, global_id, only_type=None):
9191
try:
9292
_type, _id = cls.from_global_id(global_id)
93-
graphene_type = info.schema.get_type(_type).graphene_type
94-
except Exception:
95-
return None
93+
except Exception as e:
94+
raise Exception(
95+
(
96+
'Unable to parse global ID "{global_id}". '
97+
'Make sure it is a base64 encoded string in the format: "TypeName:id". '
98+
"Exception message: {exception}".format(
99+
global_id=global_id, exception=str(e)
100+
)
101+
)
102+
)
103+
104+
graphene_type = info.schema.get_type(_type)
105+
if graphene_type is None:
106+
raise Exception(
107+
'Relay Node "{_type}" not found in schema'.format(_type=_type)
108+
)
109+
110+
graphene_type = graphene_type.graphene_type
96111

97112
if only_type:
98113
assert graphene_type == only_type, ("Must receive a {} id.").format(
@@ -101,7 +116,11 @@ def get_node_from_global_id(cls, info, global_id, only_type=None):
101116

102117
# We make sure the ObjectType implements the "Node" interface
103118
if cls not in graphene_type._meta.interfaces:
104-
return None
119+
raise Exception(
120+
'ObjectType "{_type}" does not implement the "{cls}" interface.'.format(
121+
_type=_type, cls=cls
122+
)
123+
)
105124

106125
get_node = getattr(graphene_type, "get_node", None)
107126
if get_node:

graphene/relay/tests/test_node.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from graphql_relay import to_global_id
23

34
from graphql.pyutils import dedent
@@ -83,14 +84,29 @@ def test_node_requesting_non_node():
8384
executed = schema.execute(
8485
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1)
8586
)
87+
assert executed.errors
88+
assert re.match(
89+
r"ObjectType .* does not implement the .* interface.",
90+
executed.errors[0].message,
91+
)
92+
assert executed.data == {"node": None}
93+
94+
95+
def test_node_requesting_unknown_type():
96+
executed = schema.execute(
97+
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("UnknownType", 1)
98+
)
99+
assert executed.errors
100+
assert re.match(r"Relay Node .* not found in schema", executed.errors[0].message)
86101
assert executed.data == {"node": None}
87102

88103

89104
def test_node_query_incorrect_id():
90105
executed = schema.execute(
91106
'{ node(id:"%s") { ... on MyNode { name } } }' % "something:2"
92107
)
93-
assert not executed.errors
108+
assert executed.errors
109+
assert re.match(r"Unable to parse global ID .*", executed.errors[0].message)
94110
assert executed.data == {"node": None}
95111

96112

0 commit comments

Comments
 (0)