Skip to content

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

Introduce new way to getting multiple nodes with one db query

Adam Janik30e24e63fa23
diff --git a/saleor/graphql/core/types.py b/saleor/graphql/core/types.py
index 8d9c1efb6d..0ccbeae142 100644
--- a/saleor/graphql/core/types.py
+++ b/saleor/graphql/core/types.py
@@ -15,7 +15,16 @@ class CountableConnection(graphene.relay.Connection):
return root.length
-class CountableDjangoObjectType(DjangoObjectType):
+class GetNodesMixin:
+
+ @classmethod
+ def get_nodes(cls, ids):
+ try:
+ return list(cls._meta.model.objects.filter(pk__in=ids))
+ except cls._meta.model.DoesNotExist:
+ return None
+
+class CountableDjangoObjectType(DjangoObjectType, GetNodesMixin):
class Meta:
abstract = True
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
index 7bca5753cf..9c4c8ddaaa 100644
--- a/saleor/graphql/product/mutations.py
+++ b/saleor/graphql/product/mutations.py
@@ -9,7 +9,7 @@ from ...product import models
from ..core.mutations import (
BaseMutation, ModelDeleteMutation, ModelFormMutation,
ModelFormUpdateMutation, StaffMemberRequiredMixin, Error)
-from ..utils import get_attributes_dict_from_list, get_node
+from ..utils import get_attributes_dict_from_list, get_node, get_nodes
from .forms import CollectionForm, ProductForm, ProductVariantForm
from .types import (
Category, Product, ProductAttribute, ProductImage, ProductType)
@@ -69,12 +69,11 @@ class CollectionCreateMutation(StaffMemberRequiredMixin, ModelFormMutation):
@classmethod
def get_form_kwargs(cls, root, info, **input):
- products = input.pop('products', None)
+ product_ids = input.pop('products', None)
kwargs = super().get_form_kwargs(root, info, **input)
- if products:
- products = {
- get_node(info, pr_id, only_type=Product)
- for pr_id in products}
+ if product_ids:
+ products = set(
+ get_nodes(info, product_ids, only_type=Product))
kwargs['data']['products'] = products
return kwargs
@@ -246,15 +245,14 @@ class ProductTypeCreateMutation(StaffMemberRequiredMixin, ModelFormMutation):
def get_form_kwargs(cls, root, info, **input):
product_attributes = input.pop('product_attributes', None)
if product_attributes:
- product_attributes = {
- get_node(info, pr_att_id, only_type=ProductAttribute)
- for pr_att_id in product_attributes}
-
+ product_attributes = set(
+ get_nodes(
+ info, product_attributes, only_type=ProductAttribute))
variant_attributes = input.pop('variant_attributes', None)
if variant_attributes:
- variant_attributes = {
- get_node(info, pr_att_id, only_type=ProductAttribute)
- for pr_att_id in variant_attributes}
+ variant_attributes = set(
+ get_nodes(
+ info, variant_attributes, only_type=ProductAttribute))
kwargs = super().get_form_kwargs(root, info, **input)
kwargs['data']['product_attributes'] = product_attributes
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index 3e4a26c448..ace3a4c9b8 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -6,6 +6,38 @@ from graphql_relay import from_global_id
from ..product.models import AttributeChoiceValue, ProductAttribute
+class MirumeeNode(graphene.Node):
+
+ @classmethod
+ def get_node_list_from_global_id(cls, info, global_ids, only_type=None):
+ try:
+ _ids = []
+ for _id in global_ids:
+ _type, _id = cls.from_global_id(_id)
+ _ids.append(_id)
+ graphene_type = info.schema.get_type(_type).graphene_type
+ except Exception:
+ return None
+
+ if only_type:
+ assert graphene_type == only_type, (
+ 'Must receive an {} id.'
+ ).format(graphene_type._meta.name)
+
+ get_nodes = getattr(graphene_type, 'get_nodes', None)
+ if get_nodes:
+ return get_nodes(_ids)
+
+
+def get_nodes(info, ids, only_type=None):
+ node_list = MirumeeNode.get_node_list_from_global_id(
+ info, ids, only_type=only_type)
+ if not node_list:
+ raise Exception(
+ "Could not resolve to a node_list with the global ids of '%s'." % id)
+ return node_list
+
+
def get_node(info, id, only_type=None):
"""Return node or throw an error if the node does not exist."""
node = graphene.Node.get_node_from_global_id(info, id, only_type=only_type)
diff --git a/saleor/settings.py b/saleor/settings.py
index cb3695f093..1b8be3650d 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -179,7 +179,7 @@ MIDDLEWARE = [
'saleor.core.middleware.taxes',
'social_django.middleware.SocialAuthExceptionMiddleware',
'impersonate.middleware.ImpersonateMiddleware',
- # 'saleor.graphql.middleware.jwt_middleware'
+ 'saleor.graphql.middleware.jwt_middleware'
]
INSTALLED_APPS = [