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>
This commit is contained in:
James 2020-02-10 16:16:11 -06:00 committed by GitHub
parent ad0b3a529c
commit 23bb52a770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -90,9 +90,24 @@ class Node(AbstractNode):
def get_node_from_global_id(cls, info, global_id, only_type=None): def get_node_from_global_id(cls, info, global_id, only_type=None):
try: try:
_type, _id = cls.from_global_id(global_id) _type, _id = cls.from_global_id(global_id)
graphene_type = info.schema.get_type(_type).graphene_type except Exception as e:
except Exception: raise Exception(
return None (
'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: if only_type:
assert graphene_type == only_type, ("Must receive a {} id.").format( 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 # We make sure the ObjectType implements the "Node" interface
if cls not in graphene_type._meta.interfaces: 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) get_node = getattr(graphene_type, "get_node", None)
if get_node: if get_node:

View File

@ -1,3 +1,4 @@
import re
from graphql_relay import to_global_id from graphql_relay import to_global_id
from graphql.pyutils import dedent from graphql.pyutils import dedent
@ -83,6 +84,20 @@ def test_node_requesting_non_node():
executed = schema.execute( executed = schema.execute(
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1) '{ 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} assert executed.data == {"node": None}
@ -90,7 +105,8 @@ def test_node_query_incorrect_id():
executed = schema.execute( executed = schema.execute(
'{ node(id:"%s") { ... on MyNode { name } } }' % "something:2" '{ 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} assert executed.data == {"node": None}