AppSync Masterclass: modelling the Twitter API in GraphQL schema

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay so in this lesson let's design our graphql schema but before we do that let's just revisit how twitter works and identify the key entities that we need to define so if you look at the profile page we see the name handle a description and we need something to capture the image and the background for my profile page my location my website when i joined as well as how many followers and following i have and how many tweets i have created and if i go to another user's profile i also have to know whether or not i'm following this user and if they follow me in this case they don't otherwise you will say follows you and we need some way to fetch my tweets and replies and tweets that i've liked and for every tweet we need to know who the creator is some text potentially some image how many likes and retreats it has and how many replies he has received in the case of a reply we need to know who was the user that we reply to and also what was the tweet that we reply to as well and for retreat we also need to know who did the retreat and in the case of a retreat the likes and the retweet count is not for the retreat but for the original tweet itself and if we go to the home page we also have a timeline which are tweets from the people that we're following whereas in the profile page we see our tweets either the ones that we created ourselves or retweeted or if you go to the tweet and replies page then we also see replies that we made to other tweets as well in our graphql schema we need to have made provisions for all these different cases but first thing first every graphql schema starts with the schema definition where we define the top level types for query and mutation which we then need to define here as a type for query and then another type for mutation so what operations do we need to support for queries well good starting point is to get my profile which should look at my authenticated user and then return my profile and we can add an explanation mark to say that this is not nullable but remember we also need to be able to fetch someone else's profile so we also need a generic get profile query that takes in someone's screen name which is a string and again it's not a notable argument and this should also return a profile but if you go back to the twitter page when we visit our profile there are probably information that we don't want other people to be able to see and equally we don't need to know if we are following ourselves or if we are being followed by ourselves because that just doesn't make sense whereas it makes sense for other people's profiles where you need to know whether they follow you and whether or not you're following them so usually i will have separate profile types one for my profile which i can fetch using my authenticated user and therefore this query doesn't need to take any argument i can then define these two types separately one for my profile one for other people's profile and for my profile i need to have my id we can use the built-in id scalar type from graphql and we say this is not nullable i need a name which is going to be a string the screen name also a string we also need to know the url for the profile image as well as the background image so we can capture those as you are as image url and we can represent that using abstinence the built-in aws url scalar type and we need another field for the background image uil which is also of type aws url and when you create a new profile you're not required to set your profile image or a background image so for new profiles this would be no and therefore they are notable we need a short string for our bio which again is nullable and a string for our location and the website which is going to be a url again we also need to capture our birth date which we can use the edible state scalar that absent supports and create it at time stamp which we can use edible state time and we're going to make that required i need to know how many people are following me when i'm viewing my profile so there's a required hint and how many people i'm following also an int how many tweets i have created and how many tweets i've liked and potentially when i fetch my profile maybe i also want to fetch the first page of my tweets and as i scroll down i can fetch more pages of my tweets and we can model that by adding a tweets array like this and again the explanation mark says that if this is an array then the objects inside that array need to be of the type tweet and they cannot be no so you cannot have nodes in this array but we can't just have an array of tweets because we also need some way to fetch the next page and so on so what we can do is we say we return a page of tweets instead and define that tweets page type here to contain an array of tweets and also a next token for pagination and if there's only one page then this would be no but otherwise we can use it to capture an opaque token that from the front end we can request the next page of tweets and to do that we need to add another query to get tweets that takes a user id how many tweets we want to get in a single page and the next token from the previous page and again that's going to return another tweets page and this is how we can then paginate through pages and pages of tweets as the user scrolls through the page and we also need to define the other profile type that i've had here to capture the profile information that i need to show when i visit somebody else's profile page which is probably the same as my profile plus a boolean for whether or not i'm following them and whether or not i'm followed by them and since both of these types my profile and other profile since both of these types share many common fields we can also define an interface that capture those common fields that any profile will in this case all the fields from my profile can go into this interface and we can say that my profile type implements this a profile interface and so does the other profile type this should look familiar to you if you have worked with statically typed languages like java or net to distinguish between a type and interface the convention in the java and dotnet also is to use the i prefix so we can adopt the same approach here and name our interface i profile instead of just profile and we still need to define the type for tweet and the tweet will have also an id and we need to know the profile that created this tweet i profile when we need to know when the tree was created once again it's going to use the edible state time a text that's in the tweet how many replies this had how many likes and how many retreats and we also need to know if we've liked this tweet and if we have retweeted it before and even though a replied is also a tweet it behaves slightly different because we also need to know the tweet that we reply to and the profile that we reply to as well so we can define a separate type for reply which has the same basic set of fields that a treat has but we also need to know that this is a reply to another tweet and that we are replying to which user but what if we reply to a reply so that means that we can't just have this as a tweet this also can be a reply to another reply or a retweet so to capture that commonality we can define another interface called i tweet that would need to be implemented by both a tweet and the reply and we can change this in reply to tweet field to return and i tweet instead so it can be a tweet or a reply but there's one more case we got to consider which is the case of a retweet we also need to implement this i tweet interface and we can also reply to a retweet and if we have a look on twitter when you reply something that someone has retweeted then the reply goes to both the person who created the original tweet as was the person that retweeted it and whoever that were mentioned in the original tweet so to represent that correctly we probably have to change our reply to users to users and turn that into an array instead and when we reply to a tweet that tweet can be a reply it can be a tweet that someone has created or it can be a retweet and for retweet we need to know the id and who retreated this when was the retreat and the original tweet that this is a retweet of which itself can be a tweet a reply or maybe a retweet as we discussed before a retreat doesn't have its own counts for replies likes and so on so like here we're looking at the sarah's retreat but these stats are for the original tweet so we don't need those properties on the retweet which means in terms of the common fields that we can capture and extract into the itreat interface is just going to be the id of the tweet the profile of the person who created the tweet and the when was created and since when we fetch a page of tweets it can be any one of these types a tweet a reply or a retweet so we need to change the type of the tweeter right here to use an i tweet instead including if we would ask for the trees that a user have liked and a limit argument for the page size and the next token for pagination and this also returns a tweets page and on the profile page we can also get the list of profiles that i'm following as well as the list of profiles that are following me so let's add some queries for those get followers of a user the page size and the pagination token and similar to what we had before we can add a new type for profiles page and we will add another one to get the profiles that a user is following and let's define this profile page type we should like what we had with the tweets page type we need an array to capture the profiles as well as the next page token and we also need to have a way to fetch my timeline so let's add that query get my timeline limit for the page size and the next token for pagination here we can also return a tweets page and one last query if we go back to the profile page and we say edit our profile and we can change our image to support this flow to upload an image we need to have a way to return a signed s3 url so that users can upload the images and then use them let's add another get image upload url query that takes in the extension of the file you want to upload and the content type and returns an aws url so those are all the queries we need what about for mutations well we're going to need to have some way to edit our profile edit my profile and after i'm done i should return a new profile and if you look at the property that's available in the i profile type and if you go back to twitter and say edit profile you can change your name your bio location website and birth date so we need to capture all of those here name image url for a new profile image and background image url and so on you can either define all of those fields as arguments for this mutation but a convention in the graphql is to capture those as a separate type which is a special type called input which cannot be returned by any query or mutation and can only be used as arguments so let's create a new input type called the profile input to capture all the arguments that we can pass into the edit my profile mutation including name image url background bio location website and birth date and then we will take an argument for a new profile using this input type and we also need something to create a new tweet let's just take the text for now and that's gonna return the tweet that's just been created we can like the tweet by passing in the id and here we don't really need to return anything but with graphql there are no void type so the convention is to instead say return a boolean and you can always return two if you want to and we can do the same to add the new unlike mutation and then retreat and unretreat and we can reply to a tweet and in the reply we have taken some text and this returns a new reply and lastly we can follow and unfollow someone by taking their user id and again we don't need to return anything so let's just return boolean and do the same for unfollow so this is a complete the graphql schema that captures all the basic operations that we are going to support from the next lesson we're going to start to implement the whole thing you
Info
Channel: Yan Cui
Views: 1,316
Rating: undefined out of 5
Keywords: appsync, graphql, aws, cloud, serverless, software development
Id: bgZX0zDXRCY
Channel Id: undefined
Length: 18min 58sec (1138 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.