Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access PubSub in middleware #35

Closed
valerii15298 opened this issue Feb 9, 2022 · 2 comments
Closed

Access PubSub in middleware #35

valerii15298 opened this issue Feb 9, 2022 · 2 comments
Labels
question Further information is requested

Comments

@valerii15298
Copy link

Hello, i am trying to access pubSub in middleware but cannot make it work. In documentation, I also did not find anything about it.
Is it possible to access pubSub inside of middleware?
Here is my code

@Injectable()
export class LogAccess implements MiddlewareInterface {
  constructor(@PubSub() private pubSub: PubSubEngine) {}

  async use(data: ResolverData, next: NextFn): Promise<any> {
    console.log({ pubSubInMiddleware: this.pubSub });
    console.log('Middleware');
    return next();
  }
}

@Resolver(String)
class CustomUserResolver {
  @Query((returns) => String, { nullable: true })
  @UseMiddleware(LogAccess)
  async bestUser(@Ctx() { prisma }: Context, @PubSub() pubSub: PubSubEngine) {
    // console.log({ pubSub });
    return 'You are the best of the best!!:)))';
  }
}

@Module({
  imports: [
    // use the TypeGraphQLModule to expose Prisma by GraphQL
    TypeGraphQLModule.forRoot({
      playground: true,
      introspection: true,
      emitSchemaFile,
      validate: false,
      context: () => ({ prisma }),
      globalMiddlewares: [LogAccess],
    }),
  ],
  controllers: [],
  providers: [CustomUserResolver, LogAccess, PubSubEngine as any],
})
export class AppModule {}

I am trying to use PubSubEngine, but since it is an abstract class, in middleware it is just an empty object :(

@MichalLytek
Copy link
Owner

https://typegraphql.com/docs/subscriptions.html#using-a-custom-pubsub-system

@valerii15298
Copy link
Author

Yep. I just thought maybe there is some common way to access it from middleware and I am doing something wrong.
But this way with custom pubsub it works for sure, of course. Thanks.

@MichalLytek MichalLytek added the question Further information is requested label Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants