How to increase relations depth in strapi API response?

Viewed 326

I use Strapi for building API for my small app. In a model, I have the following entities:

Article, Article Translation, Tag, Tag translation. One Article has Many Article Translations. Each Article Translation has Many Tags. Each Tag has Many Tags translations.

My problem is that when I make a GET-request to Article, I don't receive Tags translations. I suppose they are too deep for Strapi. If I just make a GET-request to Tag or to Tag Translation, the response is correct.

How can I increase the depth of relations responses?

2 Answers

You need to override the controller, something like this

module.exports = {
   /**
   * Promise to fetch all records
   *
   * @return {Promise}
   */
  find: async (ctx) => {
    return strapi.query('model-name').find(ctx.query, [
        "entries",
        "entries.image",
        "entries.expandable_children",
        "entries.expandable_children.image",
        "entries.expandable_children.localizations",
        "entries.localizations",
        "localizations"
    ]);
  },
  findOne: async (ctx) => {
    const { id } = ctx.params;
    return strapi.query('model-name').findOne({ id }, [
        "entries",
        "entries.image",
        "entries.expandable_children",
        "entries.expandable_children.image",
        "entries.expandable_children.localizations",
        "entries.localizations",
        "localizations"
    ]);
  },
};

Instead of overriding any controllers, you can try adding the Tag Translation field under the populate section of your request URL.

eg: populate[0]=article.tag.tag-translation

If you use GraphQL this would much easier. I used something similar in an app I developed and it worked fine.

Related