Skip to content

fix: resolve database query antipatterns in GraphQL resolvers#777

Merged
rohan-chaturvedi merged 4 commits intomainfrom
fix/db-query-antipatterns
Mar 7, 2026
Merged

fix: resolve database query antipatterns in GraphQL resolvers#777
rohan-chaturvedi merged 4 commits intomainfrom
fix/db-query-antipatterns

Conversation

@nimish-ks
Copy link
Member

Summary

  • Cache repeated OrganisationMember lookups in OrganisationType — 5 identical queries per request reduced to 1
  • Eliminate N+1 .exists() loops in AppType.resolve_environments, resolve_app_environments, ServiceAccountType.resolve_app_memberships — replaced with upfront values_list set lookups
  • Batch environment queries in OrganisationMemberType.resolve_app_memberships — one query for all apps instead of one per app
  • Add select_related for organisation and user foreign key access to avoid lazy-load hits
  • Wrap multi-write mutations in transaction.atomic()CreateEnvironment, UpdateMemberEnvScope, CreateServiceAccount
  • Fix race condition in SwapEnvironmentOrder with select_for_update() + transaction.atomic()
  • Bulk delete service account handlers instead of loop-based individual deletes
  • Collapse redundant queries in PhaseLicenseType.exists() + .get() + extra Role lookup → single .first() with join
  • Set-based access filtering in syncing queries instead of per-item user_can_access_app calls

Test plan

  • Verify environment listing works correctly for users with partial environment access
  • Verify service account app memberships resolve correctly
  • Test environment creation, member scope updates, and service account creation still work atomically
  • Test swapping environment order under concurrent requests
  • Verify organisation-level sync listing returns correct results
  • Verify license activation and owner resolution on EE

- Cache repeated OrganisationMember lookups in OrganisationType resolvers (5 queries → 1)
- Replace N+1 .exists() loops with upfront set lookups in AppType, ServiceAccountType, and schema resolvers
- Batch environment queries in resolve_app_memberships instead of per-app queries
- Add select_related for organisation and user foreign key access
- Wrap multi-write mutations in transaction.atomic() (CreateEnvironment, UpdateMemberEnvScope, CreateServiceAccount)
- Add select_for_update + transaction.atomic to SwapEnvironmentOrder to prevent race conditions
- Replace loop-based handler deletion with bulk delete
- Collapse redundant .exists() + .get() + Role lookup into .first() with join
- Replace per-item user_can_access_app filtering with set-based lookup in syncing queries
Signed-off-by: rohan <rohan.chaturvedi@protonmail.com>
Signed-off-by: rohan <rohan.chaturvedi@protonmail.com>
@rohan-chaturvedi rohan-chaturvedi merged commit 859f7d4 into main Mar 7, 2026
7 checks passed
@rohan-chaturvedi rohan-chaturvedi deleted the fix/db-query-antipatterns branch March 7, 2026 21:59
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