-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid type for ContextFunction? #1593
Labels
🧬 typings
Relates to TypeScript changes or improvements.
Comments
A solution for me was, create a type: type Req = { req: IncomingMessage }
const server = new ApolloServer({
schema,
context: async({req}: Req) => {
const authorization = req.headers.authorization or you can use const server = new ApolloServer({
schema,
context: async(incomingContext: { req: IncomingMessage } => {
const authorization = incomingContext.req.headers.authorization and you can use this, but type of req is not a Request context: async(contextExpress: {req: express.Request }) => {
console.log(contextExpress.req instanceof IncomingMessage) // print true |
cheapsteak
added
🧬 typings
Relates to TypeScript changes or improvements.
and removed
📝 documentation
Focuses on changes to the documentation (docs)
labels
Feb 13, 2019
4 tasks
4 tasks
4 tasks
PRs for this seem to have been merged, should the fix be published and the issue closed? |
Yep, this should have been closed a while ago. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm new to Apollo and relatively new to TypeScript, so maybe I'm missing something. I'm trying to get my types straight in my
ApolloServer
constructor. The documentation for the constructor says that thecontext
parameter takes an untyped object with a single keyreq
, like so:However, in
types.d.ts
,context
is defined as takingcontext?: Context<any> | ContextFunction<any>;
, whereContextFunction
is:This typing is claiming that the context creating function takes a context, not just returns one. Since
req
is not typed to be a member ofcontext
, TypeScript says I'm doing something wrong.Also, from reading the documentation, it really sounds like the return type should be
Context<T> | Promise<Context<T>>
; it doesn't HAVE to be an async function, does it?If my take on this issue is correct, could you please make a proper type for the callback function's first parameter, and update the typings? And also, if you could mention this type in the documentation, and also document that
req
is anhttp.IncomingMessage
, I'd be very grateful! (as a newcomer to Node, I've noticed that a lot of node documentation makes assumptions such as knowing that incoming requests in any node library is usually a leaked type from the node standard library).Something like:
Thanks!
The text was updated successfully, but these errors were encountered: