Skip to content

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

Add field for retrieving a given single attribute

NyanKiyoshi6edb5d041d9a
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 8635eb5653..80a5982b7e 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -153,6 +153,11 @@ class ProductQueries(graphene.ObjectType):
belonging to the given collection.""",
),
)
+ attribute = graphene.Field(
+ Attribute,
+ id=graphene.Argument(graphene.ID, required=True),
+ description="Lookup an attribute by ID.",
+ )
categories = PrefetchingConnectionField(
Category,
query=graphene.String(description=DESCRIPTIONS["category"]),
@@ -238,6 +243,9 @@ class ProductQueries(graphene.ObjectType):
):
return resolve_attributes(info, in_category, in_collection, query)
+ def resolve_attribute(self, info, id):
+ return graphene.Node.get_node_from_global_id(info, id, Attribute)
+
def resolve_categories(self, info, level=None, query=None, **_kwargs):
return resolve_categories(info, level=level, query=query)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 15ab5670bc..64f2f5731c 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2776,6 +2776,7 @@ type Query {
digitalContent(id: ID!): DigitalContent
digitalContents(query: String, level: Int, before: String, after: String, first: Int, last: Int): DigitalContentCountableConnection
attributes(query: String, inCategory: ID, inCollection: ID, before: String, after: String, first: Int, last: Int): AttributeCountableConnection
+ attribute(id: ID!): Attribute
categories(query: String, level: Int, before: String, after: String, first: Int, last: Int): CategoryCountableConnection
category(id: ID!): Category
collection(id: ID!): Collection
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 5e9f4a5143..81ddc6097c 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -52,6 +52,27 @@ def test_attributes_to_hstore_duplicated_slug(product, color_attribute, size_att
assert ids[str(color_attribute.pk)] == str(color_value.pk)
+def test_get_single_attribute_by_pk(user_api_client, color_attribute_without_values):
+ attribute_gql_id = graphene.Node.to_global_id(
+ "Attribute", color_attribute_without_values.id
+ )
+ query = """
+ query($id: ID!) {
+ attribute(id: $id) {
+ id
+ slug
+ }
+ }
+ """
+ content = get_graphql_content(
+ user_api_client.post_graphql(query, {"id": attribute_gql_id})
+ )
+
+ assert content["data"]["attribute"], "Should have found an attribute"
+ assert content["data"]["attribute"]["id"] == attribute_gql_id
+ assert content["data"]["attribute"]["slug"] == color_attribute_without_values.slug
+
+
def test_attributes_query(user_api_client, product):
attributes = Attribute.objects.prefetch_related("values")
query = """