From 23bb52a770e8b696770cdd29b76dbd23c4a5e749 Mon Sep 17 00:00:00 2001 From: James <33908344+allen-munsch@users.noreply.github.com> Date: Mon, 10 Feb 2020 16:16:11 -0600 Subject: [PATCH] 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 --- graphene/relay/node.py | 27 +++++++++++++++++++++++---- graphene/relay/tests/test_node.py | 18 +++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/graphene/relay/node.py b/graphene/relay/node.py index 1a5c5bdb..f8927ab7 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -90,9 +90,24 @@ class Node(AbstractNode): 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( @@ -101,7 +116,11 @@ class Node(AbstractNode): # 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: diff --git a/graphene/relay/tests/test_node.py b/graphene/relay/tests/test_node.py index 62fd31a3..de1802e9 100644 --- a/graphene/relay/tests/test_node.py +++ b/graphene/relay/tests/test_node.py @@ -1,3 +1,4 @@ +import re from graphql_relay import to_global_id from graphql.pyutils import dedent @@ -83,6 +84,20 @@ 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} @@ -90,7 +105,8 @@ 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}