Skip to content

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

Simplify the retrieval of nodes in the graphql API

NyanKiyoshi6d9dae272033
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index 781a79b64a..bad9918446 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -4,10 +4,13 @@ from django.utils import timezone
from graphene_django.registry import get_global_registry
from graphql.error import GraphQLError
from graphql_relay import from_global_id
+from typing import List
from .core.enums import PermissionEnum, ReportingPeriod
from .core.types import PermissionDisplay
+ERROR_COULD_NO_RESOLVE_GLOBAL_ID = (
+ 'Could not resolve to a node with the global id list of \'%s\'.')
registry = get_global_registry()
@@ -20,18 +23,11 @@ def get_database_id(info, node_id, only_type):
return _id
-def get_nodes(ids, graphene_type=None):
- """Return a list of nodes.
-
- If the `graphene_type` argument is provided, the IDs will be validated
- against this type. If the type was not provided, it will be looked up in
- the Graphene's registry. Raises an error if not all IDs are of the same
- type.
- """
+def _resolve_nodes(ids, graphene_type=None):
pks = []
types = []
invalid_ids = []
- error_msg = "Could not resolve to a nodes with the global id list of '%s'."
+
for graphql_id in ids:
if graphql_id:
try:
@@ -45,31 +41,50 @@ def get_nodes(ids, graphene_type=None):
graphene_type._meta.name)
pks.append(_id)
types.append(_type)
+
if invalid_ids:
raise GraphQLError(
- error_msg % invalid_ids)
+ ERROR_COULD_NO_RESOLVE_GLOBAL_ID % invalid_ids)
+
+ return types, pks
+
+
+def _resolve_graphene_type(types: List):
+ assert len(set(types)) == 1, 'Received IDs of more than one type.'
+ # get type by name
+ type_name = types[0]
+ for model, _type in registry._registry.items():
+ if _type._meta.name == type_name:
+ return _type
+
+
+def get_nodes(ids, graphene_type=None):
+ """Return a list of nodes.
+
+ If the `graphene_type` argument is provided, the IDs will be validated
+ against this type. If the type was not provided, it will be looked up in
+ the Graphene's registry. Raises an error if not all IDs are of the same
+ type.
+ """
+ types, pks = _resolve_nodes(ids, graphene_type)
# If `graphene_type` was not provided, check if all resolved types are
# the same. This prevents from accidentally mismatching IDs of different
# types.
if types and not graphene_type:
- assert len(set(types)) == 1, 'Received IDs of more than one type.'
- # get type by name
- type_name = types[0]
- for model, _type in registry._registry.items():
- if _type._meta.name == type_name:
- graphene_type = _type
- break
+ graphene_type = _resolve_graphene_type(types)
nodes = list(graphene_type._meta.model.objects.filter(pk__in=pks))
nodes.sort(key=lambda e: pks.index(str(e.pk))) # preserve order in pks
+
if not nodes:
raise GraphQLError(
- error_msg % ids)
+ ERROR_COULD_NO_RESOLVE_GLOBAL_ID % ids)
+
nodes_pk_list = [str(node.pk) for node in nodes]
for pk in pks:
assert pk in nodes_pk_list, (
- 'There is no node of type {} with pk {}'.format(_type, pk))
+ 'There is no node of type {} with pk {}'.format(graphene_type, pk))
return nodes
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 2b658aed13..e55ac8ca71 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -417,7 +417,7 @@ def test_checkout_lines_invalid_variant_id(user_api_client, cart, variant):
content = get_graphql_content(response)
data = content['data']['checkoutLinesAdd']
error_msg = (
- 'Could not resolve to a nodes with the global id list of \'%s\'.')
+ 'Could not resolve to a node with the global id list of \'%s\'.')
assert data['errors'][0]['message'] == error_msg % [invalid_variant_id]
assert data['errors'][0]['field'] == 'variantId'
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index 15475d2506..d73d201071 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -144,7 +144,7 @@ def test_create_fulfillment_with_invalid_input(
assert data['errors']
assert data['errors'][0]['field'] == 'lines'
assert data['errors'][0]['message'] == (
- 'Could not resolve to a nodes with the global id list'
+ 'Could not resolve to a node with the global id list'
' of \'[\'fake-orderline-id\']\'.')
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index 6d470b7b96..0cf99f352d 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -216,14 +216,14 @@ def test_get_nodes(product_list):
# Raise an error if no nodes were found
global_ids = []
- msg = 'Could not resolve to a nodes with the global id list of {}.'.format(
+ msg = 'Could not resolve to a node with the global id list of {}.'.format(
global_ids)
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(
+ msg = 'Could not resolve to a node with the global id list of {}.'.format(
global_ids)
with pytest.raises(Exception, message=msg):
get_nodes(global_ids, Product)