Skip to content

Commit ba33ab0

Browse files
committed
Expand error messages
1 parent 989c3ef commit ba33ab0

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

graphene/relay/node.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,37 @@ 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
9493
except Exception as e:
9594
raise Exception(
96-
"Unable to call from_global_id, is the id a base64 encoding of 'TypeName:id': {} Exception: {}".format(
97-
str(global_id), str(e)
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+
)
98101
)
99102
)
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
111+
100112
if only_type:
101113
assert graphene_type == only_type, ("Must receive a {} id.").format(
102114
only_type._meta.name
103115
)
104116

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

109125
get_node = getattr(graphene_type, "get_node", None)
110126
if get_node:

graphene/relay/tests/test_node.py

Lines changed: 16 additions & 0 deletions
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,6 +84,20 @@ 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

@@ -91,6 +106,7 @@ def test_node_query_incorrect_id():
91106
'{ node(id:"%s") { ... on MyNode { name } } }' % "something:2"
92107
)
93108
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)