Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Projects

Project: A Bookstore Query API

Build a read-only GraphQL bookstore API: write the SDL for authors, books and reviews, add resolvers over in-memory data and run nested filtered queries.

Time to put the earlier lessons together. In this project you design and "build" a small read-only API for a bookstore: authors write books, readers leave reviews, and clients can ask for exactly the slice they want, with filtering and nesting.

A real GraphQL server cannot run on this site, so the project has two halves. The schema is written in real SDL (read-only blocks you could paste into a real server). The resolvers and data are runnable JavaScript, executed by a small stand-in executor that supports field arguments and nested selections, exactly the features this project needs. Real libraries (Apollo Server, GraphQL Yoga, graphql-js) would run the same resolver logic.

You should know Fields, Arguments and Nested Selections and Resolvers before you start.

Step 1: Plan the graph

Three types, two links

Author Has many books.
Book Has one author and many reviews.
Review Belongs to one book.

The graph follows how clients think, not how tables look.

1 / 2

Step 2: Write the schema

This is the contract. Notice the choices: Genre is an enum, the list fields are [Book!]! (a list that exists and never holds null), book(id:) is nullable because an unknown id is a normal case, and averageRating is nullable because a book with no reviews has no average.

GRAPHQL
enum Genre {
  FICTION
  SCIENCE
  HISTORY
}

type Author {
  id: ID!
  name: String!
  books: [Book!]!
}

type Review {
  id: ID!
  stars: Int!
  comment: String
}

type Book {
  id: ID!
  title: String!
  year: Int!
  genre: Genre!
  author: Author!
  reviews: [Review!]!
  averageRating: Float
}

type Query {
  books(genre: Genre, authorName: String, minRating: Float, limit: Int = 10): [Book!]!
  book(id: ID!): Book
  authors: [Author!]!
}

The limit: Int = 10 default stops a client from accidentally downloading the whole catalog. (Real APIs usually go further with pagination.)

Step 3: Data and resolvers

The data lives in plain arrays. The resolvers are small functions with the (parent, args) signature. Only fields that are not simple properties need one: relationships (author, books, reviews), the computed averageRating, and the root queries.

Run this to see the executor answer three queries. The executor handles { field(arg: value) { nested } }, which is all this project needs. Variables, aliases and fragments are left out to keep it short, and its parser is a stand-in: real servers parse the full language.

JS The bookstore API and three queries

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at what happened:

  • The first query used the genre argument and returned only the two requested fields per book.
  • The second walked from a book to its author and reviews, and averageRating was computed by a resolver (4.5 from stars 5 and 4).
  • The third shows book(id: "99") returning null (the nullable field doing its job). The limit: 1 argument on Author.books was silently ignored, because the schema does not declare it there. A real server would reject it with a validation error before running anything. That is one reason you always use a real validating server and never a toy.

Step 4: Test the resolvers directly

Resolvers are ordinary functions, so you can test the business logic without any query text. This habit pays off later; see Testing GraphQL APIs. The next example checks the filtering rules with small assertions.

JS Unit-test the filtering logic

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The business rule "a book with no reviews does not match minRating" is a design decision, and a test now documents it.

Step 5: Ideas to extend it

  • Add a reviewCount field and a sortBy: BookSort enum argument (TITLE, YEAR).
  • Replace limit with a Relay connection (Pagination).
  • Spot the performance trap: Author.books called for many authors fires one lookup per author. The fix is DataLoader.
  • Add a mutation addReview with a payload type (Modeling Errors); the next project does exactly that for a todo list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is book(id: ID!): Book declared with a nullable return type?

    An unknown id is expected. With a non-null return, a missing record would bubble null up and fail the parent.
  2. 2. Which fields in the project need their own resolver?

    Plain properties are served by the default resolver. Relationships and computed values like averageRating need code.
  3. 3. What does limit: Int = 10 protect against?

    A default limit puts a sensible cap on result size, though real APIs add proper pagination.
  4. 4. Why test resolver functions directly?

    Direct tests are quick and precise. You still add some query-level tests to check the wiring.

Summary

  • Plan the graph from the client's point of view, then write the SDL as the contract.
  • Choose nullability deliberately: nullable for expected absence, [Type!]! for lists that always exist.
  • Only relationships, computed fields and root fields need resolvers.
  • Defaults on arguments keep responses bounded.
  • Resolvers are plain functions, so test their rules directly.

Next: Project: A Todo API with Mutations.