【问题标题】:Sequlize combining has many association in single fieldSequelize 组合在单个字段中具有许多关联
【发布时间】:2020-07-24 02:04:09
【问题描述】:

对于我正在处理的项目,一个玩家有多个游戏。但这些总是需要结合使用。

所以我想知道如何制作它,以便我可以将它作为一个名为 games 的信号项放在我的 graphql 方案中。

我为 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)
        }
      }
    })
  });
};

【问题讨论】:

    标签: graphql sequelize.js sequelize-typescript graphql-sequelize


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多