Reputation: 123
I am following ben's typescript GraphQL Redis tutorial I am new to typescript and first time trying redis when I add the property req.session.userId= user.id;
TypeError: Invalid argument type at encodeCommand (C:\Users\vikash patel\Desktop\reddit\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17:19
my main.ts code
declare module "express-session" {
interface SessionData {
userId: number | undefined;
}
}
import connectRedis from "connect-redis";
import { MyContext } from "./types";
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
const app = express();
const RedisStore = connectRedis(session);
const redisClient = redis.createClient() as any;
await redisClient.connect();
console.log("redis connected",redisClient.isOpen);
app.use(
session({
name: "qid",
store: new RedisStore({
client: redisClient,
disableTouch: true,
// disableTTL: true,
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 365, //1year
httpOnly: true,
sameSite: "lax", //protecting csrf
// secure:__prod__ //cookie only works in https
},
secret: "hellovikash",
resave: false,
saveUninitialized: false,
})
);
const apolloserver = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, PostResolver, UserResolver],
validate: false,
}),
context: ({ req, res }): MyContext => ({ em: orm.em, req, res }),
});
await apolloserver.start();
apolloserver.applyMiddleware({ app });
// app.get("/",(_,res)=>{
// res.send("ehllo")
// })
app.listen(4000, () => console.log("server is running on port 4000"));
my user.ts
@Mutation(() => USerResponse)
async login(@Arg('options', () => UsernamePasswordInput) options:
UsernamePasswordInput,
@Ctx() { em, req }: MyContext
): Promise<USerResponse> {
const user = await em.findOne(User, { username: options.username.toLowerCase() })
if (!user) {
return {
errors: [{
field: "username",
message: "could not find username"
}]
}
}
const valid = await argon2.verify(user.password, options.password);
if (!valid) {
return {
errors: [{
field: "username",
message: "password is incorrect"
}]
}
}
req.session.userId= user.id; //This is the line which is creating error
return {
user
};
}
my types.ts
import { EntityManager, IDatabaseDriver, Connection } from "@mikro-orm/core"
import { Request, Response } from "express"
export type MyContext = {
em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>
// req: Request;
req: Request & { session: Express.SessionStore };
res: Response;
}
I am really dumb...please help :)
Upvotes: 2
Views: 4150
Reputation: 459
If you're coming here in 2023 using [email protected], it can be tiring especially if you require backward compatibility.
In my case, I was previously using [email protected]. Here's a quick solution.
import { createClient } from "redis";
const redis_url: string = REDIS_URL;
const a = URL.parse(redis_url);
const redisClient: any = createClient({
url: a.href,
legacyMode: true // Add this to createClient
});
Upvotes: 4
Reputation: 61
You need to create the Redis client with legacyMode
enabled:
const redisClient = createClient({ legacyMode: true });
It's a limitation of connect-redis
package if you are using the newest redis v4 (https://github.com/tj/connect-redis).
Upvotes: 6