Skip to content

N+1 queries when resolving branch rules

Summary

Preloading approval_project_rules_with_unique_policies in BranchRulesResolver seems to result in N+1 queries:

Make a request for branch rules for a project with multiple protected branches: For example:

query getBranchRulesEE($projectPath: ID!) {
  project(fullPath: $projectPath) {
    id
    branchRules {
      nodes {
        approvalRules {
          nodes {
            id
          }
        }
      }
    }
  }
}

Observe that a query for approval_project_rules_with_unique_policies is performed per protected branch.

Improvements

Refactor that scope so that it can be eagerloaded or use some custom preloading

Optional: Missing test coverage

Consider adding tests around here: https://gitlab.com/gitlab-org/gitlab/-/blob/a9f1e3f17f68429e70ddbefe5a6d79ce8314160f/ee/spec/requests/api/graphql/project/branch_rules_spec.rb#L41