📖 Scenario: You are building a simple GraphQL API for a bookstore. The data is stored in a JavaScript object. You want to use GraphQL's default resolvers to fetch book information without writing custom resolver functions.
🎯 Goal: Create a GraphQL schema with types and queries that use default resolvers to return book data from a JavaScript object.
📋 What You'll Learn
Create a Book type with fields id, title, and author.
Create a Query type with a field books that returns a list of Book.
Create a JavaScript object bookData with exactly three books, each having id, title, and author.
Set up the GraphQL server schema using the default resolvers without writing any custom resolver functions.
Add a query to fetch all books using the default resolver.
💡 Why This Matters
🌍 Real World
GraphQL default resolvers let you quickly build APIs when your data structure matches your schema, saving time and effort.
💼 Career
Understanding default resolvers is essential for backend developers working with GraphQL to efficiently expose data without unnecessary code.
Progress0 / 4 steps
1
Create the book data object
Create a JavaScript constant called bookData that is an array of three objects. Each object must have the exact keys and values: { id: '1', title: '1984', author: 'George Orwell' }, { id: '2', title: 'Brave New World', author: 'Aldous Huxley' }, and { id: '3', title: 'Fahrenheit 451', author: 'Ray Bradbury' }.
GraphQL
Hint
Use const bookData = [ ... ] with three objects inside the array.
2
Define the GraphQL schema types
Create a GraphQL schema string called typeDefs that defines a Book type with fields id (ID!), title (String!), and author (String!). Also define a Query type with a field books that returns a list of Book (use square brackets).
GraphQL
Hint
Use backticks to create a multi-line string for typeDefs. Define the Book and Query types exactly as described.
3
Set up the resolver map using default resolvers
Create a JavaScript object called resolvers with a Query field. Inside Query, create a field books that returns the bookData array. Do not write any custom resolver functions for the Book fields.
GraphQL
Hint
Define resolvers as an object with a Query property. Inside Query, add a books field that returns bookData.
4
Complete the GraphQL server setup
Create an Apollo Server instance called server using typeDefs and resolvers. Export the server as the default export.
GraphQL
Hint
Import ApolloServer, create a new server with typeDefs and resolvers, then export it as default.
Practice
(1/5)
1. What is the main purpose of default resolvers in GraphQL?
easy
A. To write custom logic for every field in the schema
B. To automatically return data by matching schema fields to data keys
C. To validate user input before querying the database
D. To handle errors during query execution
Solution
Step 1: Understand default resolver behavior
Default resolvers automatically map schema fields to matching data keys without extra code.
Step 2: Compare with other options
Options B, C, and D describe custom logic, validation, and error handling, which are not default resolver roles.
Final Answer:
To automatically return data by matching schema fields to data keys -> Option B
Hint: Default resolvers match schema fields to data keys automatically [OK]
Common Mistakes:
Thinking default resolvers handle validation
Confusing default resolvers with custom resolvers
Assuming default resolvers manage errors
2. Which of the following is the correct way to define a GraphQL schema field that uses a default resolver?
easy
A. type Query { user: User() }
B. type Query { user: User @customResolver }
C. type Query { user: User }
D. type Query { user: User -> resolver }
Solution
Step 1: Identify correct schema syntax
GraphQL schema fields are defined as fieldName: Type without extra syntax for default resolvers.
Step 2: Analyze options
type Query { user: User } uses correct syntax. Options B, C, and D include invalid or non-standard syntax.
Final Answer:
type Query { user: User } -> Option C
Quick Check:
Default resolver needs simple field: Type syntax [OK]
Hint: Default resolvers need simple field: Type syntax in schema [OK]
Common Mistakes:
Adding parentheses after type name
Using custom directives without definition
Using arrow or other symbols in schema
3. Given the schema type Query { book: Book } and data { book: { title: "1984", author: "Orwell" } }, what will the default resolver return for the field book?
medium
A. { title: "1984", author: "Orwell" }
B. "1984"
C. null
D. Error: Resolver not found
Solution
Step 1: Understand default resolver behavior
The default resolver returns the data object matching the field name if it exists.
Step 2: Match field to data
The field book matches the data key book which contains the object with title and author.
Final Answer:
{ title: "1984", author: "Orwell" } -> Option A
Quick Check:
Field book returns matching data object [OK]
Hint: Default resolver returns matching data object for field name [OK]
Common Mistakes:
Returning only one property instead of whole object
Expecting null if no custom resolver is defined
Assuming error if resolver is not explicitly written
4. You wrote a GraphQL schema with type Query { user: User } but your query returns null for user. What is a likely cause related to default resolvers?
medium
A. The data source does not have a matching key named 'user'
B. You forgot to write a custom resolver for 'user'
C. The schema syntax is invalid
D. GraphQL does not support default resolvers
Solution
Step 1: Recall default resolver behavior
Default resolvers return data matching the field name from the data source.
Step 2: Identify cause of null result
If the data source lacks a key named 'user', the default resolver returns null.
Final Answer:
The data source does not have a matching key named 'user' -> Option A
Quick Check:
Missing data key causes null from default resolver [OK]
Hint: Check data keys match schema fields for default resolver to work [OK]
Common Mistakes:
Assuming custom resolver is always required
Blaming schema syntax without checking data
Thinking default resolvers are unsupported
5. You have a schema type Query { product: Product } and data { product: { id: 1, name: "Pen", price: 2.5 } }. You want to return the price with a 10% discount but keep default resolvers for other fields. How should you implement this?
hard
A. Use default resolvers only; custom resolvers cannot be mixed
B. Write a custom resolver for the entire 'product' field in Query
C. Modify the data source to store discounted price
D. Write a custom resolver only for the 'price' field inside Product type
Solution
Step 1: Understand resolver layering
Default resolvers work per field; you can override specific fields with custom resolvers.
Step 2: Apply custom resolver to 'price' only
Write a custom resolver for 'price' to apply discount, leaving other fields to default resolvers.