Repository: saleor/saleor

Issue:
Have Address.country return CountryDisplay instead of AddressCountry?
Currently `Address.country` returns an `AddressCountry` enum value which means the client has to query `Shop.countries` to display the name of the country. We should consider having `Address.country` return a `CountryDisplay` object instead.


Discussion:

Resolving patch:
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -4,7 +4,8 @@
 
 from ...account import models
 from ...core.permissions import get_permissions
-from ..core.types.common import CountableDjangoObjectType, PermissionDisplay
+from ..core.types.common import (
+    CountableDjangoObjectType, CountryDisplay, PermissionDisplay)
 from ..utils import format_permissions_for_display
 
 
@@ -23,12 +24,19 @@ class AddressInput(graphene.InputObjectType):
 
 
 class Address(CountableDjangoObjectType):
+    country = graphene.Field(
+        CountryDisplay, required=True, description='Default shop\'s country')
+
     class Meta:
         exclude_fields = ['user_set', 'user_addresses']
         description = 'Represents user address data.'
         interfaces = [relay.Node]
         model = models.Address
 
+    def resolve_country(self, info):
+        return CountryDisplay(
+            code=self.country.code, country=self.country.name)
+
 
 class User(CountableDjangoObjectType):
     permissions = graphene.List(PermissionDisplay)


Test patch:
diff --git a/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap b/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
--- a/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
+++ b/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
@@ -1095,7 +1095,7 @@ exports[`Storyshots Generics / AddressEdit default 1`] = `
           <input
             name="country"
             type="hidden"
-            value="Swaziland"
+            value="SW"
           />
           <svg
             aria-hidden="true"
@@ -4721,9 +4721,7 @@ exports[`Storyshots Orders / OrderCustomer when loaded data 1`] = `
       </p>
       <p
         class="MuiTypography-root-id MuiTypography-body1-id"
-      >
-        Cyprus
-      </p>
+      />
     </div>
     <hr
       class="OrderCustomer-hr-id"
@@ -4994,9 +4992,7 @@ exports[`Storyshots Orders / OrderCustomer with different addresses 1`] = `
       </p>
       <p
         class="MuiTypography-root-id MuiTypography-body1-id"
-      >
-        Cyprus
-      </p>
+      />
     </div>
     <hr
       class="OrderCustomer-hr-id"
@@ -5054,9 +5050,7 @@ exports[`Storyshots Orders / OrderCustomer with different addresses 1`] = `
       </p>
       <p
         class="MuiTypography-root-id MuiTypography-body1-id"
-      >
-        Cyprus
-      </p>
+      />
     </div>
   </div>
 </div>
@@ -17541,9 +17535,7 @@ exports[`Storyshots Views / Orders / Order details as a draft 1`] = `
             </p>
             <p
               class="MuiTypography-root-id MuiTypography-body1-id"
-            >
-              Cyprus
-            </p>
+            />
           </div>
           <hr
             class="OrderCustomer-hr-id"
@@ -18566,9 +18558,7 @@ exports[`Storyshots Views / Orders / Order details as a unpaid order 1`] = `
             </p>
             <p
               class="MuiTypography-root-id MuiTypography-body1-id"
-            >
-              Cyprus
-            </p>
+            />
           </div>
           <hr
             class="OrderCustomer-hr-id"
@@ -19563,9 +19553,7 @@ exports[`Storyshots Views / Orders / Order details when loaded data 1`] = `
             </p>
             <p
               class="MuiTypography-root-id MuiTypography-body1-id"
-            >
-              Cyprus
-            </p>
+            />
           </div>
           <hr
             class="OrderCustomer-hr-id"
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -110,9 +110,11 @@ def test_query_user(admin_api_client, customer_user):
                 city
                 cityArea
                 postalCode
-                country
                 countryArea
                 phone
+                country {
+                    code
+                }
             }
         }
     }
@@ -139,7 +141,7 @@ def test_query_user(admin_api_client, customer_user):
     assert address['city'] == user_address.city
     assert address['cityArea'] == user_address.city_area
     assert address['postalCode'] == user_address.postal_code
-    assert address['country'] == user_address.country.code
+    assert address['country']['code'] == user_address.country.code
     assert address['countryArea'] == user_address.country_area
     assert address['phone'] == user_address.phone.as_e164
 
@@ -689,7 +691,9 @@ def test_create_address_mutation(admin_api_client, customer_user):
          address {
             id
             city
-            country
+            country {
+                code
+            }
          }
         }
     }
@@ -703,7 +707,7 @@ def test_create_address_mutation(admin_api_client, customer_user):
     assert content['data']['addressCreate']['errors'] == []
     address_response = content['data']['addressCreate']['address']
     assert address_response['city'] == 'Dummy'
-    assert address_response['country'] == 'PL'
+    assert address_response['country']['code'] == 'PL'
     address_obj = Address.objects.get(city='Dummy')
     assert address_obj.user_addresses.first() == customer_user