GraphQL
Junior Questions
What are Enums in GraphQL?
Enums are a special kind of scalar that is restricted to a particular set of allowed values. This allows for validation and type safety.
What problems does this solve?
- Enums improve API clarity and prevent invalid string-based values.
What are Fragments?
Fragments are reusable units that let you group sets of fields. This avoids duplication in large queries and helps keep component-based code modular.
What problems does this solve?
- Fragments are a powerful tool for query organization and sharing data requirements across components.
What are GraphQL Mutations?
Mutations are used to modify data on the server (Create, Update, Delete). Like queries, they return the resulting object, allowing the client to update its UI immediately.
What problems does this solve?
- Mutations handle the "Write" part of APIs; understanding how they return data is key for frontend state sync.
What are GraphQL Queries?
Queries are used to fetch data from the server. They are equivalent to GET requests in REST but allow fetching multiple related resources in a single roundup.
What problems does this solve?
- Queries are the most common operation; knowing their syntax and execution is essential.
What are Input Types?
Input types allow passing complex objects as arguments to queries and mutations. They are separate from regular object types to ensure they only contain inputtable fields.
What problems does this solve?
- Input types are essential for mutations that require structured data.
What are Scalars and Objects?
Scalars are the "leaves" of the query tree (Int, Float, String, Boolean, ID). Objects define a set of fields that can be queried recursively.
What problems does this solve?
- Differentiating between primitive values and complex objects is basic schema design knowledge.
What are Variables and Directives?
Variables let you pass parameter values into queries. Directives like `@include` or `@skip` let you dynamically change query structure based on variables.
What problems does this solve?
- Dynamic queries are essential for real-world applications; variables are safer and faster than string interpolation.
What is GraphQL?
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
- Declarative: Clients specify exactly what data they want.
- Hierarchical: Queries mirror the shape of the data returned.
- Strongly Typed: The schema defines exactly what is possible.
What problems does this solve?
- GraphQL's main value prop is solving over-fetching and under-fetching, which is a common interview topic.
What is the Schema Definition Language (SDL)?
The SDL is a human-readable syntax used to define the types and fields in a GraphQL schema. It serves as a contract between the client and the server.
What problems does this solve?
- Understanding how to read and write SDL is fundamental to working with any GraphQL API.
Mid-Level Questions
How are Client Fragments used?
Fragments allow components to define exactly what data they need, then the parent query can combine these fragments to fetch all data in a single request.
What problems does this solve?
- Colocating data requirements with components is a primary best practice for scalable frontend architecture.
How are Non-null and Lists handled?
Use `!` to mark a field or argument as non-nullable. Wrap a type in `[]` to indicate a list. They can be combined: `[String!]!` means a non-null list of non-null strings.
What problems does this solve?
- Correct nullability and list definitions are key to API reliability and client-side safety.
How does Resolver Logic work?
Each field in a GraphQL query is backed by a resolver function. When a field is queried, the server executes its resolver to fetch the actual value.
What problems does this solve?
- Resolvers are the brain of a GraphQL server; understanding their lifecycle is fundamental.
How is Error Handling handled in GraphQL?
GraphQL returns an `errors` array alongside the `data` object. A request can be partially successful, returning some data even if some resolvers failed.
What problems does this solve?
- Understanding partial failures and the structure of error responses is critical for building robust clients.
Interfaces vs Unions?
Interfaces define a set of fields that multiple types must implement. Unions allow a field to return one of several different types but don't require shared fields.
What problems does this solve?
- Knowing when to use polymorphic types is crucial for modeling complex data relationships.
What are GraphQL Subscriptions?
Subscriptions allow the server to push real-time data to clients when specific events occur. They typically use WebSockets for persistent communication.
What problems does this solve?
- Subscriptions are the third major operation type, essential for real-time features.
What is Apollo Client?
Apollo Client is a comprehensive state management library for JavaScript that handles fetching, caching, and modifying application data with GraphQL.
What problems does this solve?
- Apollo Client is the most popular library for GraphQL in the frontend ecosystem.
What is Cursor Pagination?
The Relay specification for "Connections" defines a cursor-based pagination pattern (`first`, `after`, `edges`, `node`, `pageInfo`) that is widely adopted for GraphQL APIs.
What problems does this solve?
- Cursor pagination is the standard way to handle large datasets in GraphQL elegantly.
What is GraphQL Introspection?
Introspection allows a client to query the API for its own schema, enabling tools like GraphiQL or GraphQL Playground to provide documentation and autocomplete.
What problems does this solve?
- Introspection is what makes GraphQL's developer experience so high; knowing when to disable it in production is important.
What is the Resolver Context?
The context is an object shared across all resolvers in a single execution. It's used for authentication data, database connections, and loaders.
What problems does this solve?
- Context is the standard way to inject shared values into resolvers without prop drilling.
What is the Resolver N+1 Problem?
The N+1 problem occurs when a query fetches a list of items, and then for each item, a separate database call is made to fetch its nested fields.
What problems does this solve?
- Solving N+1 is the most common performance challenge in GraphQL development.
Senior Questions
How does DataLoader work?
DataLoader is a utility library from Facebook that batches and caches requests to your backend (e.g., database or microservices) to solve the N+1 problem.
What problems does this solve?
- DataLoader is the standard industry tool for handling batched data fetching professionally.
How does Normalized Cache work?
Normalization breaks down complex query results into individual objects with unique IDs, allowing the client to update one object and have the change reflect across all queries using it.
What problems does this solve?
- Normalized caching is the core reason GraphQL clients are so powerful; understanding it prevents data staleness.
How is Authentication handled?
Authentication is usually handled via standard HTTP headers (e.g., Bearer tokens). The token is decoded and user identity is added to the resolver context.
What problems does this solve?
- Knowing that GraphQL uses standard HTTP for auth helps clarify that it's not a complete replacement for existing security layers.
How is Authorization handled?
Authorization (permissions) can be implemented in resolvers, using directives, or within your business logic layer (recommended to avoid leaks).
What problems does this solve?
- Correctly assigning permissions across deeply nested queries is critical for data security.
Production Best Practices?
Production checklist: disable introspection, use persisted queries, implement rate limiting/depth analysis, monitor resolver performance, and cache aggressively where possible.
What problems does this solve?
- Summarizing the transition from development to a secure, stable production environment is a standard closing question.
What are Persisted Queries?
Persisted queries store the query string on the server and use a hash instead, reducing bandwidth and preventing arbitrary large/unauthorized queries from being run.
What problems does this solve?
- Persisted queries are a powerful performance and security optimization for production APIs.
What is Apollo Federation?
Federation is an architecture for building a supergraph where multiple "subgraphs" contribute to a single schema, using specialized directives for cross-service fields.
What problems does this solve?
- Federation is the current industry standard for scalable microservice GraphQL implementations.
What is Depth Limiting?
Depth limiting is a security measure that rejects queries nested too deeply, preventing malicious users from overloading the server with complex recursive queries.
What problems does this solve?
- Protecting against malicious queries is vital for any public-facing GraphQL API.
What is GraphQL Codegen?
Codegen tools scan your schema and queries to automatically generate TypeScript types or hooks, ensuring perfect alignment between your frontend and backend.
What problems does this solve?
- Automating type safety is a hallmark of professional TypeScript + GraphQL development.
What is Schema Stitching?
Schema stitching is a process that combines multiple underlying GraphQL APIs into a single, unified gateway API.
What problems does this solve?
- Understanding how to unify microservices into a single graph is a senior-level data architecture skill.