Join our Newsletter — 33% off our NHI Course

Recursive Fragments

Recursive fragments are reusable GraphQL query components that can reference patterns in a way that repeats through nested structures. They are powerful for clean query design, but if an engine does not control depth or recursion, they can consume excessive resources and create denial of service conditions.

How Recursive Fragments Work

Recursive fragments are a GraphQL composition pattern, not a separate security control. They let a query repeat the same logical selection across nested nodes, which makes deeply structured data easier to request and keeps client queries readable.

The key design point is that recursion is bounded by the schema and by server behaviour. If the schema allows self-referential traversal, the query can continue following nested relationships until a stop condition is reached, usually by depth limits, complexity scoring, or resolver safeguards.

Why They Matter in GraphQL Design

Recursive fragments are useful when the same object shape appears at multiple levels, such as categories, organizational trees, comments, or dependency graphs. They reduce duplication in the query document and can help clients express complex data retrieval without writing many separate field blocks.

That convenience comes with a trade-off: the same structure that improves readability can also make it easier to ask for far more data than intended. The risk is not the fragment itself, but the combination of recursion, nested field expansion, and an engine that does not enforce a practical stop condition.

Security and Resource Implications

When recursion is unrestricted, a query can grow rapidly in cost as each nested level multiplies resolver work, response size, and backend load. In practice, this can turn a normal-looking GraphQL request into an expensive operation that degrades performance or exhausts server resources.

That makes recursive fragments relevant to API security and availability, especially where GraphQL endpoints are exposed broadly or sit behind expensive data sources. Defensive controls usually focus on query depth, complexity, rate limits, and resolver efficiency rather than on fragment syntax alone.

Well-designed GraphQL services treat recursive fragments as a legitimate modeling tool, but they assume that recursion must be constrained somewhere in the request path or execution layer.

Common Implementation Considerations

Recursive fragments are most effective when the schema is intentionally structured for hierarchical traversal and the client needs a stable, reusable representation of repeated node types. They are less appropriate when the same effect can be achieved with a narrower query or a fixed-depth selection that is easier to reason about operationally.

Practitioners should think in terms of execution cost, not just query correctness. A recursive fragment can be syntactically valid and still be operationally unsafe if it creates wide fan-out, unbounded nesting, or heavy backend joins.

Risk and Threat Considerations

Recursive fragments can contribute to denial of service when a GraphQL server accepts deep or repetitive traversal without effective limits. The danger is amplified when the same pattern is used against expensive resolvers, object graphs with many branches, or backends that perform work per nested node.

Failure mechanism: An attacker or buggy client sends a query that expands recursively through nested objects, forcing excessive resolver execution, database access, and response construction until latency or resource exhaustion affects the service.

Impact: The API can become slow, unstable, or unavailable, and the cost may spread beyond the GraphQL layer to dependent services, caches, and downstream data stores.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP API Security Top 10 addresses the attack and risk surface, while NIST CSF 2.0 and CIS Controls v8 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP API Security Top 10 API4 — Unrestricted Resource Consumption Recursive GraphQL queries can amplify server work and consume excessive resources.
Recommendation — Cap query depth and complexity to prevent recursive fragments from exhausting API resources.
NIST CSF 2.0 PR.PS-01 — Configuration Management GraphQL execution limits and resolver settings are configuration controls that shape exposure.
Recommendation — Configure depth and complexity limits in the GraphQL service to reduce recursion abuse.
CIS Controls v8 CIS-16 — Application Software Security Secure design and validation of application-layer request handling applies to GraphQL recursion.
Recommendation — Validate GraphQL query patterns and reject unbounded recursive expansion at the application layer.

Practitioner Guidance

What to watch for: Treat recursive fragments as a signal to validate the server’s execution controls, not as a problem to ban outright. The practical question is whether the schema, query planner, and resolver layer together prevent uncontrolled traversal from becoming an availability issue.

Governance implication: Teams that allow GraphQL recursion should own explicit limits for depth, complexity, and field expansion, and they should review those limits whenever the schema grows or new nested relationships are introduced.