I am using Koa, Apollo and Passport and I am having difficulty accessing the Passport user from the req.user within the Apollo Resolver. I also have a simple REST Endpoint. When I call ctx.req.user from a rout along the rest endpoint it gives me back the username, email etc.
However, the same req.user statement within the Apollo Resolver comes back as undefined. If I just call ctx.req by itself then I can log the full request to the console that includes the cookie/session.
I suspect (mostly because I have tried everything else) that it might be happening because I created the Apollo Server in my app.ts file before applying the Passport Middleware. However, I'm not sure this is the case and Idon't know what change to make anyway. I'm also slow to pull my mostly working codebase apart to change this.
//app.ts
import Koa = require('koa');
import { getGraphqlApp} from './graphql/get-apollo-server'
import { config } from './config/config';
import { BaseContext } from 'koa';
import * as passport from 'koa-passport';
//Create the Apollo Server and the Koa App
const server = getGraphqlApp();
const app = new Koa();
console.log()
//Apply the App onto the Apollo Server
server.applyMiddleware({ app, path: config.graphqlUri });
//Export the App so that we can import it in server.ts
module.exports = app;
//get-apollo-server.ts
import { makeAugmentedSchema } from 'neo4j-graphql-js';
import { ApolloServer } from 'apollo-server-koa';
import {typeDefs} from './get-graphql-schema'//../typeDefs';
import { getNeo4jDriver} from '../database/connection-neo4j'
import resolvers from './resolvers'
const driver = getNeo4jDriver();
export function getGraphqlApp(): ApolloServer {
const schema = makeAugmentedSchema({
typeDefs,
resolvers,
config: {
query: false,
mutation: false
}
//resolverValidationOptions: { requireResolversForResolveType: false }
});
const graphqlOptions = {
schema,
context: (
{ ctx }
) => {
return {
driver,
ctx
};
},
playground: true,
formatError: error => {
return error
},
introspection: true
};
return new ApolloServer(graphqlOptions);
}
//resolver.ts
import { neo4jgraphql } from "neo4j-graphql-js";
const resolvers = {
Query: {
Dataset(object, params, ctx, resolveInfo) {
return neo4jgraphql(object, params, ctx, resolveInfo);
},
DatasetAttributes(object, params, ctx, resolveInfo) {
return neo4jgraphql(object, params, ctx, resolveInfo);
},
Idiom(object, params, ctx, resolveInfo) {
console.log(ctx.ctx.req.user)
if (!1) {
throw new Error("request not authenticated");
} else {
return neo4jgraphql(object, params, ctx, resolveInfo);
}
},
}
};
export default resolvers;
The issue was that I had initiated the Apollo Server before I applied Passport to the App. I reworked the app.ts file as per below and removed the passport parts from server.ts to get this working:
import Koa = require('koa');
import { getGraphqlApp} from './graphql/get-apollo-server'
import { config } from './config/config';
import { BaseContext } from 'koa';
import * as session from 'koa-session';
import * as passport from 'koa-passport';
//Create the Apollo Server and the Koa App
const server = getGraphqlApp();
const app = new Koa();
//Setup the session
app.keys = ['infornite-secret-key'];
app.use(session(app));
//Setup Authentication
require('./config/auth');
app.use(passport.initialize());
app.use(passport.session());
//Apply the App onto the Apollo Server
server.applyMiddleware({ app, path: config.graphqlUri });
//Export the App so that we can import it in server.ts
module.exports = app;