onConnvect, graphql-ws subscription

onConnvect, graphql-ws subscription


0

thank you for your time, my problem is that i want to return the data to the client immediately after subscribing. I know about the onConnect method, but it is called before JwtAuthGuard. What is the best way to do this?
I was thinking of moving the authorization logic to onConnect.

To better understand the problem, I will add the code of what logic I implement, but I don’t like this method:

  @UseGuards(JwtAuthGuard)
  @Subscription(() => Int)
  public async countUnprocessedConversations() {
    const count =
      await this.conversationService.getCountUnprocessedConversations();

    setTimeout(() =>
      this.pubSub.publish('countUnprocessedConversations', {
        countUnprocessedConversations: count,
      }),
    );

    return this.pubSub.asyncIterator('countUnprocessedConversations');
  }

Here is my graphql module:

    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      autoSchemaFile: true,
      playground: true,
      csrfPrevention: false,
      subscriptions: {
        'graphql-ws': {
          path: '/subscriptions',
        },
      },

      context: async (ctx) => {
        if (ctx.connectionParams) {
          return {
            req: {
              headers: { authorization: ctx.connectionParams.Authorization },
            },
          };
        }
        return ctx;
      },
    }),

I would like to get rid of setTimeout, and if possible, do it with graphql-ws.

New contributor

Riki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 2 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *