Skip to content

Add a helpful message to when a global_id fails to parse. #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions graphene/relay/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,24 @@ def node_resolver(cls, only_type, root, info, id):
def get_node_from_global_id(cls, info, global_id, only_type=None):
try:
_type, _id = cls.from_global_id(global_id)
graphene_type = info.schema.get_type(_type).graphene_type
except Exception:
return None
except Exception as e:
raise Exception(
(
'Unable to parse global ID "{global_id}". '
'Make sure it is a base64 encoded string in the format: "TypeName:id". '
"Exception message: {exception}".format(
global_id=global_id, exception=str(e)
)
)
)

graphene_type = info.schema.get_type(_type)
if graphene_type is None:
raise Exception(
'Relay Node "{_type}" not found in schema'.format(_type=_type)
)

graphene_type = graphene_type.graphene_type

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

# We make sure the ObjectType implements the "Node" interface
if cls not in graphene_type._meta.interfaces:
return None
raise Exception(
'ObjectType "{_type}" does not implement the "{cls}" interface.'.format(
_type=_type, cls=cls
)
)

get_node = getattr(graphene_type, "get_node", None)
if get_node:
Expand Down
18 changes: 17 additions & 1 deletion graphene/relay/tests/test_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from graphql_relay import to_global_id

from graphql.pyutils import dedent
Expand Down Expand Up @@ -83,14 +84,29 @@ def test_node_requesting_non_node():
executed = schema.execute(
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1)
)
assert executed.errors
assert re.match(
r"ObjectType .* does not implement the .* interface.",
executed.errors[0].message,
)
assert executed.data == {"node": None}


def test_node_requesting_unknown_type():
executed = schema.execute(
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("UnknownType", 1)
)
assert executed.errors
assert re.match(r"Relay Node .* not found in schema", executed.errors[0].message)
assert executed.data == {"node": None}


def test_node_query_incorrect_id():
executed = schema.execute(
'{ node(id:"%s") { ... on MyNode { name } } }' % "something:2"
)
assert not executed.errors
assert executed.errors
assert re.match(r"Unable to parse global ID .*", executed.errors[0].message)
assert executed.data == {"node": None}


Expand Down