System Information
- Strapi Version: 4.24.2
- Operating System: Windows
- Database:
- Node Version: v20.11.0
- NPM Version:
- Yarn Version: 1.22.4
I have currently setup pages to contain a Dynamic Zone called blocks
.
blocks
contains a component called upcomingEvents
This upcomingEvents
-component has a single field called upcomingEventsBlock
which is a has one
-relationship to a UpcomingEventsBlock
which is a Collection Type that looks like this:
I have this setup so I can re-use the same upcomingEventsBlock
for multiple pages without having to fill it in for every page. Right now I am retrieving the data with this query:
http://localhost:1337/api/pages?populate[blocks][populate][toWorkLink][populate][0]=internal_link&populate[blocks][populate]=photoArtist&populate[internal_link]=true&populate[blocks][populate][upcomingEventsBlock][populate][0]=upcomingEventsBlock
This populates all the fields and results in the following data:
{
"data": [
{
"id": 1,
"attributes": {
"title": "Home",
"createdAt": "2024-05-18T13:30:02.854Z",
"updatedAt": "2024-05-22T15:27:58.063Z",
"publishedAt": "2024-05-19T22:53:11.575Z",
"locale": "nl",
"blocks": [
{
"id": 4,
"__component": "sections.upcoming-events",
"upcomingEventsBlock": {
"data": {
"id": 1,
"attributes": {
"Title": "Opkommende events",
"createdAt": "2024-05-22T14:02:24.397Z",
"updatedAt": "2024-05-22T14:02:45.380Z",
"publishedAt": "2024-05-22T14:02:30.353Z",
"locale": "nl"
}
}
}
},
{
"id": 7,
"__component": "layout.footer",
"copyright": "Home",
"toWorkLink": {
"id": 2,
"variant": "primary",
"iconPlacement": "left",
"icon": "arrow-left",
"buttonLabel": null,
"title": "Home EN",
"externalLink": null,
"internal_link": {
"data": {
"id": 4,
"attributes": {
"slug": "/en",
"createdAt": "2024-05-21T11:14:20.474Z",
"updatedAt": "2024-05-21T11:14:21.579Z",
"publishedAt": "2024-05-21T11:14:21.574Z",
"title": "Home (EN)"
}
}
}
}
},
{
"id": 4,
"__component": "sections.about-artist",
"title": "Test Home",
"subtitle": null,
"text": null,
"photoArtist": {
"data": null
}
}
],
"internal_link": {
"data": {
"id": 2,
"attributes": {
"slug": "/",
"createdAt": "2024-05-20T14:53:15.070Z",
"updatedAt": "2024-05-20T14:58:41.662Z",
"publishedAt": "2024-05-20T14:58:41.659Z",
"title": "Home"
}
}
}
}
},
{
"id": 2,
"attributes": {
"title": "Test",
"createdAt": "2024-05-20T14:59:03.209Z",
"updatedAt": "2024-05-21T10:57:13.999Z",
"publishedAt": "2024-05-20T14:59:25.754Z",
"locale": "nl",
"blocks": [
{
"id": 2,
"__component": "layout.footer",
"copyright": "Test 2024",
"toWorkLink": null
}
],
"internal_link": {
"data": {
"id": 1,
"attributes": {
"slug": "/nl/test",
"createdAt": "2024-05-19T12:13:55.325Z",
"updatedAt": "2024-05-21T11:17:51.188Z",
"publishedAt": "2024-05-19T12:13:56.427Z",
"title": "Test"
}
}
}
}
},
{
"id": 3,
"attributes": {
"title": "Over kunstenaar",
"createdAt": "2024-05-20T16:15:22.476Z",
"updatedAt": "2024-05-22T14:05:39.869Z",
"publishedAt": "2024-05-20T16:15:26.023Z",
"locale": "nl",
"blocks": [
{
"id": 3,
"__component": "layout.footer",
"copyright": "Test 2024 about",
"toWorkLink": null
},
{
"id": 1,
"__component": "sections.upcoming-events",
"upcomingEventsBlock": {
"data": {
"id": 3,
"attributes": {
"Title": "Opkommende Events Lokaal",
"createdAt": "2024-05-22T14:03:37.479Z",
"updatedAt": "2024-05-22T14:03:49.593Z",
"publishedAt": "2024-05-22T14:03:38.519Z",
"locale": "nl"
}
}
}
},
{
"id": 2,
"__component": "sections.about-artist",
"title": "Test",
"subtitle": "Kunstenaar",
"text": null,
"photoArtist": {
"data": null
}
}
],
"internal_link": {
"data": {
"id": 3,
"attributes": {
"slug": "/nl/over-kunstenaar",
"createdAt": "2024-05-20T16:14:16.619Z",
"updatedAt": "2024-05-21T11:17:33.876Z",
"publishedAt": "2024-05-20T16:14:38.522Z",
"title": "Over Kunstenaar"
}
}
}
}
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 3
}
}
}
This works just fine but will be costly to request all blocks this way once the pages start to fill up with blocks that all require a relationship to be populated on retrieving a page.
What I would like to do is retrieve the page without populating my upcomingEventsBlock
and just retrieving the ID the item it is related to, which is an ID of 1
.
So instead of:
"blocks": [
{
"id": 4,
"__component": "sections.upcoming-events",
"upcomingEventsBlock": {
"data": {
"id": 1,
"attributes": {
"Title": "Opkommende events",
"createdAt": "2024-05-22T14:02:24.397Z",
"updatedAt": "2024-05-22T14:02:45.380Z",
"publishedAt": "2024-05-22T14:02:30.353Z",
"locale": "nl"
}
}
}
},
]
I would get something like:
"blocks": [
{
"id": 4,
"__component": "sections.upcoming-events",
"upcomingEventsBlock": 1
},
]
Then I could get the data in a separate call like this when rendering that component with:
http://localhost:1337/api/upcoming-events-blocks/1
Is this possible in Strapi as is? Or is there any possible workaround to get this working?
One workaround I can come up with is adding a text-field to the upcomingEvents
-component to hold the ID of the relationship. This way I have the ID without having to populate, but this would be messy for content editors as it could desync from the actual relationship configured to it.