fix: resolve database query antipatterns in GraphQL resolvers#777
Merged
rohan-chaturvedi merged 4 commits intomainfrom Mar 7, 2026
Merged
fix: resolve database query antipatterns in GraphQL resolvers#777rohan-chaturvedi merged 4 commits intomainfrom
rohan-chaturvedi merged 4 commits intomainfrom
Conversation
- 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
approved these changes
Mar 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OrganisationType— 5 identical queries per request reduced to 1.exists()loops inAppType.resolve_environments,resolve_app_environments,ServiceAccountType.resolve_app_memberships— replaced with upfrontvalues_listset lookupsOrganisationMemberType.resolve_app_memberships— one query for all apps instead of one per appselect_relatedfororganisationanduserforeign key access to avoid lazy-load hitstransaction.atomic()—CreateEnvironment,UpdateMemberEnvScope,CreateServiceAccountSwapEnvironmentOrderwithselect_for_update()+transaction.atomic()PhaseLicenseType—.exists()+.get()+ extra Role lookup → single.first()with joinuser_can_access_appcallsTest plan