I have Fixed My Problem By This parts
here is the updated section
async function startApolloServer() {
// Required logic for integrating with Express
const app = express();
const RedisStore = connectRedis(session);
const redis = new Redis();
const httpServer = http.createServer(app);
app.set('trust proxy', process.env.NODE_ENV !== 'production')
//if you access it from localhost uncomment below lines
const whitelist = ['http://localhost:3000', 'https://studio.apollographql.com'];
let corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors({ credentials: true, origin: corsOptions }));
app.use(
session({
name: "qid",
store: new RedisStore({
client: redis,
disableTouch: true,
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 365 * 10, // 10 years
//if you access it from localhost uncomment below lines
// httpOnly: true,
//please un comment below codes if u run it production level
// sameSite: 'none', // csrf
// secure: false, // cookie only works in https
// secure:false,
},
saveUninitialized: false,
secret: "qowiueojwojfalksdjoqiwueo",
resave: false,
})
);
// Same ApolloServer initialization as before, plus the drain plugin.
const server = new ApolloServer({
schema:await buildSchema({
resolvers:[HelloResolver,ClientResolver,ProjectResolver],
validate:false
}),
context:({req,res}):MyContext => ({req,res,redis}),
// csrfPrevention: true,
// cache: 'bounded',
// plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
// More required logic for integrating with Express
await server.start();
const corsOpt={origin:"https://studio.apollographql.com",credentials:true}
server.applyMiddleware({
app,
cors:false,//change it to corsOpt if u want in localhost
// By default, apollo-server hosts its GraphQL endpoint at the
// server root. However, *other* Apollo Server packages host it at
// /graphql. Optionally provide this to match apollo-server.
});
// Modified server startup
await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
// app.listen(4000,()=>{
// console.log("server listening on port 4000")
// })
await connectDB()
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
}
startApolloServer().catch(error=>{
console.log(error)
})