graphql-redis-subscriptions icon indicating copy to clipboard operation
graphql-redis-subscriptions copied to clipboard

RedisPubSub works different from nestjs pubsub. How to serialize?

Open Zony-Zhao opened this issue 4 years ago • 3 comments

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;
}

Zony-Zhao avatar Dec 22 '20 02:12 Zony-Zhao

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`);
  }

otroboe avatar Jan 12 '21 19:01 otroboe

Here is what I found great: image 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. image At last use IOC. Inject the redispubsub in the providers, you can then use it in the controller, resolver etc.

Zony-Zhao avatar May 07 '21 01:05 Zony-Zhao

Hey @Zony-Zhao @otroboe Does this issue belong here? Would you suggest any change to this package?

Thanks ahead

davidyaha avatar May 15 '21 19:05 davidyaha