Modeling an Instagram clone using GraphQL and Dgraph Cloud - Dgraph Blog

A better data model…

There is no situation where you would know the usernames you follow before hand. You would have to query them first. However, that is not efficient, as you could follow many users (7500 Instagram, 5000 Twitter / Snapchat, 2000 YouTube etc).

So, similar to this post (before I understood GraphQL):

You should have a model like so:

User {
  id: ID!
  username: String! @id
  name: String!
  about: String
  email: String!
  avatarImageURL: String!
  posts: [Post!] @hasInverse(field: postedBy)
  following: [User] @hasInverse(field: followers)
  followers: [User] @hasInverse(field: following)
}

This way, you can actually query a follower feed, something that is impossible in noSQL.

Follower Feed

query {
  getUser(id: "0x1234") {
    following {
      posts(order: { desc: createdAt }, first: 10) {
        id
        title
        description
        createdAt
      }
    }
  }
}

and get the feed from data.following.posts[].

And one day when nested filters are available, you will be able to query posts directly with something like this:

query {
  queryPost(
    filter: { postedBy: { followers: { id: '0x1234' } } },
    order: { desc: createdAt },
    first: 10
  ) {
    id
    title
    description
    createdAt
  }
}

It is also worth noting that you would not use addPost nor updatePost in an actual production app as you cannot secure the fields and data using the @auth directive, only the user. There are also no pre-hooks, so you would have to write a custom mutation for both updatePost and addPost.

J