Skip to content
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

Closed
nevyn-lookback opened this issue Aug 29, 2018 · 3 comments
Closed

Invalid type for ContextFunction? #1593

nevyn-lookback opened this issue Aug 29, 2018 · 3 comments
Assignees
Labels
🧬 typings Relates to TypeScript changes or improvements.

Comments

@nevyn-lookback
Copy link

nevyn-lookback commented Aug 29, 2018

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 the context parameter takes an untyped object with a single key req, like so:

	context: ({ req }) => ({
		authScope: getScope(req.headers.authorization)
	}),

However, in types.d.ts, context is defined as taking context?: Context<any> | ContextFunction<any>;, where ContextFunction is:

    export declare type ContextFunction<T = any> = (context: Context<T>) => Promise<Context<T>>;

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 of context, 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 an http.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:

interface RequestContext { req: IncomingMessage }
declare type ContextFunction<T> = (requestContext: RequestContext) => Context<T> | Promise<Context<T>>;

Thanks!

  • Nevyn
@ghost ghost added the 📝 documentation Focuses on changes to the documentation (docs) label Aug 29, 2018
@rafageist
Copy link

rafageist commented Oct 16, 2018

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

@marcospgp
Copy link

PRs for this seem to have been merged, should the fix be published and the issue closed?

@JacksonKearl
Copy link

Yep, this should have been closed a while ago.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🧬 typings Relates to TypeScript changes or improvements.
Projects
None yet
Development

No branches or pull requests

5 participants