Skip to content

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

Fetch user address by its ID

Krzysztof Świątekaae54f5bfa18
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6d4d91ab7d..49dae2cb42 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -39,6 +39,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix voucher start date filtering - #5133 by @dominik-zeglen
- Search by sku in products query - #5117 by @fowczarek
- Send fulfillment update email - #5118 by @IKarbowiak
+- Add address query - #5148 by @kswiatek92
## 2.9.0
diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py
index f69ec9cf22..c96a3fe265 100644
--- a/saleor/graphql/account/resolvers.py
+++ b/saleor/graphql/account/resolvers.py
@@ -150,3 +150,14 @@ def prepare_graphql_payment_sources_type(payment_sources):
}
)
return sources
+
+
+def resolve_address(info, id):
+ user = info.context.user
+ service_account = info.context.service_account
+ _model, address_pk = graphene.Node.from_global_id(id)
+ if service_account:
+ return models.Address.objects.filter(pk=address_pk).first()
+ if user and not user.is_anonymous:
+ return user.addresses.filter(id=address_pk).first()
+ return PermissionDenied()
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index 5c24c91d4e..40db11ba3c 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -52,6 +52,7 @@ from .mutations.staff import (
UserUpdatePrivateMeta,
)
from .resolvers import (
+ resolve_address,
resolve_address_validation_rules,
resolve_customers,
resolve_service_accounts,
@@ -59,7 +60,7 @@ from .resolvers import (
resolve_user,
)
from .sorters import ServiceAccountSortingInput, UserSortingInput
-from .types import AddressValidationData, ServiceAccount, User
+from .types import Address, AddressValidationData, ServiceAccount, User
class CustomerFilterInput(FilterInputObjectType):
@@ -94,6 +95,13 @@ class AccountQueries(graphene.ObjectType):
graphene.String, description="Sublocality like a district."
),
)
+ address = graphene.Field(
+ Address,
+ id=graphene.Argument(
+ graphene.ID, description="ID of an address.", required=True
+ ),
+ description="Look up an address by ID.",
+ )
customers = FilterInputConnectionField(
User,
filter=CustomerFilterInput(description="Filtering options for customers."),
@@ -166,6 +174,9 @@ class AccountQueries(graphene.ObjectType):
def resolve_user(self, info, id):
return resolve_user(info, id)
+ def resolve_address(self, info, id):
+ return resolve_address(info, id)
+
class AccountMutations(graphene.ObjectType):
# Base mutations
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ef00850d71..592771493a 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3521,6 +3521,7 @@ type Query {
checkoutLine(id: ID): CheckoutLine
checkoutLines(before: String, after: String, first: Int, last: Int): CheckoutLineCountableConnection
addressValidationRules(countryCode: CountryCode!, countryArea: String, city: String, cityArea: String): AddressValidationData
+ address(id: ID!): Address
customers(filter: CustomerFilterInput, sortBy: UserSortingInput, before: String, after: String, first: Int, last: Int): UserCountableConnection
me: User
staffUsers(filter: StaffUserInput, sortBy: UserSortingInput, before: String, after: String, first: Int, last: Int): UserCountableConnection
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index d3581924bf..29924daefe 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -3002,3 +3002,53 @@ def test_change_active_status_for_himself(staff_api_client, permission_manage_us
data["errors"][0]["message"] == "Cannot activate or deactivate "
"your own account."
)
+
+
+ADDRESS_QUERY = """
+query address($id: ID!) {
+ address(id: $id) {
+ postalCode
+ lastName
+ firstName
+ city
+ country {
+ code
+ }
+ }
+}
+"""
+
+
+def test_address_query_as_owner(user_api_client, customer_user):
+ address = customer_user.addresses.first()
+ variables = {"id": graphene.Node.to_global_id("Address", address.pk)}
+ response = user_api_client.post_graphql(ADDRESS_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["address"]
+ assert data["country"]["code"] == address.country.code
+
+
+def test_address_query_as_not_owner(
+ user_api_client, customer_user, address_other_country
+):
+ variables = {"id": graphene.Node.to_global_id("Address", address_other_country.pk)}
+ response = user_api_client.post_graphql(ADDRESS_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["address"]
+ assert not data
+
+
+def test_address_query_as_service_account(
+ service_account_api_client, service_account, address_other_country
+):
+ variables = {"id": graphene.Node.to_global_id("Address", address_other_country.pk)}
+ response = service_account_api_client.post_graphql(ADDRESS_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["address"]
+ assert data["country"]["code"] == address_other_country.country.code
+
+
+def test_address_query_as_anonymous_user(api_client, address_other_country):
+ variables = {"id": graphene.Node.to_global_id("Address", address_other_country.pk)}
+ response = api_client.post_graphql(ADDRESS_QUERY, variables)
+ assert_no_permission(response)