Top 50 GraphQL Interview Questions & Answers
Practise 50 GraphQL interview questions on schemas, resolvers, DataLoader and N+1, cursor pagination, caching, security, subscriptions and federation.
GraphQL Basics
Q1. What is GraphQL?
GraphQL is a query language and runtime for APIs in which clients ask for exactly the fields they need. It is typed by a schema and independent of any particular database or language.
Q2. How does GraphQL differ from REST?
REST typically exposes many resource URLs with fixed responses. GraphQL usually has one endpoint where clients choose their fields, which reduces over-fetching but adds server and caching complexity.
Q3. What are over-fetching and under-fetching?
Over-fetching means getting more data than you need. Under-fetching means one call is not enough, so several are required. GraphQL addresses both through client-specified selections.
Q4. What are the three operation types?
query reads data, mutation changes data and subscription receives updates over time.
Q5. What is a GraphQL schema?
A schema is the contract that defines available types, fields and operations, commonly written in Schema Definition Language (SDL). Every request is validated against it.
Q6. What are the built-in scalar types?
Int, Float, String, Boolean and ID. ID is serialized as a string and is meant for unique identifiers. You can add custom scalars such as DateTime.
Q7. What do ! and [] mean in a type?
! means non-null and [] means a list. [String!]! is therefore a required list that cannot contain null items, though it may be empty.
Q8. What is an input type?
An input type groups the arguments passed to a field, such as a mutation's payload. Input types can only contain scalars, enums and other input types, not output object types.
Q9. What is the difference between an interface and a union?
An interface requires implementing types to share specific fields. A union lists possible types with no shared field requirement, so you select fields using inline fragments.
Q10. When is GraphQL not a good choice?
It can be excessive for simple APIs, public endpoints that depend on HTTP caching, or plain file transfers. REST may be simpler when clients have uniform data needs.
Writing Queries
Q11. What are variables and why use them?
Variables pass dynamic values separately from the query text, as in query GetUser($id: ID!). This keeps queries static, reusable and easier to cache and validate.
Q12. What are aliases?
Aliases rename fields in the response, which lets you request the same field twice with different arguments, such as home: user(id: 1) and work: user(id: 2).
Q13. What are fragments?
Fragments are reusable sets of fields, written as fragment UserInfo on User { id name }. Inline fragments such as ... on Admin select fields for a specific type.
Q14. What are directives?
Directives change how a query or schema is handled. @include(if:) and @skip(if:) control field selection at query time, and @deprecated marks a schema field as outdated.
Q15. What is __typename?
A meta field that returns the concrete type name of an object. Clients use it with unions and interfaces, and caches such as Apollo Client use it to identify objects.
Q16. What is introspection?
A feature that lets clients query the schema itself through __schema and __type. Tools use it for docs and code generation, and many teams limit it on public production APIs.
Q17. Why might a request return HTTP 200 and still have errors?
GraphQL over HTTP often reports field errors in the errors array, while data may contain partial results. Clients should check for errors rather than rely on the status code alone.
Q18. What does a partial response mean?
If a nullable field fails, it becomes null, an entry is added to errors, and other fields still return. If a non-null field fails, the null moves up to the nearest nullable parent.
Q19. How are mutations and queries executed differently?
Top-level query fields may run in parallel. Top-level mutation fields run one after another in order, which keeps sequential changes predictable.
Q20. Should a mutation return data?
Yes. It is good to return the changed object, and often a payload type with any user-facing errors, so a client can update its cache without another request.
Resolvers & Performance
Q21. What is a resolver?
A resolver is a function that returns the data for a field. It can read from a database, another service or memory, and it runs only for fields in the request.
Q22. What arguments does a resolver receive?
Usually four: parent (the previous result), args (field arguments), context (per-request data such as the user or loaders) and info (execution details).
Q23. What is the N+1 problem in GraphQL?
One query fetches a list, and a nested resolver runs a separate query for each item. Field-level resolvers make this common, so it needs deliberate batching.
Q24. What is DataLoader?
A library that collects keys requested in the same tick, loads them in one batch and caches results per request. Create loaders per request so that one user's cached data cannot reach another user.
Q25. What is the difference between schema-first and code-first?
Schema-first writes SDL and then adds resolvers. Code-first generates the schema from code, using libraries such as Nexus or Strawberry, which keeps types close to the implementation.
Q26. How does caching work in GraphQL?
POST to one URL does not fit normal HTTP caching. Common approaches are client normalized caches, server response or field caches, and persisted queries sent over GET for CDN caching.
Q27. What are persisted queries?
The client sends a hash rather than the full query, and the server finds the stored query. This cuts request size and helps CDN caching. A strict allowlist also blocks unknown queries.
Q28. How does Apollo Client cache data?
It normalizes responses into objects keyed by __typename and id, so updating one object updates every query that uses it. Fetch policies control when the network is used.
Q29. How do you monitor a GraphQL API?
URL-level metrics are not enough because everything uses one endpoint. Track operation names, resolver timing, error rates and field usage, and require clients to name their operations.
Q30. How do you handle file uploads?
The GraphQL spec does not define uploads. A common approach is to have a mutation return a signed URL for direct upload to object storage, which avoids streaming files through the API.
Pagination & Schema Design
Q31. What is the difference between offset and cursor pagination?
Offset uses limit and offset, which is simple but slow and unstable on large, changing data. Cursor pagination continues from a marker, which is stable and efficient.
Q32. What is the Relay connection model?
A cursor pagination standard with edges holding a node and cursor, plus pageInfo with hasNextPage and endCursor. Arguments such as first and after control the page.
Q33. How do you version a GraphQL API?
Usually you evolve one schema rather than create /v2. Add new fields, mark old ones with @deprecated, track their usage, then remove them once clients have moved.
Q34. What counts as a breaking schema change?
Removing a field, changing its type, making an output field nullable or adding a required argument. Adding optional fields is safe. Schema checks in CI can catch breaking changes.
Q35. How should you decide what is nullable?
Make fields nullable if they can fail independently or may be missing, because a non-null failure spreads upward. Use non-null where the value is truly guaranteed.
Q36. How should you model errors?
Use the errors array for unexpected or system failures. Expected business errors, such as a taken username, can be modelled in the schema using a union or payload field, which clients can handle safely.
Q37. How should you name mutations?
Use clear, specific verbs such as createUser and cancelOrder, a single input argument and a dedicated payload type. This makes it easy to add fields later without breaking clients.
Q38. What is the Relay node interface?
A pattern where objects have a globally unique id and can be refetched through a node(id:) query. It supports client cache updates and consistent refetching.
Q39. Why should the schema not mirror the database?
Exposing tables directly leaks internal structure and makes changes harder. Design around what clients need, so storage can change without changing the API.
Q40. What is a custom scalar?
A type such as DateTime or Email with your own parsing and output logic. It validates data once at the API edge and makes the schema easier to understand.
Security, Subscriptions & Federation
Q41. How do you handle authentication?
Authenticate the HTTP request, typically by a token or session, before execution, then place the user in context for resolvers to read.
Q42. Where should authorization logic live?
Keep it in the business logic layer or use per-field rules, rather than only at the top-level query. Nested paths can otherwise reach restricted data by another route.
Q43. How do you limit expensive queries?
Use depth limits, query cost analysis, timeouts, and required pagination limits. A client can otherwise write a deeply nested query that overloads the server.
Q44. What is a batching or alias attack?
A single request can contain many operations or aliased fields, which bypasses simple per-request rate limits, for example on a login mutation. Limit by query cost and restrict aliases on sensitive fields.
Q45. Why does rate limiting differ from REST?
One request can be very cheap or very expensive, so counting requests is not enough. Budgeting by calculated query cost, as some large public APIs do, is fairer.
Q46. How do subscriptions work?
A client subscribes to an event, and the server pushes results over a long-lived connection, usually WebSockets (graphql-ws) or Server-Sent Events.
Q47. How do you scale subscriptions?
Connections are stateful, so use a shared pub/sub system such as Redis or Kafka to deliver events across servers. Plan for reconnections and re-check authorization on long-lived connections.
Q48. What is the difference between schema stitching and federation?
Stitching combines schemas at a gateway using gateway-level logic. Federation lets each service own its part of a shared graph, with the composition defined by directives such as @key.
Q49. What does Apollo Federation do?
It builds a supergraph from separate subgraphs owned by different teams. The router plans each query, calls the needed subgraphs and merges the results.
Q50. How do you test a GraphQL API?
Unit test resolvers and business logic, run integration tests against real operations, and check schema changes in CI. Add tests for authorization on nested fields and limits on costly queries.