I am trying to create a GraphQL server with express using typescript using graphql-http package. No other package i want to use like Apollo-server, etc.
The issue is whenever i an trying to set the user session using server, through login mutation, i am getting some type error from ts server.
I am using grapgql-http & express with typescript to create a GraphQL server. Everything is working fine with it, the issue i come up with is when setting the session for user from server call.
I am using express-session with express to initialize the session, below is the code for it:
const sessionOption: SessionOptions = {
secret: process.env.COOKIE_SECRET as string,
name: "sid",
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
},
};
app.use(session(sessionOption));
Also i am setting the user with session like below:
declare module "express-session" {
interface SessionData {
user: UserObj | {};
}
}
Everything till now is ok and i am able to use the session in middleware, the issue come up when i want to set the session from login mutation. I try the below code:
app.all(
"/graphql",
createHandler({
schema: executableSchema,
context(req: Request) {
return req;
},
})
);
createHandler take a HandlerOptions interface which contain schema, context, validate, etc.
The signature of context is:
context?: Context | ((req: Request<RequestRaw, RequestContext>, params: RequestParams) => Promise<Context | Response> | Context | Response);
When i tried to use context for session, i got error:
Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>) => Request<ParamsDictionary, any, any, ParsedQs, Record<...>>' is not assignable to type 'OperationContext | ((req: Request<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, RequestContext>, params: RequestParams) => OperationContext | ... 1 more ... | Promise<...>)'.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>) => Request<ParamsDictionary, any, any, ParsedQs, Record<...>>' is not assignable to type '(req: Request<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, RequestContext>, params: RequestParams) => OperationContext | ... 1 more ... | Promise<...>'.
I have tried everything & search all the docs for graphql-http package as well, but got no solution.
Can someone tell me whats to set the context so its dosen’t throw error.
BTW:
app.all(
"/graphql",
createHandler({
schema: executableSchema,
context(req: any) {
return req;
},
})
);
setting the “req:any” works but session is not initialize & throws undefined.
Ashwin Solanki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.