Skip to content

Commit

Permalink
Add a helpful message to when a global_id fails to parse. (#1074)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
allen-munsch and jkimbo committed Feb 10, 2020
1 parent ad0b3a5 commit 23bb52a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
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

0 comments on commit 23bb52a

Please sign in to comment.