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

Replace guards with single interceptor #4942

Open
2 tasks
hero101 opened this issue Feb 14, 2025 · 0 comments
Open
2 tasks

Replace guards with single interceptor #4942

hero101 opened this issue Feb 14, 2025 · 0 comments
Labels
Atlas Team Issues and Epics worked on by team Atlas server user story

Comments

@hero101
Copy link
Collaborator

hero101 commented Feb 14, 2025

The scene

Guards in NestJS are used as a guarding mechanism on API routes. For GraphQL a global guard will work on all resolvers or when used with the UseGuard decorator on a specific field resolver or on a specific resolver for all field resolvers.

The problem

When listing multiple entities and their nested entities, e.g.

{
  users {
    profile {
      id
    }
  }
}

the guard will be executed before each execution of the field resolver (quick maths: 300 users = 300 guard executions).

Furthermore, the guard in most cases is not used to protect resolvers but rather to switch context or attach the user to the context (300 times in the previous example) which a simple interceptor should be able to do ONCE.

The problem deepens when dataloaders are involved.
The fundamental nature of the dataloader is to coalesce all individual loads that occur within a single frame of execution (a single tick of the event loop). When a guard is introduced in the equation its execution is not able to fit in the same tick, so for each resolver + guard an addition tick is introduced, forcing the dataloader to batch a single entry per tick which makes it useless.

Acceptance criteria

  • The UseGuard decorator is removed for resolvers without the AuthorizationAgentPrivilege decorator
  • A global interceptor is introduced, doing the job of the previously used GraphqlGuard

Additional Context

Having UseGuard without the AuthorizationAgentPrivilege decorator is useless since no privilege is attached that the guard requires to execute any actual guarding (see GraphqlGuard.handleRequest for reference)

@hero101 hero101 added Atlas Team Issues and Epics worked on by team Atlas server user story labels Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Atlas Team Issues and Epics worked on by team Atlas server user story
Projects
None yet
Development

No branches or pull requests

1 participant