We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
user
, their posts
, and the comments
on
those posts, or the network of friends of a user, or the kinds of posts a user
tends to like.
UI requirements
Most apps are more than what you can see on the screen, but UI is what you are focusing on here, and thinking about the UI you want to kick-off your design process. So, let’s at start by looking at what you would like to build for your app’s UI Although a single GraphQL query can save you lots of calls and return you a subgraph of data, a complete page might be built up of blocks that have different data requirements. For example, in a sketch of your app’s UI you can already see these data requirements forming.
Thinking in graphs
Designing a graph schema is about designing the things, or entities, that form nodes in the graph, and designing the shape of the graph, or what links those entities have to other entities. There’s really two concepts in play here. One is the data itself, often called the app data graph. The other is the schema, which is itself graph shaped but really forms the pattern for the data graph. You can think of the difference as somewhat similar to objects (or data structure definitions) versus instances in a program, or a relational database schema versus rows of actual data. Already you can start to tease out what some of the types of data and relationships in your graph are. There’s users who post posts, so you know there’s a relationship between users and the posts they’ve made. You know the posts are going to be assigned to some set of categories and that each post might have a list of comments posted by users. So your schema is going to have these kinds of entities and relationships between them.
user
is going to have some number of posts
and a post
can have exactly
one author
. A post
can be in only a single category
, which, in turn, can
contain many posts
.
How does that translate into the app data graph? Let’s sketch out some examples.
Let’s start with a single user who’s posted three posts into a couple of
different categories. Your graph might start looking like this.



How graph queries work
Graph queries in GraphQL are really about entry points and traversals. A query picks certain nodes as a starting point and then selects data from the nodes or follows edges to traverse to other nodes. For example, to render a user’s information, you might need only to find the user. So your use of the graph might be like in the following sketch --- you’ll find the user as an entry point into the graph, perhaps from searching users by username, query some of their data, but not traverse any further.

/post/0x2
, then you’ll follow edges to the post’s
author and category, but you’ll also need to follow the edges to all the
comments, and from there to the authors of the comments. That’ll be a multi-step
traversal like the following sketch.

Schema
Now that you have investigated and considered what you are going to show for posts and users, you can start to flesh out your schema some more. Posts, for example, are going to need a title and some text for the post, both string valued. Posts also need some sort of date to record when they were uploaded. They’ll also need links to the author, category, and a list of comments. The next iteration of your schema might look like this sketch.