graphql - GraphQL 订阅特定房间的(按 roomId)设置更改
问题描述
我想使用 gql 订阅来观察游戏室的设置变化。我有具有唯一房间代码的房间列表。每个房间都有设置属性。
例子:
class Room {
roomCode: string;
settings: {
difficulty
otherSetting
}
}
现在我想订阅房间的设置更改(NestJS 示例):
@Subscription((type) => RoomSetting)
async roomSettingsChanged(@Args("roomCode") roomCode: RoomCode) {
return this.pubSub.asyncIterator("roomSettingsChanged");
}
@Mutation((type) => RoomSetting)
async changeRoomSettings(
@Args("roomCode") roomCode: RoomCode,
@Args("RoomSettingsData") roomSettingsData: RoomSettingsData
): Promise<RoomSetting> {
const newRoomSettings = await this.roomsSerive.changeRoomSettings(
roomCode,
roomSettingsData
);
this.pubSub.publish("roomSettingsChanged", {
roomSettingsChanged: newRoomSettings,
});
return newRoomSettings;
}
RoomSettings 的外观如下:
RoomSettings {
difficulty: "hard",
otherSetting: "blabla"
}
现在,我如何在订阅中使用我传入订阅参数的 RoomCode 识别某些房间设置属于房间?我可以在 pubSub.publish 的一些额外变量中传递 roomCode 吗?或者,也许在 pubSub 中,我应该通过设置传递整个 Room 对象(那么如何在订阅解析器中仅获取设置)?RoomSettings 没有任何标识值(只是属于具有唯一 roomCode 的 Room)。我想实现这种情况:
- Someone subscribe to "roomSettingsChanged" and pass roomCode as argument
- When someone change room settings then the client, who subscribed to that room's settings change get notified with updates values.
解决方案
I probably found solution that seems to work as expected. Attach roomCode in subscription trigger string, eg:
this.pubSub.asyncIterator("roomSettingsChanged" + roomCode);
推荐阅读
- laravel-5.5 - local.ERROR:SQLSTATE [22003]:数值超出范围:1264 第 1 行列“尝试”的值超出范围
- tinymce - 输入tinymce编辑器后如何删除任何类似html的标签
- react-native - 在抽屉导航中显示动态信息 - React Native
- azure - 为 Azure 应用服务创建站点扩展并添加到库
- python - 将损失值传递给 Keras 中的其他模型
- django - 在 python virtualenv 中安装了 django-redis,找不到 redis-cli 命令
- logging - 如何仅从 Google Apps 脚本中的 getDate() 获取时间?
- angular - npm 错误!解析时 JSON 输入意外结束
- c++ - Visual Studio 2017 看不到包含文件和文件夹
- binary-search - 找到将数组划分为 2 个子数组的索引,其和的绝对差最小