Skip to content

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

Try to guess attribute value type in API

Artur Smęt7e03eaae869c
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index eeaf331ef8..09217718e4 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -20,6 +20,8 @@ class AttributeChoiceValueInput(graphene.InputObjectType):
required=True, description='Internal name.')
name = graphene.String(
required=True, description='Name displayed in the interface.')
+ value = graphene.String(
+ required=True, description='Real value eg. HEX color.')
class AttributeChoiceValueUpdateInput(graphene.InputObjectType):
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 8e93fa7a64..1008df5fbe 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -1,3 +1,5 @@
+import re
+
import graphene
from graphene import relay
from graphene_django.filter import DjangoFilterConnectionField
@@ -17,6 +19,13 @@ from ..utils import get_database_id
from .filters import ProductFilterSet
+class ProductAttribuetValueType(graphene.Enum):
+ COLOR = 'COLOR'
+ GRADIENT = 'GRADIENT'
+ URL = 'URL'
+ STRING = 'STRING'
+
+
def resolve_attribute_list(attributes):
keys = list(attributes.keys())
values = list(attributes.values())
@@ -35,10 +44,23 @@ def resolve_attribute_list(attributes):
return attributes_list
+def resolve_attribute_value_type(attribute_value):
+ color_pattern = '^(#[0-9a-fA-F]{3}|#(?:[0-9a-fA-F]{2}){2,4}|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))$' # noqa
+ color_pattern = re.compile(color_pattern)
+ if color_pattern.match(attribute_value):
+ return ProductAttribuetValueType.COLOR
+ if 'gradient(' in attribute_value:
+ return ProductAttribuetValueType.GRADIENT
+ if '://' in attribute_value:
+ return ProductAttribuetValueType.URL
+ return ProductAttribuetValueType.STRING
+
+
class ProductAttributeValue(CountableDjangoObjectType):
name = graphene.String(description='Visible name for display purposes.')
slug = graphene.String(
description='Internal representation of an attribute name.')
+ type = ProductAttribuetValueType(description='Type of value.')
class Meta:
description = 'Represents a value of an attribute.'
@@ -46,6 +68,9 @@ class ProductAttributeValue(CountableDjangoObjectType):
interfaces = [relay.Node]
model = models.AttributeChoiceValue
+ def resolve_type(self, info):
+ return resolve_attribute_value_type(self.value)
+
class ProductAttribute(CountableDjangoObjectType):
name = graphene.String(description='Visible name for display purposes.')
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 82b0624019..26d4136cc9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -345,6 +345,7 @@ input AttributeChoiceValueInput {
attribute: ID
slug: String!
name: String!
+ value: String!
}
type AttributeChoiceValueUpdate {
@@ -1119,6 +1120,13 @@ type Product implements Node {
imageById(id: ID): ProductImage
}
+enum ProductAttribuetValueType {
+ COLOR
+ GRADIENT
+ URL
+ STRING
+}
+
type ProductAttribute implements Node {
id: ID!
slug: String
@@ -1156,7 +1164,9 @@ type ProductAttributeValue implements Node {
id: ID!
sortOrder: Int!
name: String
+ value: String!
slug: String
+ type: ProductAttribuetValueType
}
input ProductAttributesInput {
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 455fbd69e2..6cd83fab71 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -1,4 +1,5 @@
import json
+from unittest.mock import Mock
import graphene
import pytest
@@ -7,6 +8,7 @@ from tests.utils import get_graphql_content
from saleor.product.models import (
Category, ProductAttribute, AttributeChoiceValue)
from saleor.graphql.product.utils import attributes_to_hstore
+from saleor.graphql.product.types import resolve_attribute_value_type, ProductAttribuetValueType
def test_attributes_to_hstore(product, color_attribute):
@@ -165,12 +167,14 @@ def test_delete_product_attribute(admin_api_client, color_attribute):
def test_create_attribute_choice_value(admin_api_client, color_attribute):
attribute = color_attribute
query = """
- mutation createChoice($attribute: ID!, $name: String!, $slug: String!) {
+ mutation createChoice($attribute: ID!, $name: String!, $slug: String!, $value: String!) {
attributeChoiceValueCreate(
- input: {attribute: $attribute, name: $name, slug: $slug}) {
+ input: {attribute: $attribute, name: $name, slug: $slug, value: $value}) {
attributeChoiceValue {
name
slug
+ type
+ value
}
}
}
@@ -178,8 +182,9 @@ def test_create_attribute_choice_value(admin_api_client, color_attribute):
attribute_id = graphene.Node.to_global_id('ProductAttribute', attribute.id)
name = 'test name'
slug = 'test-slug'
+ value = 'test-string'
variables = json.dumps(
- {'name': name, 'slug': slug, 'attribute': attribute_id})
+ {'name': name, 'slug': slug, 'value': value, 'attribute': attribute_id})
response = admin_api_client.post(
reverse('api'), {'query': query, 'variables': variables})
content = get_graphql_content(response)
@@ -188,6 +193,8 @@ def test_create_attribute_choice_value(admin_api_client, color_attribute):
'data']['attributeChoiceValueCreate']['attributeChoiceValue']
assert data['name'] == name
assert data['slug'] == slug
+ assert data['value'] == value
+ assert data['type'] == 'STRING'
def test_update_attribute_choice_value(admin_api_client, pink_choice_value):
@@ -239,3 +246,57 @@ def test_delete_attribute_choice_value(admin_api_client, color_attribute, pink_c
assert 'errors' not in content
with pytest.raises(value._meta.model.DoesNotExist):
value.refresh_from_db()
+
[email protected]('raw_value, expected_type', [
+ ('#0000', ProductAttribuetValueType.COLOR),
+ ('#FF69B4', ProductAttribuetValueType.COLOR),
+ ('rgb(255, 0, 0)', ProductAttribuetValueType.COLOR),
+ ('hsl(0, 100%, 50%)', ProductAttribuetValueType.COLOR),
+ ('hsla(120, 60%, 70%, 0.3)', ProductAttribuetValueType.COLOR),
+ ('rgba(100%, 255, 0, 0)', ProductAttribuetValueType.COLOR),
+ ('http://example.com', ProductAttribuetValueType.URL),
+ ('https://example.com', ProductAttribuetValueType.URL),
+ ('ftp://example.com', ProductAttribuetValueType.URL),
+ ('example.com', ProductAttribuetValueType.STRING),
+ ('Foo', ProductAttribuetValueType.STRING),
+ ('linear-gradient(red, yellow)', ProductAttribuetValueType.GRADIENT),
+ ('radial-gradient(#0000, yellow)', ProductAttribuetValueType.GRADIENT),
+])
+def test_resolve_attribute_value_type(raw_value, expected_type):
+ assert resolve_attribute_value_type(raw_value) == expected_type
+
+
+def test_query_attribute_values(
+ color_attribute, pink_choice_value, user_api_client):
+ attribute_id = graphene.Node.to_global_id(
+ 'ProductAttribute', color_attribute.id)
+ query = """
+ query getAttribute($id: ID!) {
+ attributes(id: $id) {
+ edges {
+ node {
+ id
+ name
+ values {
+ name
+ type
+ value
+ }
+ }
+ }
+ }
+ }
+ """
+ variables = json.dumps({'id': attribute_id})
+ response = user_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['attributes']['edges'][0]['node']
+ values = data['values']
+ pink = [v for v in values if v['name'] == pink_choice_value.name]
+ assert len(pink) == 1
+ pink = pink[0]
+ assert pink['value'] == '#FF69B4'
+ assert pink['type'] == 'COLOR'
+
diff --git a/tests/conftest.py b/tests/conftest.py
index f37c83b055..7878125471 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -255,7 +255,7 @@ def color_attribute(db): # pylint: disable=W0613
@pytest.fixture
def pink_choice_value(color_attribute): # pylint: disable=W0613
value = AttributeChoiceValue.objects.create(
- slug='pink', name='Color', attribute=color_attribute)
+ slug='pink', name='Pink', attribute=color_attribute, value='#FF69B4')
return value