question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript Context for Apollo Server

See original GitHub issue

Hey, I’m trying to make the switch over to using Passport to verify email/password logins using GraphQLLocalStrategy but I’m running into an issue with defining the type of my context. Previously my context looked like:

export interface Context {
    req: Request;
    res: Response;
    prisma: PrismaClient;
    smtpTransport: any;
};

And I’m now trying to update the context like:

export interface MyContext extends PassportContext<Context, ExpressRequest>{}

And build my context as follows:

const server = new ApolloServer({
        schema,
        playground: {
            settings: {
                "request.credentials": "same-origin",
            },
        },
        context: ({ req, res }): MyContext =>
            buildContext({
                req,
                res,
                prisma,
                smtpTransport,
            }),
    });

But I keep running into Property 'logOut' is missing in type 'Context<Request<ParamsDictionary>>' but required in type 'MyContext'.

How can I update the context type correctly? Thank you!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
gforgecommented, Jul 30, 2020

Sorry for the delayed response. The project where I had been using this package was put on hold and I got some new test-errors after updating the packages. First, if anyone stumbles upon issues with your Request not being valid, make sure that you have the correct definitions (see this issue) by deleting your lock-files and re-installing everything.

I’m not sure that I actually understand the original code example. The definition for the PassportContext requires 4 arguments:

export declare type PassportContext<
  UserObjectType extends {}, 
  Credentials extends {}, 
  AuthInfoTemplate extends {} = {}, 
  Request extends object = ExpressRequest
> = SharedPassportContext<UserObjectType, Credentials, AuthInfoTemplate, Request>;

In the example provided there are only two arguments provided, Context and ExpressRequest. My definitions look something like this:

export interface Credentials {
  username: string;
  password: string;
}

declare interface MyContext extends PassportContext<UserModelInterface, Credentials, {}, express.Request> {
  dataSources: MyDataSourcesInterface;
}

As of TS 2.2 there you can add the prisma and smtpTransport just as I did with dataSources even though the original context is a type and not an interface. If you want it stricter then perhaps an intersection is a better choice.

1reaction
gforgecommented, Jul 10, 2020

Sure, I’ll try to have a look at this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context and contextValue - Apollo GraphQL Docs
During a GraphQL operation, you can share data throughout your server's resolvers and plugins by creating an object named contextValue .
Read more >
How can I correctly type the context object in Apollo Server?
I'm using apollo server with typescript and I'm having trouble getting the context parameter inside of my resolver to pick up that the...
Read more >
Typescript Context for Apollo Server · Issue #34 - GitHub
Hey, I'm trying to make the switch over to using Passport to verify email/password logins using GraphQLLocalStrategy but I'm running into an ...
Read more >
Build a scalable GraphQL server using TypeScript and Apollo ...
Build a scalable GraphQL server using TypeScript and Apollo Server · ## Create a folder mkdir apollo-server · ## Install required modules ·...
Read more >
Type safe GraphQL server context with GraphQL Code ...
Add type safety to your GraphQL server context with GraphQL Code Generator plugin " TypeScript ... Apollo CLI TypeScript /GraphQL Codegen.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found