Skip to content
Launch GitLab Knowledge Graph

Add GraphQL API for batch user queries to reduce N+1 problems

Feature Request: GraphQL API

Problem

Our REST API has N+1 query problems when clients need related data:

// ❌ Current: N+1 queries (1 + 10 users = 11 queries)


const recommendations = await fetch('/api/recommendations?limit=10');
for (const rec of recommendations) {
  const user = await fetch(`/api/users/${rec.userId}`);  // N queries
}

Solution

Implement GraphQL API with DataLoader pattern:

query GetRecommendationsWithUsers {
  recommendations(limit: 10) {
    id
    score
    user {
      id
      name
      avatar
    }
  }
}

Benefits:

  • Single query instead of N+1
  • DataLoader batching (10 user queries → 1 batch query)
  • p99 latency: Expected 412ms → 180ms

Implementation Plan

  1. Add graphql-python dependency
  2. Create schema with User and Recommendation types
  3. Implement DataLoader for user batching
  4. Add GraphQL endpoint /graphql
  5. Migrate iOS app to use GraphQL

Related

Edited by Administrator