I tried to find the internet for a tutorial using Apollo Server 2 with Passport.js and I couldn't find, perhaps because it's new and people are still using apollo-server-express.
It's really confusing for me and that is why I'm asking for you guys is there's somewhere I can find a tutorial for that.
Hello @hasusozam ,
I'm using passport with apollo to make an API and this is how I did :
server.js:
const app = new Koa();
const server = new ApolloServer(graphQLConfig); // graphQLConfig is an exported object
app.use(router.routes()); // using koa-router
server.applyMiddleware({ app });
router.js:
import Router from 'koa-router';
import passport from 'passport';
const router = new Router();
router
.post('/graphql', async (ctx, next) => {
await passport.authenticate('bearer', { session: false }, async (err, response, info, status) => {
//do your stuff here
return await next();
})(ctx);
})
.get('/graphql', async (ctx, next) => {
return await next();
})
;
export default router;
I don't know if it can help you but it works very well. The hardest concept to understand is to pass the koa context to the graphql one like this(graphqlconfig file):
context: ({ ctx }) => ctx,
After a while and reading the actual source code I understood the concept of the apollo-server package, it's just a wrapper around apollo-server-express, it's pretty much a graphql-yoga. The thing is that if you want to use middleware you actually have to use apollo-server-express as apollo-server doesn't have the applyMiddleware anymore.
I really liked your example, I'm trying to implement passport + passport-jwt.
Nice I will be interessed in your implementation of passport-jwt. I will thankful if you could share it ;D
Good luck
I'm trying, but it's not as I want it to be, at least not yet.
This is my implementation so far, but I don't to make the endpoint closed as it is, I want to make it open and check if there's a user in the context.
@hasusozam is not posible to use just Apollo-server without apollo-server-express?
im trying also to merge but a think is imposible to handle the request of passport because it can be added to use in just apollo-server.
@pablocarreraest if need to use express middleware, will need to use apollo-server-express. apollo-server 2.x does not use express underneath the hood
Hi @hasusozam! Thanks for reporting this issue. We won't be directly maintaining integrations with specific frameworks in Apollo Server 3, so closing this issue. For questions like this you can head over here: https://spectrum.chat/apollo where there are community members who might be able to relate to a similar problem, or might be able to help you out more interactively! :)
Most helpful comment
After a while and reading the actual source code I understood the concept of the
apollo-serverpackage, it's just a wrapper aroundapollo-server-express, it's pretty much agraphql-yoga. The thing is that if you want to use middleware you actually have to useapollo-server-expressasapollo-serverdoesn't have theapplyMiddlewareanymore.I really liked your example, I'm trying to implement
passport+passport-jwt.