graphql-redis-subscriptions
graphql-redis-subscriptions copied to clipboard
RedisPubSub works different from nestjs pubsub. How to serialize?
I'm using mongodb and nestjs. The original pubsub(nestjs) would serialize the id filed for every model( which is the string of every _id) . But it is lost in redis pubsub, which break my app. How should I change the serialization with this lib?
What I think is something like:
reviver =(obj,key,value){
if(key==='_id'){
obj['id']=value.toString()
}
return value;
}
I have the same issue, the serialisation is not the same. In additional of the the _id => id, I lose my DateTime field which becomes null.
It's probably related to this issue: https://github.com/nestjs/graphql/issues/1117
Until I find a propre solution, I'm transforming my objects myself:
@Subscription(() => [ProfileAnswer], {
resolve: ({ profileAnswers }: Record<string, any>) =>
profileAnswers.map(({ _id, occuredAt, ...item }) => ({
id: _id,
occuredAt: new Date(occuredAt),
...item,
})),
})
@UseGuards(AccountAuthGuard)
@UseGuards(GqlAuthGuard)
profileAnswers(@Args('uuid') uuid: string): AsyncIterator<ProfileAnswer[]> {
// Subscribe only to own answers
return this.pubSub.asyncIterator<ProfileAnswer[]>(`${uuid}.answers`);
}
Here is what I found great:
As @otroboe did, I found id and dateTime field is missing or messed up.
And then I found mongoose document( what you get from this.userModel.findOne({})) has a method toJSON, which would put all the getters and virtual fields into the object(while JSON.stringify won't). With that, the ids will be added automaticlly.
At last use IOC. Inject the redispubsub in the providers, you can then use it in the controller, resolver etc.
Hey @Zony-Zhao @otroboe Does this issue belong here? Would you suggest any change to this package?
Thanks ahead