I ran into that issue:
- I cannot get the access the session object in the request object inside the apollo server context, so I get undefined as output.
- Otherwise I can access the session object in the request object inside express routes as normal.
if you can solve that to my i would appreciate it !
import express, { Application, Request, Response } from 'express';
import bodyParser from "body-parser";
import helmet from "helmet";
import xss from 'xss';
import moment from "moment";
import passport from "passport";
import { connect } from "mongoose";
import "reflect-metadata";
import { ApolloServer } from 'apollo-server-express';
import { buildSchema, ResolverData } from "type-graphql";
// Import resolvers
import main_config from './main.config';
import AuthenticationRoutes from './Authentication/Authentication.routes';
import './Authentication/Authentication.strategies';
import {
topicResolver, docsResolver, courseResolver, articleResolver, projectIdeaResolver
} from './Graphql/Topics/Topics.resolvers';
import cookieSession from 'cookie-session';
import expressSession from 'express-session';
// const expGql = require("express-graphql");
const app : Application = express();
// Init cookie cookie-session
app.use(cookieSession({
keys : ["IDFVBHNIOVFFBUE"],
name : 'DBDIUN',
secret : "IDFVBHNIOVFFBUE"
}));
async function runapp (){
// ?nit passport app and routes
app.use(passport.initialize());
app.use(passport.session())
// Run apollo server
const apollo = new ApolloServer({
schema : await buildSchema({
resolvers : [
topicResolver, docsResolver, courseResolver, articleResolver, projectIdeaResolver
],
globalMiddlewares: [],
}),
context: ({ req, res }) =>{
console.log("context")
console.log(req.session.passport) // cannot get session object then Get undefined
return {
getUser: () => req.user,
logout: () => req.logout(),
}
},
playground : true
})
apollo.applyMiddleware({ app });
// Init body parser and helmet
app.use(helmet());
app.use(bodyParser.json());
app.use('/auth', AuthenticationRoutes)
}
runapp();
question from:
https://stackoverflow.com/questions/65893519/cannot-get-sessions-in-apollo-server-context 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…