Skip to content

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

Added support for querying attribute by slug, added a test for it (#6477)

LeOndaz30fe250747ae
diff --git a/saleor/graphql/attribute/resolvers.py b/saleor/graphql/attribute/resolvers.py
index ae6ddf3ce3..43ca95b1b7 100644
--- a/saleor/graphql/attribute/resolvers.py
+++ b/saleor/graphql/attribute/resolvers.py
@@ -6,3 +6,7 @@ def resolve_attributes(info, qs=None, **_kwargs):
requestor = get_user_or_app_from_context(info.context)
qs = qs or models.Attribute.objects.get_visible_to_user(requestor)
return qs.distinct()
+
+
+def resolve_attribute_by_slug(slug):
+ return models.Attribute.objects.filter(slug=slug).first()
diff --git a/saleor/graphql/attribute/schema.py b/saleor/graphql/attribute/schema.py
index 0efb99273f..35e19a1019 100644
--- a/saleor/graphql/attribute/schema.py
+++ b/saleor/graphql/attribute/schema.py
@@ -13,7 +13,7 @@ from .mutations import (
AttributeValueDelete,
AttributeValueUpdate,
)
-from .resolvers import resolve_attributes
+from .resolvers import resolve_attribute_by_slug, resolve_attributes
from .sorters import AttributeSortingInput
from .types import Attribute
@@ -27,17 +27,18 @@ class AttributeQueries(graphene.ObjectType):
)
attribute = graphene.Field(
Attribute,
- id=graphene.Argument(
- graphene.ID, description="ID of the attribute.", required=True
- ),
+ id=graphene.Argument(graphene.ID, description="ID of the attribute."),
+ slug=graphene.Argument(graphene.String, description="Slug of the attribute."),
description="Look up an attribute by ID.",
)
def resolve_attributes(self, info, **kwargs):
return resolve_attributes(info, **kwargs)
- def resolve_attribute(self, info, id):
- return graphene.Node.get_node_from_global_id(info, id, Attribute)
+ def resolve_attribute(self, info, id=None, slug=None):
+ if id:
+ return graphene.Node.get_node_from_global_id(info, id, Attribute)
+ return resolve_attribute_by_slug(slug=slug)
class AttributeMutations(graphene.ObjectType):
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_query.py b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
index a0d6a2bbfa..23477505cd 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_query.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
@@ -34,6 +34,30 @@ def test_get_single_attribute_by_id_as_customer(
assert content["data"]["attribute"]["slug"] == color_attribute_without_values.slug
+def test_get_single_attribute_by_slug_as_customer(
+ user_api_client, color_attribute_without_values
+):
+ attribute_gql_slug = color_attribute_without_values.slug
+ query = """
+ query($slug: String!) {
+ attribute(slug: $slug) {
+ id
+ name
+ slug
+ }
+ }
+ """
+ content = get_graphql_content(
+ user_api_client.post_graphql(query, {"slug": attribute_gql_slug})
+ )
+
+ assert content["data"]["attribute"], "Should have found an attribute"
+ assert content["data"]["attribute"]["slug"] == attribute_gql_slug
+ assert content["data"]["attribute"]["id"] == graphene.Node.to_global_id(
+ "Attribute", color_attribute_without_values.id
+ )
+
+
QUERY_ATTRIBUTE = """
query($id: ID!) {
attribute(id: $id) {
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 61dcd6cf4c..9047b0c1bb 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -4414,7 +4414,7 @@ type Query {
channel(id: ID): Channel
channels: [Channel!]
attributes(filter: AttributeFilterInput, sortBy: AttributeSortingInput, before: String, after: String, first: Int, last: Int): AttributeCountableConnection
- attribute(id: ID!): Attribute
+ attribute(id: ID, slug: String): Attribute
appsInstallations: [AppInstallation!]!
apps(filter: AppFilterInput, sortBy: AppSortingInput, before: String, after: String, first: Int, last: Int): AppCountableConnection
app(id: ID!): App