首页 > 解决方案 > 如何在graphql和sequelize中过滤三向多对多关系?

问题描述

我已经为后端设置了一个带有 express 和 sequelize-typescript 的 GraphQL Apollo 服务器。我在以下表/模型之间具有三向多对多关系:

Action{
    id: Int
    name: String
    participants:[Participant]
    participanttypes: [ParticipantType]
}
Participant{
    id: Int
    displayname: String
    actions: [Action]
    participanttypes: [ParticipantType]
}
ParticipantType{
   id: Int
   description: String
   actions: [Action]
   participants: [Participant]
}

使用第四个模型将所有三个连接在一起

ActionParticipants{
   id: Int
   action: Int
   participant: Int
   participanttype: Int
}

我无法使用 GraphQL 过滤上述数据结构。例如,我想获取一个 Action,然后是该 Action 的 Participants,然后是该 Action 上每个参与者的 ParticipantType 描述。例如,使用返回第一个动作的 getAction(id:1) 查询

getAction(id:1){
  name
  participants{
    displayName
    participanttypes{
      description
    }
  }
}

通过上面的查询,我将收到动作的名称,然后是该动作的参与者[名称]数组,最后是每个参与者的所有参与者类型描述。

但是,我想将参与者类型过滤为仅该操作的参与者。

标签: typescriptexpressgraphqlsequelize.jsapollo

解决方案


推荐阅读