diff --git a/tests/test_graphql_api/test_reservation/test_query.py b/tests/test_graphql_api/test_reservation/test_query.py index 0502732aa..0f2636d95 100644 --- a/tests/test_graphql_api/test_reservation/test_query.py +++ b/tests/test_graphql_api/test_reservation/test_query.py @@ -378,3 +378,29 @@ def test_reservation__query__order__all_fields(graphql): "expiresInMinutes": 5, } } + + +def test_reservation__query__reservation_unit_is_archived_but_data_is_still_returned_through_relation(graphql): + reservation = ReservationFactory.create(reservation_units__is_archived=True) + reservation_unit = reservation.reservation_units.first() + + graphql.login_with_superuser() + global_id = to_global_id("ReservationNode", reservation.pk) + + fields = "pk reservationUnits { pk isArchived }" + expected_response = { + "pk": reservation.pk, + "reservationUnits": [{"pk": reservation_unit.pk, "isArchived": True}], + } + + # Single + query = reservation_query(id=global_id, fields=fields) + response = graphql(query) + assert response.has_errors is False, response.errors + assert response.first_query_object == expected_response + + # All + query = reservations_query(fields=fields) + response = graphql(query) + assert response.has_errors is False, response.errors + assert response.node(0) == expected_response diff --git a/tests/test_graphql_api/test_reservation_unit/test_query_all.py b/tests/test_graphql_api/test_reservation_unit/test_query_all.py index 8ae06cb2c..a957e51d0 100644 --- a/tests/test_graphql_api/test_reservation_unit/test_query_all.py +++ b/tests/test_graphql_api/test_reservation_unit/test_query_all.py @@ -14,6 +14,7 @@ def test_reservation_unit_all__no_pagination_limit(graphql): graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 1 ReservationUnitFactory.create_batch(2) + ReservationUnitFactory.create(is_archived=True) # Should not be returned graphql.login_with_superuser() query = reservation_units_all_query(fields="pk nameFi nameEn nameSv") diff --git a/tilavarauspalvelu/api/graphql/types/reservation_unit/types.py b/tilavarauspalvelu/api/graphql/types/reservation_unit/types.py index 301443400..c86a2fd58 100644 --- a/tilavarauspalvelu/api/graphql/types/reservation_unit/types.py +++ b/tilavarauspalvelu/api/graphql/types/reservation_unit/types.py @@ -179,7 +179,10 @@ class Meta: @classmethod def filter_queryset(cls, queryset: models.QuerySet, info: GQLInfo) -> models.QuerySet: - # Always hide archived reservation units + # Allow fetching data for archived reservation units, through relations from ReservationNode + if getattr(info.return_type, "name", None) in ["ReservationNode", "ReservationNodeConnection"]: + return queryset + # Archived reservation units should not be directly visible in the API return queryset.filter(is_archived=False) def resolve_is_closed(root: ReservationUnit, info: GQLInfo) -> bool: