Rails API Tutorial Episode 05: JSON:API and Active Model Serializers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello uh everyone uh this is mohammed from uh rubio maroc today i will explain how to use a json api using the active model serializers this is the main websites of the json api and here is an example response using json api as you see there is a links section which uh include the previous next or last pages here the data which include the type of the record here its articles the id and the attributes here it lists all the attributes in the year the relationships and the relationships is only include the type in the id and later there is a an included section which gives more information about their relationships yeah let's check out why we should use the json api uh first uh if you agreed with your team that you want to follow the json api specifications and second you want to focus on what your projects need for example you want to include only relationships that you need also if you want to only return fields that you will display also its cover filtering sorting pagination also pagination links are added by defaults and finally efficient caching in this tutorial we will implement the json epi using the active model serializer gem why you should use this gem simply uh if you are comfortable using the active model serializers gem if you didn't watch the previous episode uh go ahead and check some pros of of this gem also i explained how to create serializers and some other stuff and this tutorial is gonna be a continuation of the previous one so please go and check it out second if you want to use the includes and fields statically i say statically which means the api doesn't allow you to specify which relationships to include in which fields to return although i am going to try to make them dynamically by adding some tasty code moreover there are other gems which i will explain in the next episodes like json api resources graphics json api that's rb all these gems uh allow you to uh to add this this includes in fields dynamically also if you want to include the pagination links out links automatically uh if you want to include some model related links like edits uh path for example last and not least it comes with a possibility for this realization which means that you can deserialize parameters from json api structure to a normal json to use in the create and update action let's let's have a look at our serializer that we created the before for example we have here the books realizer we implemented caching i will remove this just to uh focus on uh on the json api response structure here we resorted like title language called number of pages in the outsource and the outsource realizer will have like id and the name name which cannot be like displayed as follow let's run the server and call our api http localhost port 3000 let's say books to json yeah here you see that we have just a normal uh json structure so now to make it like json api we simply have to change the adapter to use the json api because the default one is the attribute that we have used before yeah let's go and change this adapter i will just copy this and go to uh our project config initializers and create a new file for example ems dot rb i will pass this here and yeah we have to restart our server and try again here we go awesome so we have the data section with the with the array of the books here we have the first book here is like the type and the id the attributes that we wanted the uh in the relationships which is like outers data and here the id of the author let's say the second book here the same the third book the same here is the fourth book here the same so and this book has uh three outers so yeah this one it doesn't come with uh with the included section of the outers if we want to do that we just have to uh go to our uh controller and and added here so we just to include outers let's try this again awesome here we have like the list of the books as before and at the end we have like an included section which include the uh more information about the authors here so yeah this means that if we have for example many relationships here so we exactly have to define which relationships we have to include their uh their data what about if we want to um to return like some specific attribute let's say that we have to uh to return um for example only the title and maybe the number of pages without language code so this we also have to define here in the controller so we do uh fields and here we specify for example from the books we want to return the title and number of pages oops so if we come here yeah so we just return uh the two here also we can do the same for for alter let's add like two uh attributes attributes let's say foo and bar and we will like create like foo which will return like some texts here test in the bar will return i don't know just one two three four five and here we specify which which fields from the authors that we have to uh to to to show so we do for example uh full name and bar for example so in this case we want we will not show the full here we go we have the full name in the bar we don't show everything yeah this looks great but as i said before that's the uh the includes and and fields are are static here you can you can do the same here uh in the show action maybe you want to add more includes or to add more fields or vice versa you don't want to uh to show here too many data yeah you you have all the flexibility to change that yeah let's check the uh the documentation of the active model serializer in the uh json api shima here you see that uh to make them like dynamic so you can include some relationships that you want so you can specify them in your url the same as fields yeah let's apply this in our example let's go to the terminal http http [Music] localhost for 3000 here books that json and we want to do the include equal outers and fields works equal to uh what did we have title title and unknown pages and fields alters equal to full name let's say food design instead of bar that's gonna be our url it's still like the the same but we have to make some changes here to make this uh dynamically here what we added we added basically something uh like like that include outers which is here fields are books is the title and known pages and here uh fields from the authors its full name and foo uh yeah it's showed like this just because of the uh of the console yeah let's copy this and make them dynamically i will comment this out and try everything from here yeah the includes um so here what we want is params um include um so we want to do uh simply uh splits kerma so when we receive like outers uh and something else here for example writings so we won't like to split them into an array but sometimes the it's this can be like uh empty so what we will do we'll do just here this uh symbol here to not break our application if we don't pass anything yeah let's try this again here we go it works for the include so the included here is uh it's uh it's uh it's works let's um do the same for um the um for the uh fields so we want to replace this code here so here we have like params um fields that um so what we have here is we have like uh multiple uh multiple keys for the fields so what we want to do we want to keep this uh this as it is but we have to uh uh convert this string to an array so what we can do is we can transform this uh the values of the uh of the value here to an array so there is a method which is called transfer values here we do like value value dot splits yeah and we want to split like the crew masks yeah let's break this into lines and here the same just in case of if we don't pass anything yeah let's go ahead and try that it works we have like the included section here and we'll return um language code and non-page non-pages title so there is something wrong let's check that out yeah let's do some debugging let's add the pie by bug for debugging um i'll try that again let's check what do we have in params fields yeah we have like books title chroma on pages and authors which has full name and coma through what i can see here is that here we use like strings instead of the symbols but yeah we have we specified like uh symbols let's change this and see if that will work or not so let's do uh let's convert this to as json yeah here we go but everything is a string if we do like symbolize keys yeah we here we have like symbolized keys and now let's do uh our transfer values oops trends for values method which can be here value the value that splits the arrays by chroma yeah in here they see like uh strings so we have to convert everything to symbol let's do map to sim yeah here we go we have it let's try this out here let's remove the debugging here try this again let's finish that and try this again yeah here we go we have like full name with foo and we have like title with known pages now we can like include only full name we can and also include only title yeah here is its full name only title with the includes it's very uh very great yeah as as said uh this gem it doesn't have a lot of uh [Music] ready uh features to use for uh for json api in the next episode uh i will be uh explaining like some other gems which which has all this features uh implemented yeah you can do a lot of modifications but i will use active mode or serializer if if i have some simplicity in our api but if there is like some complexity or some cases where you want to use like different include fields i would use another gem like uh json api that's rpg gem or graffiti yeah there are too many uh other uh options that we can use i will be explaining them in the next episodes in term of caching you have to try that in your in your in your projects and maybe you do some tests in the uh um in the uh in the production environment from your application and and see if it works well or not because here in the um in the documentation it says that um the caching might not works well so you have to try that yeah i come to the end of this episode thanks for watching and see you in the next one
Info
Channel: Ruby au Maroc
Views: 632
Rating: undefined out of 5
Keywords: rails, rails 6, api, rest, ams, active_model_serializers, rabl, jbuilder, rails api, rails api tutorial, rails 7, ruby on rails, ruby on rails tutorial jsonapi, jsonapi spec, jsonapi specification, jsonapi serializer, jsonapi filter, jsonapi rails, jsonapi pagination
Id: CMauLPXJiH4
Channel Id: undefined
Length: 19min 18sec (1158 seconds)
Published: Mon Nov 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.