#119 Introduction to GraphQL
2-11-2018

Summary

GraphQL is a query language for your API. In this episode, we will look at adding GraphQL to our Ruby on Rails application and seeing how we can pull down data and create records.
6
rails api graphql 17:38
  • Student & Teacher
  • $9.00 / month
  • Valid School Email Required
  • Same Access as Pro
  • Subscribe Now
  • Pro Monthly
  • $15.00 / month
  • Access to Pro Episodes
  • Invite to Slack Channel
  • Priority Episode Suggestions
  • Ad Free
  • Subscribe Now
  • Pro Annual
  • $150.00 / year
  • Access to Pro Episodes
  • Invite to Slack Channel
  • Priority Episode Suggestions
  • Ad Free
  • Subscribe Now
  • Teams
  • starts at $75.00 / month
  • 5 Users Minimum
  • $15.00 / user / month
  • Same Access as Pro
  • Subscribe to Teams
they're handing now yeah and that and that and learned lessons said we're delighted flagged well which is eight when we win which order a yacht gross que el as was the different uses an else has certain situations were at all to get it's the right fit so this absurd on what to look it with how would you scrap do well in one hour would not use it that think they can generally be summed up with the idea that if you have a deep yard that is opened that others can program to it and interact with the graft well maybe a good choice however if you have a t p r and that is specifically for communicating with your back and war with a mobile app that you're developing or with the third party they get access to work with the grass too well maybe more trouble than it's worth again if he hadn't opened a pr newborn baby in an after party or in users developing their own applications and interacting with yours and rescue well to be a very powerful query language for you a pr it but for those who may not know grace kelly's please just a query language so you have one single and point your application that someone will write good to him with this once you have a point you would make a post request to it angular says in the day that of the day to day you wanting returned said he had a list of users a one returned instead of giving all of the information maybe just one certain aspects of the user may be there for same last thing email at the end user model has ah hundred other fields on it then that the fall and typically with your hp ice is going to return all that information instead with graph you well you specify which ones and you actually want the first same last name email and just returned that they don't also works with associations a construction yes the data to say if you wanna get all the users surfer same last him an email and if you want the list of comments been able to get the comments that a user has been it's all structure laid eyes on her though and to build something like this was something like active model zero wiser searches on builder it could be a bit more tedious but what can they do make that aspect very nice and all the dice about this they can change on request the city also want to get their birthday thing you would just had to modify your query day you're posting again now you're getting their birthday as well set to get started glad the gym rescue well toward jim bob the beach and rom bottom freezer you will set the kitchen they're within your application on the terminal being type roles generate then graft you well then install this will generate a couple of different files will notice that under the apple her within the graphic you well in school is so we're all gonna different things with tied to the mutations that within the pipes for this we're going to keep our queries so getting the list the users getting specific users are in his associations with l and then we had the mutations for which you need to to zurich going to be basically you're right a ball actions where you are creating workers editing or updating and deleting records in there within our controllers it created a graph to a controller and within our routes while we have this and point rescue well that points to this graph to a controller to the execute action it also added the graphic too well rails gem in this gem is going to be a interface for jesse development of armor and there were like to be able to grind and passer queries or code snacks and they generate eight model within the model and his clinical user will have a first name the last name the email and a birth date and the birthday bridges going to make it big field the number i am i'd read my database with rails to be migrate so next less good amateur will set location and check out the graphic too well and so if we did or local sports we thousand a week had been before slash scratchy que well you see that we're given eight under fair use them with this weekend pass out her application first create the opening close massages and then i can just put in the past field an essay does have some intel except for one where it can look at our application to see what's available in there we can run that's just as real came from our generator and it's in the tides query type while so to run it all going ahead execute he had seabed the response of we get back is jason and everything is gonna be wrapped within the day that so within here we ever test field and in the hello world they're within our team the seeds file as good as as some examples where we have to users that we're creating with a for sale last name and email and a birthday in a playful my database have made the field birthday in on the left hand side or december terminal and on the right-hand side that the rails server running so i'm going to tie the crop command and april command will just basically make a request to eat in point and i'm making a post so there's going to be the h t t p for post them passing some day that or this can also be seen as the parameters so at the parameter key query and that is equal to then i'm passing in the spring has field just like we did for the graphic you will interface them passing in d h t t p local host for three thousand and two the graphic you well in point and so if we want as we get this huge response back but if we score weapon look you see that we have it invalid authenticity token because of actual interface is a p i we can skip the bill for accent and then be verified authenticity took him and we're back and run the school request you see that we got a query and the query was the most ashes test field im we got our data back and so now let's look at how we can get our listened users back so honored the app graft you well colleagues we had the queer retired better be filed within here you see that has feel that we created so in order to get the list of users back we will have to create a separate kind and that type is going to be warmer and user the tongue and create a new file and i'm just going to call this the user to add on or being it would already have types name spaced they will put energy is retired and to define as the guy that rescue well the object i'd tried to find the will pass in a block the within years to define the type we first had to give it a name the same we're going to call it a user getting at the fields so we'll have a field id the field first name a field last name field email and the field birthday to you should also declared the type said to declare the type i'd be eating colony tired start i'd be there for the for sale is going to be in types that string same for the last name and email and the birth date we're going to make it at state but i'm partially with the birth date it's not actually supported the type so we will have to use spring those are just on response will return the birthday as a string in a little trick is if you want to be labeled as a maybe this is not the name they you want action show the user been saying you want them to use something like birthday the can specify birthday and then pass and they know the grammar will use property they will actually specify that this birthday is matt to the property worth they sent them back to work we're retired no need to create a new interface and this is going to be the new feel that we create yeah he's careful casing like all users or if you wanna keep it on to score that's just fine as well that we need is specified water types are so incentive passing it as a parameter here and this could create a block and they called type and this is going to reference the types there we had to reference or user type that we created so that's going to be the types and then the user type we can give it a description they will call result and as el mundo with the object arguments and the context would think here which is one return the user dot all a weekend closer block this is dedicated from all we can make a medical request and the scientists said a passing in the test field we wanna pass in the all users the type that we created we need a pass in some parameters saw creates a mustache is here now call the id and then the sausages get the first name now we get back our day dep all users then enter ready to wii years in his attentions there we have ideal for him but the first name john id do and the first name jane if you were you guess is out of the browsers well so we can get are all users and with the all users we want the id the first name the last name and let's also get the birth date center birthday and you'll see that it's birthday that's like a good sense of feel that we had specified now laying clear out the comments here of that so what does we get the warrant you see that we get are dated back and if we want to get rid of some information like the first name or the last name to just get ill with the birthdays you get better as well in there or a pr it has changed to only would turn the information there be a specified there really is the nature of what rescue all does it as a whole lot more under the hood and became a good job op board with associations and other can equate the general gist of it is that it's providing you a customizable a pr into the into user so far we ever 'cause field and are all users but what happens if we want to find a specific use her clear we are database for a particular user all weekend as creating another field and i'm just gonna call this one user because we're finding one single and they will open up the block below declared type but this time because we're returning just one user which is scoring the types user the week they know the description there were just a call this one user by d would it take our game it as our unit is gonna be our ideas and this was gonna be required for we call this field there were an unspecified as as a type of any id no we have our resolve that we will normally call with the lens of the world again are objects are arguments as well as the contacts wanna look evasive him or in a later episode one weekend do with the state of them within here and we just simply to argue certified as we normally would and this time we're going to pass sentence of the friends i'd be read as in the parks id has this is all we're getting from our our unit and says types user and we did call this a user tied to make sure that you are matching the names as he did with the new definitions and so now we can create a not a query that's why we wanna find the user and users going to require the idea argument and will choose the id to the weekend has another has the one he get some information like the first name and the last name elf we weren't as you can see that we get our id user to back when it does verify that with also getting the id and so with these two examples if we wanted to be able to these informations on the list of users then also just as he is writing too we're able to do that the assembly just hang these and there were turned all users and then returned the user specifying been looking up one particular user thing that's going to run this so now under the day that we have for all users has and this is the array of each one the users are only returning the id and the birth date there we have our one individual user which we're we're turning the id number two and there for same last name and her father you know made the mistake of calling the user place and this is gonna have a conflicting has ski that doesn't like this were playing any good or bad review see that a complex it's er let's take a look at what you'll take to modify record so working with their list the users within the mutation types and that's under the graph que well types mediation type the net gain of the field similar to how we did for the queer retired so start a field and i'm just pinnacle this create user then just to simplify this because we're gonna have a lot information in your own going to pass in another field and then upgrade the other complications that within this module create a user that we will need to pass in the field so what does feel create user we'll create a new file under the grass que el mutations that out his call this a create user and so with our module mutations we have our create user to witness said this to the grass que well and we lay vacation then define if so within this block we're going to first get the name and it's gonna be the previews are no we didn't specify air and put fields and these are the field that would it be accepting on the graph to a query so we didn't specify area but feel and the first one is gonna be our first name there we had to declare the type so this is the media types of strength if we wanna make the first same required be a throwaway been in there and then it's going to fail unless it's the first in his past in our cities something similar for the last thing about what's on the t required yeah we can also requesting email and also the birthday and so once we have our input feels we also had to have the return field and with the return field this is what we're expecting to be passed back in them what is best for your user then the pipes and then these are tight this is as we do find in the graph to all types the user type we then pass in the result and the resulting in this game via london and we get that center object the arguments in the context know we can start creating our user so we'll just said a user equals to the user that create and we get thousand of lockyer for user or you just pass in the hash doesn't really matter there will ever use a bad first name we're going to set the sequel to the arguments that we got back from our rescue a query or in this area to the first name miller did the same thing for the last name and the same for the email and the birthday once we finish that wouldn't expect he was all to pass back a hash the hash of britain aspect is simply just the user because we have the tide user type here said for our return field and we're returning the user then the user record that we just created it's going to be defined by wall weekend requests back by the fields here now within the graphic you will we think reading attention and the need to share we're going to pass an ass there within here we need to specify reaction in this and be creative user name he had to pass any input the input is going to be your list of runners there we're wanting to create so while creating ease with the person and in tuscany give this user's name john well said the email to jon huntsman of an example dot com gossip has in a birth date notice that the birthday present specifying year is not birthday that simply because when i created the mutation as specified birthday to i didn't specify birthday that would've been better because as can be more consistent with what the user is taken back as well use january thirty first nineteen eighty it's not true we create the user we can specify what we want to display back to the user so with the user we can get back the idea that was created we also get the email in there we can get the birthday so if we run as you see that it creates the user we get the id back in and we have our email and the birthday her run this again been given a last name and our last name will be smith and even though this is damage as i got to put in the car most vicious cut out the clearing works so long run as as we get our id then then we saw at the same if my short heard the weekend were turned us named if we wanted the best integrated know the record because we're just running the same mutation clean the user and so i'll do that through glass cube well and all of others different scenarios in recycling said you might be able to do down the road because this can get very complicated this is inside the inherent nature bachelor's is kind of thing all together and just a couple different places that they met their sell by better way to organize he was offers it to keep things a bit cleaner what it's all for this episode thank you for watching