Skip to content

Conversation

@astorije
Copy link
Contributor

@astorije astorije commented Aug 4, 2021

I am currently doing exactly this to store the authenticated user info on the Apollo context: https://www.apollographql.com/docs/apollo-server/security/authentication/#putting-authenticated-user-info-on-the-context

Then, in the datasource, one would want to fetch something based on that user info:

  findMyFruits() {
    if (!this.context.user) {
      throw new AuthenticationError("you must be logged in");
    }

    return this.knex
      .select()
      .from<Fruit>("fruits")
      .where({ userId: this.context.user.id });
  }

By correctly typing the context of the datasource, this avoids some awkward typing such as (this.context as any).user as User.

And while I was at it anyway, I figured I'd help with #68 :)

@cvburgess
Copy link
Owner

Thank you! Will merge and ship this 🙏

@cvburgess cvburgess changed the title Add types for this.db and this.context v1.5.0 Add types for this.db and this.context Aug 9, 2021
@cvburgess cvburgess merged commit 4e4ab7a into cvburgess:main Aug 9, 2021
@astorije astorije deleted the astorije/types branch August 10, 2021 04:45
@astorije
Copy link
Contributor Author

Amazing, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants