对于我正在开发的项目,一个玩家有多个游戏。但这些总是需要以组合的方式进行。
所以我想知道如何制作它,以便我可以将它作为一个名为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)
}
}
})
});
};