Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix get nodes in api

fowczarek19c1bad7812a
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index 0ed3da720b..fda9cf8e35 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -30,14 +30,24 @@ def get_nodes(ids, graphene_type=None):
"""
pks = []
types = []
+ wrong_ids = []
+ error_msg = "Could not resolve to a nodes with the global id list of '%s'."
for graphql_id in ids:
if graphql_id:
- _type, _id = from_global_id(graphql_id)
- if graphene_type:
- assert str(graphene_type) == _type, (
- 'Must receive an {} id.').format(graphene_type._meta.name)
- pks.append(_id)
- types.append(_type)
+ try:
+ _type, _id = from_global_id(graphql_id)
+ except Exception:
+ wrong_ids.append(graphql_id)
+ else:
+ if graphene_type:
+ assert str(graphene_type) == _type, (
+ 'Must receive an {} id.').format(
+ graphene_type._meta.name)
+ pks.append(_id)
+ types.append(_type)
+ if wrong_ids:
+ raise GraphQLError(
+ error_msg % wrong_ids)
# If `graphene_type` was not provided, check if all resolved types are
# the same. This prevents from accidentally mismatching IDs of different
@@ -54,8 +64,7 @@ def get_nodes(ids, graphene_type=None):
nodes = list(graphene_type._meta.model.objects.filter(pk__in=pks))
if not nodes:
raise GraphQLError(
- "Could not resolve to a nodes with the global id list of '%s'."
- % ids)
+ error_msg % ids)
nodes_pk_list = [str(node.pk) for node in nodes]
for pk in pks:
assert pk in nodes_pk_list, (
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index 80730d23b2..6d470b7b96 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -221,6 +221,13 @@ def test_get_nodes(product_list):
with pytest.raises(Exception, message=msg):
get_nodes(global_ids, Product)
+ # Raise an error if pass wrong ids
+ global_ids = ['a', 'bb']
+ msg = 'Could not resolve to a nodes with the global id list of {}.'.format(
+ global_ids)
+ with pytest.raises(Exception, message=msg):
+ get_nodes(global_ids, Product)
+
@patch('saleor.product.models.Product.objects')
def test_filter_by_query_param(qs):