0
0
GraphQLquery~30 mins

Default resolvers in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
GraphQL Default Resolvers Practice
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Import ApolloServer, create a new server with typeDefs and resolvers, then export it as default.