Api Design hard
A malicious client sends a GraphQL query with nested fragments causing exponential resolution (query bomb). How do you prevent this?
- A Enforce static analysis to reject queries exceeding a maximum depth threshold and implement a complexity scoring system that assigns a scalar cost to every schema field.
- B Adopt a trust-based policy that relies on client-side linting and developer documentation to ensure that only optimized and shallow queries are sent to the production cluster.
- C Implement a basic request-count rate limiter that treats all GraphQL operations as having an identical execution cost regardless of the depth or breadth of the requested fragment.
- D Disable the use of nested fragments and recursive object references entirely to ensure that every possible GraphQL query is strictly linear in its resolution path.
Answer: A. Enforce static analysis to reject queries exceeding a maximum depth threshold and implement a complexity scoring system that assigns a scalar cost to every schema field.
GraphQL's graph nature allows for recursive or deeply nested queries (e.g., user -> posts -> comments -> author -> posts...), which can exhaust server resources. To mitigate this, implement **static analysis** to reject queries exceeding a max depth (e.g., 5) or **complexity cost analysis**, where fields are assigned values and the total query cost must stay below a limit.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?