Try to search your question here, if you can't find : Ask Any Question Now ?

Right way to have Authentication and PubSub in GraphQLServer

HomeCategory: stackoverflowRight way to have Authentication and PubSub in GraphQLServer
sourav asked 6 days ago

I’m trying to use subscriptions on my GraphQL server. The problem I’m facing is that I can’t have both a middleware to extract my JWT and a PubSub when initializing the GraphQLServer.

This is what I have:

const server = new GraphQLServer({
    schema,
    context: ({request}) => extractJWT(request),
  });

And it works just fine with this:

@Mutation(returns => User)
    public async findUser(
        @Ctx() context: IContext,
        @PubSub() pubsub: PubSubEngine,
    ) {
        const user = await User.findById(context.tokenData.userId)
        pubsub.publish('user', { user })
        return user
    }

But if I change my code to the below, I can’t any longer access what I have in my @Ctx decorators.

const pubSub = new PubSub();
const server = new GraphQLServer({
    schema,
    context: {
       ctx: ({request}) => extractJWT(request),
       pubSub
  },
  });

What is the proper way to initialize my GraphQLServer using both JWT extraction middleware and the PubSub?

1 Answers
Best Answer
Arben answered 6 days ago
Your Answer

9 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com