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

initialize() retrieve user #108

Open
RemyMachado opened this issue Sep 12, 2022 · 0 comments
Open

initialize() retrieve user #108

RemyMachado opened this issue Sep 12, 2022 · 0 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@RemyMachado
Copy link

RemyMachado commented Sep 12, 2022

I would like to store the authenticated user in the context, in order for each method of the DataSource to directly access it, but I think it may be an antipattern. I'm not even sure async initialize() is allowed.

async initialize(config: DataSourceConfig<GraphQLContext>) {
        super.initialize?.(config)

        const user = await this.findBearerUser()
        this.context.user = user
}

This would allow the following (no need for user fetching):

async findOneById(id) {
        if (!this.context.user) throw new Error('Unauthenticated.')
        // ...
}

Instead of fetching the user for every method:

async findOneById(id) {
        const user = await this.findBearerUser()
        if (!user) throw new Error('Unauthenticated.')
        // ...
}

What's the best way to achieve the desired behavior?
Are we constrained to fetch the user on every method?

@lorensr lorensr added help wanted Extra attention is needed question Further information is requested labels Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants