onConnvect, graphql-ws subscription

34 views Asked by At

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.

0

There are 0 answers