Prepare for Your GraphQL Interview: Basic to Advanced Questions
This comprehensive guide features 30 GraphQL interview questions with detailed answers, organized by difficulty level. Ideal for freshers, candidates with 1-3 years of experience, and professionals with 3-6 years in GraphQL development. Master concepts, practical implementations, and real-world scenarios.
Basic GraphQL Interview Questions (1-10)
1. What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries with your existing data. It allows clients to request exactly the data they need, solving common issues like over-fetching or under-fetching data.[1][2]
2. What are the core components of a GraphQL architecture?
The three main components are: Schema (defines data types and capabilities), Resolvers (functions that fetch data for fields), and Queries/Mutations/Subscriptions (operations clients perform).[4]
3. What is a GraphQL schema?
A GraphQL schema is a blueprint defining the types of data, relationships between them, and available operations using Schema Definition Language (SDL). It specifies what clients can query or mutate.[4][5]
4. What are the scalar types in GraphQL?
GraphQL has six built-in scalars: ID, String, Int, Float, Boolean, and DateTime (custom). These represent leaf values in queries.[1]
5. What is the difference between Object types and Input types?
Object types define the structure of query results, while Input types are used for arguments in queries and mutations. Input types cannot contain non-nullable fields with default values.[1]
type User {
id: ID!
name: String!
}
input CreateUserInput {
name: String!
}
6. What are GraphQL queries?
Queries read data from the server without modifying it. They are declarative, allowing clients to specify exact fields needed. Queries map to HTTP GET and are cacheable.[1][2]
query {
user(id: "123") {
name
email
}
}
7. What are GraphQL mutations?
Mutations modify data on the server (create, update, delete). They follow the same structure as queries but can have side effects. Always specify the fields you want returned.[2]
mutation {
createPost(input: {title: "New Post"}) {
id
title
}
}
8. What are GraphQL subscriptions?
Subscriptions enable real-time data updates using WebSockets. Clients subscribe to events, and the server pushes updates when data changes matching the subscription criteria.[5]
9. What is introspection in GraphQL?
Introspection allows clients to query the schema itself to discover types, fields, and operations available. It enables powerful tooling like automatic documentation and type-checking.[5]
10. How do you define a simple GraphQL schema?
Use SDL to define types, queries, and mutations. Here’s a basic example for a blog API:
type Query {
posts: [Post!]!
}
type Post {
id: ID!
title: String!
}
Intermediate GraphQL Interview Questions (11-20)
11. What are resolver functions in GraphQL?
Resolvers are functions attached to each field in the schema that return the data for that field. They map fields to data sources and can implement custom logic like authentication.[1][2]
12. Explain the GraphQL query execution process.
1. Parse the query syntax. 2. Validate against the schema. 3. Execute resolvers for each field in parallel. 4. Merge results and return JSON response.[1]
13. How do you implement pagination in GraphQL?
Use cursor-based pagination with first, after, last, before arguments. It’s more reliable than offset-based for large datasets at companies like Atlassian.[3]
type Query {
posts(first: Int, after: String): PostConnection!
}
14. What are GraphQL directives?
Directives provide conditional logic in queries like @include(if: Boolean) and @skip(if: Boolean). Custom directives can add metadata or modify execution.[1]
15. How do you handle errors in GraphQL?
GraphQL collects all errors during execution and returns them in the errors array alongside partial data. Use error codes, paths, and messages for debugging.[1]
16. What is the N+1 problem in GraphQL?
When resolving a list field, each child field’s resolver makes separate database calls, creating N+1 queries. Use DataLoader to batch and cache requests.[3]
17. How do you write a nested GraphQL query?
Nest fields to fetch related data in one request, eliminating multiple round trips:
query {
user(id: "1") {
name
posts {
title
comments {
text
}
}
}
}
18. What are fragments in GraphQL?
Fragments reuse sets of fields across queries. They improve readability and reduce duplication:
fragment PostFields on Post {
id
title
}
19. How do you implement authentication in GraphQL resolvers?
Check JWT tokens or context in parent resolvers, then pass user info through resolver chains. Reject unauthorized requests early in the chain.[2]
20. What is query batching in GraphQL? (Scenario: Zoho Corp)
At Zoho, multiple client queries get batched into single requests using tools like DataLoader, reducing database round trips and improving performance.[5]
Advanced GraphQL Interview Questions (21-30)
21. How do you create a custom scalar in GraphQL?
Define serialization and parsing logic. Example for Date scalar:
scalar Date
// Resolver:
const dateScalar = new GraphQLScalarType({
name: 'Date',
parseValue(value) { return new Date(value); }
});
22. Explain schema stitching.
Schema stitching combines multiple GraphQL schemas into one. Useful when services are split across teams. Customize field resolution between schemas using GraphQL Tools.[3]
23. What is the difference between schema stitching and federation?
Stitching manually merges schemas at one gateway. Federation distributes schema ownership across services, each owning parts of types. Federation scales better for microservices.[3]
24. How do you implement role-based access control (RBAC)? (Scenario: Salesforce)
Salesforce uses context with user roles in resolvers. Check permissions per field:
if (!context.user.canRead('posts')) throw new Error('Unauthorized');
[3]
25. How do you optimize GraphQL query performance?
1. Use DataLoader for N+1. 2. Set query depth/complexity limits. 3. Implement persisted queries. 4. Add caching layers.[2][3]
26. What are persisted queries and why use them?
Pre-define and hash queries clients can only execute by hash ID. Reduces bandwidth, improves security, and enables query analytics.[1]
27. How do you set query complexity limits?
Assign complexity scores to fields and reject queries exceeding limits:
const complexity = fieldName => {
if (fieldName === 'posts') return 10;
return 1;
};
[3]
28. How do you structure a large-scale GraphQL schema? (Scenario: Adobe)
Adobe uses domain-based schema organization: separate modules for users, orders, products. Use interfaces/unions for shared fields and namespace types.[3]
29. How do you debug GraphQL in production?
Enable query logging, use Apollo Server tracing, monitor resolver timings, and implement structured error logging with request IDs.[3]
30. Explain how to implement real-time notifications using subscriptions (Scenario: Swiggy)
Swiggy uses PubSub pattern: client subscribes to order updates, mutations publish to channel matching subscription filters. Scale with Redis PubSub backend.
subscription {
orderUpdates(customerId: "123") {
status
}
}