Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

v1.5.0 Add types for this.db and this.context #69

Merged
merged 4 commits into from
Aug 9, 2021

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 :)

Signed-off-by: Jérémie Astori <jeremie@astori.fr>
Signed-off-by: Jérémie Astori <jeremie@astori.fr>
Signed-off-by: Jérémie Astori <jeremie@astori.fr>
@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!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants