Querying Dynamic Zones Only When Needed

System Information
  • Strapi Version: 3.5.3
  • Database: postgres
  • Node Version: 14.16.0
  • NPM Version: 6.14.11

Hi, I am trying to query dynamic zones in my custom endpoint. I dont want to query for dynamic zones when I’m fetching multiple. I will need it in only findOne.

If I add autoPopulate:false to the settings json of the model then I’m not able to populate inside my controller either by columns or withRelated using bookshelf query.

If I remove autoPopulate:false from settings json then it populates it even if i ask for specific columns e.g. see the code snippet below, here meet_city is the dynamic zone i’ve:

{
  "kind": "collectionType",
  "connection": "default",
  "collectionName": "cities",
  "info": {
"name": "City",
"description": ""
  },
  "options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true
  },
  "attributes": {
"activities": {
  "autoPopulate": false,
  "collection": "activity",
  "via": "cities"
},
"name": {
  "type": "string",
  "required": true
},
"regions": {
  "autoPopulate": false,
  "collection": "region",
  "via": "cities"
},
"description": {
  "type": "text"
},
"latitude": {
  "type": "float"
},
"longitude": {
  "type": "float"
},
"images": {
  "collection": "file",
  "via": "related",
  "allowedTypes": ["images", "files", "videos"],
  "plugin": "upload",
  "required": false
},
"province": {
  "model": "province",
  "via": "cities"
},
"slug": {
  "type": "string",
  "unique": true
},
"meet_city": {
  "type": "dynamiczone",
  "components": [
    "travel-guide.list-text-w-header-icons",
    "travel-guide.calendar",
    "travel-guide.structured-text",
    "travel-guide.blog-carousel",
    "travel-guide.list-text-w-small-icons",
    "travel-guide.large-numbered-list",
    "travel-guide.images",
    "travel-guide.text",
    "travel-guide.title",
    "travel-guide.text-w-below-images"
  ]
},
"menu_image": {
  "model": "file",
  "via": "related",
  "allowedTypes": ["images", "files", "videos"],
  "plugin": "upload",
  "required": false
}
  }
}

Query:

 const cities = await strapi
      .query("city")
      .model.query((qb) => {
        qb.innerJoin("provinces", "cities.province", "=", "provinces.id");
        qb.innerJoin("countries", "provinces.country", "=", "countries.id");
        qb.whereNot({ "cities.published_at": null })
          .whereNot({
            "cities.province": null,
          })
          .andWhere("countries.slug", "=", country);
      })
      .fetchAll({
        withRelated: [
          {
            images: function (qb) {},
          },
          {
            province: function (qb) {
              qb.columns("provinces.id", "provinces.name", "provinces.slug");
            },
          },
        ],
        columns: [
          "cities.id",
          "cities.name",
          "cities.latitude",
          "cities.longitude",
          "cities.slug",
          "cities.description",
          "cities.province",
        ],
      });

What I need is:

  • Either I can autoPopulate false the dynamic zone and fetch it only when needed.

OR

  • Keep auto populating on but dont fetch in case of some specific endpoints.

It would be great if someone can provide some direction on this.

1 Like