apostrophecms / apostrophe

A full-featured, open-source content management framework built with Node.js that empowers organizations by combining in-context editing and headless architecture in a full-stack JS environment.
https://apostrophecms.com
Other
4.37k stars 590 forks source link

adds logic in relationship field validate to add to projection sub re… #4773

Closed ValJed closed 4 weeks ago

ValJed commented 1 month ago

PRO-6535

Summary

When validating relationship fields, if there withRelationships and builders.project are set, add needed idsStorage fields to project in order to be able to retrieve sub relationship.

New behavior

If withRelationships is set to \true we take all first level relationships,

          _pages: {
            label: 'Pages',
            type: 'relationship',
            withType: 'default-page',
            withRelationships: true,
            builders: {
              project: {
                title: 1,
                _url: 1
              }
            }
          }

In this case if the default-page has relationships to foodArticles and sportArticles, it will incluce both. But if foodArticles has a relationship to topics, it won't take it. this behavior already worked but was not documented, I just made sure it still works with missing projections.

If withRelationships is an array with one or multiple levels of relationships using the dot notation. for each one having a projection, we add the missing fields to build these relationships.

          _pages: {
            label: 'Rel page',
            type: 'relationship',
            withType: 'default-page',
            withRelationships: [ '_articles._topics' ],
            builders: {
              project: {
                title: 1
              }
            }
          }

In this case we will get pages, their articles and the topics of the articles. Even if the projections on each level do not include the needed relationship properties.

⚠️ It might change some existing behavior since if relationship is not explicitly false we take the first level of relationship even if the projection does not include the right field. I would argue that it's not a problematic change since it's the normal behavior when no projection.

What are the specific steps to test this change?

See Ticket description:

If you don't want to spend time configuring a project to test it, I created a branch for you: https://github.com/ValJed/a3-wp/tree/pro-6535-projections-sub-relationships

It reproduces the behavior described in the ticket.

What kind of change does this PR introduce?

Make sure the PR fulfills these requirements:

linear[bot] commented 1 month ago

PRO-6535 Projection not working with nested relationships