Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to GraphQL  ›  Lesson

Thinking in Graphs

Thinking in Graphs 12 minA GraphQL schema models the domain as a graph where object types are nodes and fields that return objects are edges, so a query is a walk that starts at a root field and follows edges until it reaches plain values.

Thinking in Graphs

A new developer asks, "Which endpoint gives me an author's other books?" In GraphQL that question has no answer, because there are no such endpoints. There is one graph of connected types, and you walk it.

The rule

Types are nodes, fields are edges; a query is a walk.

Model the bookshop as things and the links between them:

type Book {
  id: ID!
  title: String!
  author: Author!
  reviews: [Review!]!
}

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

type Review {
  id: ID!
  rating: Int!
  book: Book!
}
  • Each object type (Book, Author, Review) is a kind of node.
  • A field that returns another object type (author, books, reviews) is an edge: a link you can follow.
  • A field that returns a plain value (title, rating) is a leaf: where a walk stops.

Every walk starts at an entry point on the Query type, such as book(id:). From there you follow edges as far as you need:

query {
  book(id: "b1") {
    author {
      books { title }
    }
  }
}

This answers "other books by the author of b1" without anyone designing an endpoint for it. Links run both ways when useful (Book.author and Author.books), so a client can start from either side.

Design the graph around your domain, the way people talk about the business, not around your database tables. A Review has a book, not a book_id column; the server works out how to join.

The shortcut

To write a query from a question, say the path out loud, then nest it. Each "'s" in the sentence becomes one level of braces.

"The ratings of reviews of b1's author's books"Step 1 of 4
  1. Find the entry point

    Start at Query.book(id: "b1").

The usual mistake is designing fields like authorId: ID! and making the client look the author up in a second request. Expose the edge author: Author! instead.

Drills

Answer fast — trust the rule.

quick drill

In the bookshop graph, which field is an edge to another node?

quick drill

Where does every query walk begin?

quick drill

Which query lists the titles of all books by the author of book b1?

quick drill

A teammate proposes Review.bookId: ID! as the only link from a review to its book. What is better graph design?

Remember

  • Types are nodes, fields are edges; a query is a walk from a Query entry point.
  • Say the path out loud, then nest one level per step, ending at leaves.
  • Trap: exposing raw IDs instead of edges, and modelling tables instead of the domain.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWalk the graph

The schema also has a root field author(id: ID!): Author. Write the query for 'the ratings of the reviews on every book by author a2' and explain how you turned the sentence into nesting. Then say why author: Author! on Book is a better field than authorId: ID!.