0

对于我正在开发的项目,一个玩家有多个游戏。但这些总是需要以组合的方式进行。

所以我想知道如何制作它,以便我可以将它作为一个名为games.

我做了类似的事情,fullName但我找不到建立组合关联的方法

@Table({
  timestamps: true
})
export class Player extends Model<Player> {

  @Column({ unique: 'compositeIndex' })
  firstName: string;

  @Column({ unique: 'compositeIndex' })
  lastName: string;

  @Column({ unique: 'compositeIndex' })
  memberId: string;

  // Assicioations
  @HasMany(() => Game, { as: 'player1-team1', foreignKey: 'player1Team1Id' })
  games1?: Game[];
  @HasMany(() => Game, { as: 'player1-team2', foreignKey: 'player1Team2Id' })
  games2?: Game[];
  @HasMany(() => Game, { as: 'player2-team1', foreignKey: 'player2Team1Id' })
  games3?: Game[];
  @HasMany(() => Game, { as: 'player2-team2', foreignKey: 'player2Team2Id' })
  games4?: Game[];

  @Column(DataType.VIRTUAL(DataType.STRING, ['firstName', 'lastName']))
  get fullName(this: Player): string {
    return `${this.firstName || ''} ${this.lastName || ''}`.trim();
  }

  get games() {
    return [...this.games1, ...this.games2, ...this.games3, ...this.games4];
  }
}

当前方案对象

  const playerType = new GraphQLObjectType({
    name: 'Player',
    description: 'A Player',
    fields: Object.assign(attributeFields(sequelizeInstance.models.Player), {
      games: {
        type: new GraphQLList(gameType),
        resolve: resolver(/* Some magic here? */),
      }
    })
  });

  return new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'RootQueryType',
      fields: {
        player: {
          type: playerType,
          args: {
            id: {
              description: 'id of the user',
              type: new GraphQLNonNull(GraphQLID)
            }
          },
          resolve: resolver(Player)
        }
      }
    })
  });
};
4

1 回答 1

0

找到了我的问题的“解决方案”我犯了“错误”,我已经制作了 4 个外键。通过创建多对多关系,我能够让一切按我的意愿工作。

但我仍然很好奇如何做到这一点......

于 2020-04-11T12:12:01.787 回答