Interview questions with answers
GraphQL interview questions and answers
Schemas, resolvers, N+1 and DataLoader, caching, federation and security. Open any question to read its model answer; the multiple-choice set marks the right option and explains it.
- Subjective
- 30
- Objective
- 30
Subjective questions 30
1What is GraphQL and how does it differ from REST?Asked almost every time
GraphQL is a query language and runtime for APIs with one endpoint and a typed schema. Clients ask for exactly the fields they need, across related types, in one request, so there is no over- or under-fetching and fewer round trips. REST exposes resources at many URLs with fixed responses and uses HTTP caching naturally. GraphQL moves complexity to the server (resolvers, query cost, caching).
2What is the N+1 problem and how does DataLoader fix it?Asked almost every time
Resolving a list of 50 posts and then each post's author runs 1 query for posts plus 50 for authors. DataLoader collects all the IDs requested in one tick and calls a batch function once (WHERE id IN (...)), then caches results per request, turning N+1 queries into 2.
3Explain queries, mutations and subscriptions.Asked almost every time
Queries read data and can run in parallel. Mutations change data and run in order, returning the changed objects. Subscriptions keep a connection open (usually WebSockets or SSE) and push results when events occur, such as new chat messages.
4How would you design a GraphQL schema for an e-commerce app?Asked almost every time
Model types around the domain (Product, Variant, Cart, Order, Customer) with relationships as fields, use connections for lists, input types and payload types for mutations (addToCart(input): AddToCartPayload with userErrors), nullable fields where data can fail, and avoid exposing database structure directly.
5How do you handle authentication and authorisation in GraphQL?Asked almost every time
Authenticate at the transport layer (token or cookie) and put the user in the context. Authorise in resolvers or a business layer per field or object, with directives or a policy library, so every path to sensitive data is checked, not just top-level queries.
6What are the downsides of GraphQL?Asked almost every time
HTTP caching is harder, complex queries can overload the server, the N+1 problem needs DataLoader, file uploads and errors are less standard, monitoring per operation needs tooling, and there is more setup than a simple REST API.
7What are resolvers?Very often asked
Functions that return the value for each field in the schema: (parent, args, context, info). The root resolvers fetch top-level data; field resolvers compute nested fields from their parent. Context carries per-request things such as the user, DataLoaders and database clients.
8How do you secure a GraphQL API?Very often asked
Authenticate per request and pass the user in context, authorise in resolvers or a business layer (not only at the gateway), limit query depth and complexity, set timeouts and rate limits, disable introspection or use persisted queries in production if appropriate, and validate inputs.
9How is caching done with GraphQL?Very often asked
HTTP caching is harder because requests are POSTs to one URL. Clients such as Apollo and urql keep a normalised cache keyed by type and ID. Servers use DataLoader per request, response caching, cache hints, and persisted queries sent as GET so CDNs can cache them.
10Schema-first vs. code-first?Very often asked
Schema-first writes SDL (type User { id: ID! }) and maps resolvers to it; the schema is a readable contract. Code-first builds the schema from code (Pothos, Nexus, TypeGraphQL, NestJS decorators), giving type safety and less duplication. Both produce the same runtime schema.
11What is GraphQL federation?Very often asked
A way to compose one supergraph from several subgraphs owned by different teams. Each service defines its types and can extend types from others with entity keys; a router (such as Apollo Router) plans and executes queries across services.
12How do you version a GraphQL API?Very often asked
Usually you do not version it: add new fields and types freely, mark old ones @deprecated(reason), monitor usage with schema analytics, and remove them once clients stop using them. Breaking changes are checked in CI against the registry.
13What are input types and why are they separate?Very often asked
Input types (input CreateUserInput) define arguments for mutations and queries. They are separate from output types because inputs cannot contain resolvers or interfaces and often have different required fields.
14What are interfaces and unions?Very often asked
An interface defines common fields that several types implement (interface Node { id: ID! }). A union lists possible types without shared fields (union SearchResult = User | Post). Clients use fragments with ... on Type to select type-specific fields.
15How do client libraries like Apollo Client cache data?Very often asked
They normalise responses by __typename and id into a flat cache, so updating an object in one query updates it everywhere. You may need cache policies for pagination and manual updates after mutations that add or remove items.
16How do you implement subscriptions at scale?Very often asked
Use WebSockets (graphql-ws) or Server-Sent Events, publish events through a shared pub/sub (Redis, Kafka) so any server can deliver them, filter per subscriber, authenticate the connection, and limit the number of subscriptions per client.
17How do you limit query cost?Very often asked
Set maximum depth and complexity scores, paginate all lists with maximum page sizes, use timeouts, rate-limit per client by cost rather than requests, and allow only persisted queries for public clients.
18How do you handle errors in GraphQL?Often asked
Responses can contain both data and an errors array, often with HTTP 200. Use error extensions with codes, return typed error results in unions for expected errors (union CreateUserResult = User | ValidationError), and log unexpected errors without leaking internals.
19How do you paginate in GraphQL?Often asked
Offset pagination (limit, offset) is simple. Cursor-based pagination, often the Relay connection spec (edges, node, cursor, pageInfo), stays stable when data changes and suits infinite scrolling.
20What are fragments and why use them?Often asked
Reusable sets of fields (fragment UserCard on User { name avatar }) included in queries. They keep components' data needs next to the components (colocation) and avoid repeating field lists.
21What is introspection?Often asked
The ability to query the schema itself (__schema, __type), which powers tools like GraphiQL, code generation and editor completion. Some teams disable it publicly to reduce information exposure.
22What are directives?Often asked
Annotations prefixed with @ that change execution or schema behaviour: built-in @include(if:), @skip(if:) and @deprecated, and custom schema directives for auth, formatting or caching hints.
23How do you upload files with GraphQL?Often asked
The common approach is to upload directly to object storage with a pre-signed URL obtained through a mutation, then send the resulting URL or key in another mutation. Multipart GraphQL uploads exist but complicate servers and CDNs.
24What is the difference between Apollo Server, GraphQL Yoga and Mercurius?Often asked
All are Node GraphQL servers: Apollo Server integrates with Apollo's platform; GraphQL Yoga is lightweight and standards-focused (Envelop plugins); Mercurius is built for Fastify with high performance. Choice depends on ecosystem and performance needs.
25How do you test GraphQL resolvers?Often asked
Unit-test resolvers as functions with fake context and data sources, and integration-test the schema by executing operations against the server (executeOperation) with a test database or mocks.
26What is schema stitching vs. federation?Often asked
Stitching merges schemas at a gateway that knows how to combine them. Federation lets each subgraph declare its entities and keys with directives, and a router composes them, which scales better across teams.
27How does code generation help GraphQL clients?Often asked
Tools like GraphQL Code Generator create TypeScript types and typed hooks from the schema and your operations, so queries are type-checked and responses are typed without hand-written interfaces.
28What does ! mean in a type?Sometimes asked
Non-null: String! must always have a value. Lists combine: [String!]! is a non-null list of non-null strings. A null in a non-null field propagates the null up to the nearest nullable parent.
29What are scalars and enums?Sometimes asked
Built-in scalars are Int, Float, String, Boolean and ID; custom scalars (DateTime, Email) add validation and serialisation. Enums restrict a field to a fixed set of values.
30How do you monitor a GraphQL API?Sometimes asked
Track metrics per operation name (latency, errors, request rate), trace resolver timings, log slow and failing operations, and use schema usage analytics to know which fields clients rely on.
Objective questions 30
1How many endpoints does a typical GraphQL API expose?Asked almost every time
Why: Usually a single /graphql endpoint.
2Which tool batches and caches lookups to fix N+1?Asked almost every time
Why: DataLoader batches IDs per tick.
3Which operation changes data?Asked almost every time
Why: Mutations run sequentially.
4Where should authorisation checks happen?Asked almost every time
Why: Every path to data must be checked.
5How are fields retired without versioning?Asked almost every time
Why: Monitor usage first.
6Which is a known downside of GraphQL?Asked almost every time
Why: Single POST endpoint.
7Which operation pushes live updates?Very often asked
Why: Subscriptions keep a connection open.
8What does String! mean?Very often asked
Why: ! marks non-null.
9Which resolver argument holds per-request data like the user?Very often asked
Why: Context is built for each request.
10Which defends against very expensive nested queries?Very often asked
Why: Limit how deep and costly queries can be.
11Which keyword defines a mutation's argument shape?Very often asked
Why: Input types.
12A union differs from an interface because it…Very often asked
Why: Use inline fragments.
13How does Apollo Client normalise objects?Very often asked
Why: One copy per object.
14Which protocol library serves subscriptions over WebSockets?Very often asked
Why: The modern standard library.
15Which limits expensive queries?Very often asked
Why: Score queries before running.
16Which pagination style uses edges, node and pageInfo?Often asked
Why: Relay's connection spec is cursor-based.
17What composes many subgraphs into one API?Often asked
Why: A router executes queries across subgraphs.
18Which built-in scalar is meant for unique identifiers?Often asked
Why: ID is serialised as a string.
19GraphQL responses with partial failures contain…Often asked
Why: Partial data can return with errors.
20Which directive conditionally excludes a field?Often asked
Why: @include is the opposite.
21Recommended way to upload large files?Often asked
Why: Keeps servers light.
22Which GraphQL server is built for Fastify?Often asked
Why: High performance.
23Which tool generates TypeScript types from operations?Often asked
Why: Typed hooks too.
24Federation composes subgraphs using…Often asked
Why: Each team owns a subgraph.
25What should metrics be grouped by?Often asked
Why: One endpoint hides details otherwise.
26Fragments are used to…Sometimes asked
Why: They also enable colocation in components.
27Which query lets tools read the schema?Sometimes asked
Why: Introspection powers IDE tooling.
28Persisted queries help with…Sometimes asked
Why: Clients send an ID or hash instead of the full query.
29What does ... on User select?Sometimes asked
Why: An inline fragment.
30Which interface is common for refetching any object by ID?Sometimes asked
Why: Used by Relay.
Track what you have practised and search every bank in your dashboard.
Start learning