Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

466
Views
Cannot get sessions in apollo server context?

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.

How can I solve that?

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();
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Don´t deconstruct req work for me:

 context: (req, res) => {
    return {
      req: req,
      res: res,
    }
};
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!