Skip to content

Commit 5c8f8a7

Browse files
authored
Merge pull request #429 from ianmeigh/feature/add-default-site-filter-to-sites-query
Add `isDefaultSite` filter to sites query
2 parents 4f82fa0 + 57e833d commit 5c8f8a7

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Unreleased
22

3+
## [0.31.0] - 2026-04-21
4+
5+
### Added
6+
7+
- Add an `is_default_site` filter to the sites query ([#429](https://github.com/torchbox/wagtail-grapple/pull/429)) @ianmeigh
8+
39
## [0.30.0] - 2026-03-20
410

511
### Added
@@ -430,7 +436,8 @@
430436
- Improve field definition and under-the-hood implementation ([#28](https://github.com/torchbox/wagtail-grapple/pull/28))
431437
- Add conditional checks when resolving streamfield type ([#29](https://github.com/torchbox/wagtail-grapple/pull/29))
432438

433-
[unreleased]: https://github.com/torchbox/wagtail-grapple/compare/v0.30.0...HEAD
439+
[unreleased]: https://github.com/torchbox/wagtail-grapple/compare/v0.31.0...HEAD
440+
[0.31.0]: https://github.com/torchbox/wagtail-grapple/compare/v0.30.0...v0.31.0
434441
[0.30.0]: https://github.com/torchbox/wagtail-grapple/compare/v0.29.0...v0.30.0
435442
[0.29.0]: https://github.com/torchbox/wagtail-grapple/compare/v0.28.0...v0.29.0
436443
[0.28.0]: https://github.com/torchbox/wagtail-grapple/compare/v0.27.0...v0.28.0

docs/general-usage/graphql-types.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,19 @@ the ``sites`` or ``site`` field on the root query type. Available fields for the
155155
pages(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, id: ID): [PageInterface]
156156

157157

158-
The plural ``sites`` field can be queried like so:
158+
The plural ``sites`` field accepts the following arguments:
159+
160+
::
161+
162+
# Optionally filters sites by their default site status. Omitting this returns all sites.
163+
is_default_site: Boolean
164+
165+
and can be queried like so:
159166

160167
::
161168

162169
query {
163-
sites {
170+
sites (isDefaultSite: true) {
164171
port
165172
hostname
166173
}

grapple/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.30.0"
1+
__version__ = "0.31.0"

grapple/types/sites.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,24 @@ class Mixin:
7272
SiteObjectType, hostname=graphene.String(), id=graphene.ID()
7373
)
7474
sites = QuerySetList(
75-
graphene.NonNull(SiteObjectType), enable_search=True, required=True
75+
graphene.NonNull(SiteObjectType),
76+
enable_search=True,
77+
required=True,
78+
is_default_site=graphene.Argument(graphene.Boolean),
7679
)
7780

7881
def resolve_sites(self, info, **kwargs) -> QuerySet[Site]:
7982
"""
80-
Return all `Site` objects.
83+
Return all `Site` objects, optionally filtered to those where
84+
`is_default_site` matches the provided value.
8185
"""
8286

83-
return resolve_queryset(Site.objects.all(), info, **kwargs)
87+
qs = Site.objects.all()
88+
89+
if (is_default := kwargs.get("is_default_site")) is not None:
90+
qs = qs.filter(is_default_site=is_default)
91+
92+
return resolve_queryset(qs, info, **kwargs)
8493

8594
def resolve_site(self, info, **kwargs) -> Optional[Site]:
8695
"""

tests/test_grapple.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ def test_with_multisite(self):
742742

743743
class SitesTest(TestCase):
744744
def setUp(self):
745+
# Default site is created in testapp migration (002_create_homepage.py)
746+
745747
self.site = wagtail_factories.SiteFactory(
746748
hostname="grapple.localhost", site_name="Grapple test site"
747749
)
@@ -784,6 +786,28 @@ def test_sites(self):
784786
self.assertEqual(type(executed["data"]["sites"]), list)
785787
self.assertEqual(len(executed["data"]["sites"]), Site.objects.count())
786788

789+
def test_sites_is_default_site_filter(self):
790+
query = """
791+
query($isDefaultSite: Boolean) {
792+
sites(isDefaultSite: $isDefaultSite) {
793+
isDefaultSite
794+
}
795+
}
796+
"""
797+
total_non_default_sites = Site.objects.filter(is_default_site=False).count()
798+
cases = [
799+
(True, 1),
800+
(False, total_non_default_sites),
801+
]
802+
803+
for value, expected_count in cases:
804+
with self.subTest(isDefaultSite=value):
805+
results = self.client.execute(
806+
query,
807+
variables={"isDefaultSite": value},
808+
)
809+
self.assertEqual(len(results["data"]["sites"]), expected_count)
810+
787811
def test_site(self):
788812
query = """
789813
query($hostname: String) {

0 commit comments

Comments
 (0)