如何正确处理同名反向关系

How to properly handle a reverse relationship with the same name

我遇到的情况是有多个片段具有相同的字段。然而,当反向关系时,它成为一个问题,因为它们似乎无法共存:

{
  name: '_relatedPeople',
  type: 'joinByArrayReverse',
  withType: 'organization',
  filters: {
    projection: {
      _url: 1,
      title: 1,
      tags: 1
    }
  },
},
{
  name: '_relatedPeople',
  type: 'joinByArrayReverse',
  withType: 'event',
  filters: {
    projection: {
      _url: 1,
      title: 1,
      tags: 1
    }
  },
},

事件类型覆盖组织类型。这是这两种类型都具有的字段(对于 DRY):

{
  "label": "Related People",
  "help": "",
  "name": "_relatedPeople",
  "type": "joinByArray",
  "withType": ["person"],
  "filters": {
    "projection": {
      "_url": 1,
      "title": 1
    }
  }
}

我尝试使用 idsFieldreverseOf,但都失败了。 person类型能够得到其他两种类型的反向关系的合适策略是什么?

我在筛选 ApostropheCMS 文档时通过反复试验找到了答案。我想我会在这里分享我的解决方案,以防其他人遇到相同类型的问题:

{
  name: '_organizations', // type from which to get the pieces, prefixed with "_" and suffixed with "s"
  reverseOf: '_relatedPeople', // field name from which to get the pieces
  type: 'joinByArrayReverse',
  withType: 'organization', // type which holds the pieces
  filters: {
    projection: {
      _url: 1,
      title: 1
    }
  },
},
{
  name: '_events', // type from which to get the pieces, prefixed with "_" and suffixed with "s"
  reverseOf: '_relatedPeople', // field name from which to get the pieces
  type: 'joinByArrayReverse',
  withType: 'event', // type which holds the pieces
  filters: {
    projection: {
      _url: 1,
      title: 1
    }
  },
},

注意 namereverseOfwithType 属性,这样可以确保避免覆盖结果。

然后可以使用 data.piece._organizationsdata.piece._events 并获得每个 _relatedPeople