0
0
GraphQLquery~30 mins

Mocking resolvers in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Mocking Resolvers in GraphQL
📖 Scenario: You are building a simple GraphQL API for a bookstore. Before connecting to a real database, you want to create mock resolvers to simulate data fetching. This helps frontend developers start working without waiting for the backend to be fully ready.
🎯 Goal: Create a GraphQL schema for books and authors, then write mock resolvers that return fixed sample data for queries.
📋 What You'll Learn
Define a GraphQL schema with types Book and Author
Create a query type with books and authors fields
Write mock resolver functions that return fixed arrays of books and authors
Connect the schema and resolvers to create a working mock GraphQL server
💡 Why This Matters
🌍 Real World
Mocking resolvers lets frontend developers work with a GraphQL API before the real database and backend logic are ready.
💼 Career
Understanding how to mock GraphQL resolvers is useful for rapid prototyping, testing, and collaboration between frontend and backend teams.
Progress0 / 4 steps
1
Define the GraphQL schema
Create a GraphQL schema string called typeDefs with these exact types: Book with fields id: ID!, title: String!, authorId: ID!; Author with fields id: ID!, name: String!; and a Query type with fields books: [Book!]! and authors: [Author!]!.
GraphQL
Need a hint?

Use the gql template literal to define your schema string.

2
Create mock data arrays
Create two constant arrays called books and authors. books should contain exactly two objects with keys id, title, and authorId with values '1', 'The Great Gatsby', '1' and '2', '1984', '2'. authors should contain exactly two objects with keys id and name with values '1', 'F. Scott Fitzgerald' and '2', 'George Orwell'.
GraphQL
Need a hint?

Use arrays of objects with exact keys and values as specified.

3
Write mock resolver functions
Create a constant object called resolvers with a Query field. Inside Query, add two functions: books and authors. Each function should return the corresponding array books or authors.
GraphQL
Need a hint?

Use arrow functions inside the Query object to return the arrays.

4
Create the Apollo Server with mocks
Create a constant called server by instantiating ApolloServer with an object containing typeDefs and resolvers. Use const { ApolloServer } = require('apollo-server'); at the top. This completes the mock GraphQL server setup.
GraphQL
Need a hint?

Import ApolloServer and create a new instance with your schema and resolvers.