GraphQL APIs with Spring Boot · درس

تحديد معدل الطلبات والحماية من عمق الاستعلامات

احمِ واجهة Spring Boot GraphQL API من إساءة الاستخدام وهجمات حجب الخدمة عبر تحديد معدل استدعاء العملاء وعمق تداخل استعلاماتهم.

الدرس 4 من 413 خطوة

تحديد معدل الطلبات والحماية من عمق الاستعلامات درس مجاني في GraphQL APIs with Spring Boot على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في GraphQL APIs with Spring Boot، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why GraphQL Needs Protection

A single GraphQL endpoint accepts arbitrarily complex queries. A malicious or careless client can request deeply nested data or hammer the server, exhausting resources.

Rate limiting and depth protection defend against these attacks.

The Nested Query Threat

Because GraphQL allows cyclic relationships, a client could ask for an author's books, each book's author, that author's books, and so on. This recursion can balloon into an enormous, expensive query.

query {
  author {
    books { author { books { author { name } } } }
  }
}

Limiting Query Depth

graphql-java provides MaxQueryDepthInstrumentation, which rejects any query that nests deeper than a set limit before execution begins.

@Bean
public Instrumentation depthLimit() {
    return new MaxQueryDepthInstrumentation(10);
}

Limiting Field Count

Beyond depth, a broad query can request thousands of fields. MaxQueryComplexityInstrumentation caps the total complexity score of a query.

@Bean
public Instrumentation complexityLimit() {
    return new MaxQueryComplexityInstrumentation(200);
}

What Is Rate Limiting?

Rate limiting caps how many requests a client may make in a time window. It prevents abuse and ensures fair resource sharing across clients.

The Token Bucket Idea

A common algorithm is the token bucket: each client has a bucket that refills at a steady rate. Every request consumes a token; if the bucket is empty, the request is rejected.

Rate Limiting with Bucket4j

The bucket4j library implements token buckets in Java. Configure a bucket with a refill rate and capacity.

Bandwidth limit = Bandwidth.simple(100, Duration.ofMinutes(1));
Bucket bucket = Bucket.builder().addLimit(limit).build();

Enforcing the Limit

Before processing a request, try to consume a token. If none is available, return an error instead of executing the query.

if (!bucket.tryConsume(1)) {
    throw new RateLimitException("Too many requests");
}

Per-Client Buckets

Track a separate bucket per client, keyed by API key or authenticated user ID, so one heavy client cannot starve everyone else.

Bucket bucket = buckets.computeIfAbsent(userId, k -> newBucket());

Combining Defenses

Layer your protections for full coverage:

  • Depth limit stops recursive abuse
  • Complexity limit stops broad expensive queries
  • Rate limit stops request floods
  • Timeouts stop slow runaway operations

Best Practices

Tune limits to your real traffic:

  • Start strict, then relax based on monitoring
  • Return clear errors so clients can back off
  • Apply tighter limits to unauthenticated traffic
  • Log rejected queries to spot abuse patterns

Quick Check

Test your API protection knowledge.

Recap

You hardened your GraphQL API:

  • Depth and complexity instrumentation block expensive queries
  • Rate limiting caps request frequency per client
  • Token buckets (bucket4j) implement fair limits
  • Layer depth, complexity, rate, and timeout defenses

These guards keep your API available and resilient under abuse.

البدء مجانًا

تعلم GraphQL APIs with Spring Boot مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تحديد معدل الطلبات والحماية من عمق الاستعلامات» مجاني؟

نعم — نص درس «تحديد معدل الطلبات والحماية من عمق الاستعلامات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة GraphQL APIs with Spring Boot، انتقل إلى CoddyKit PRO. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.

ماذا ستتعلم في «تحديد معدل الطلبات والحماية من عمق الاستعلامات»؟

احمِ واجهة Spring Boot GraphQL API من إساءة الاستخدام وهجمات حجب الخدمة عبر تحديد معدل استدعاء العملاء وعمق تداخل استعلاماتهم. تتمرن على GraphQL APIs with Spring Boot مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ GraphQL APIs with Spring Boot؟

لا تُشترط خبرة سابقة. GraphQL APIs with Spring Boot على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تحديد معدل الطلبات والحماية من عمق الاستعلامات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس GraphQL APIs with Spring Boot هذا؟

نعم. كل درس في GraphQL APIs with Spring Boot يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. معالجة الأخطاء المخصصة في GraphQL
  2. المصادقة باستخدام Spring Security
  3. التفويض باستخدام Directives وContext
  4. تحديد معدل الطلبات والحماية من عمق الاستعلامات
← العودة إلى GraphQL APIs with Spring Boot