An Introduction to GraphQL in Go -Tutorial 01

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I recently tried out graphQL and built a server both in node and Go to compare. Seemed really interesting and something I'd like to explore further

👍︎︎ 1 👤︎︎ u/johanlejdung 📅︎︎ May 16 2020 🗫︎ replies
Captions
hi everyone inside from tutorial i SunNet and unless tutorial we could be looking at the basics of working with craft ql in cool now this was the most popular topic for doing a video this weekend based on the poll I posted in Thursday and I just like to say thanks to everyone that posted in that and thanks to everyone for their great suggestions now if you do have suggestions for content you would like to see in the future then please let me know in the comment section down below and as always the full text version of this tutorial can be found on my website and I'll also leave a link to that in the description below so let's dive in now what we're going to cover in this tutorial well we're gonna cover the basic cv behind graph Gail we're then gonna look at how you can create a simple graph GL app and go and then we're also gonna look at how you can query graph Gale and go and get responses back from the server so let's dive into 3d know let's have a look at the traditional approach that most developers tend to follow now imagine you were developing a front-end application that was written in something like reacts our view or angular now the traditional approach would be to hit a number of API endpoints to retrieve the information you want to render within this front-end client know this approach is great and it's one of my favorite proaches to developing applications however from more complex applications this can sometimes end up with multiple HTTP requests just to render simple components now let's imagine that I'm a developer working on an application that skills to thousands if not millions of users per day if I'm just trying to render a simple component that features our tutorial title and they need to have the tutorial endpoint which returns a response like this then suddenly I'm getting a lot of redundant information and this can be very very expensive in terms of the network costs associated with it when it starts to scale to thousands if not millions of requests no this is where graph KO comes in to save the day with graph Gale I can define a query that contains only the fields I want or need for my particular component that squiddy would then be sent to the graph Kyocera and the graph geoserver would then do the job of resolving each of these fields to the various API endpoints in order to call it that they are into one response that is an instant back to the client so one of the key points to note here is that the client is only making one HTTP request the graph your server the graph your server itself is then doing subsequent HTTP requests these are far quicker and more fashion as the graph go server tend to live in the same underlying infrastructure as the rest of your EP is now one important thing to note is the graph jail is not a query language like traditional sequel as an abstraction that says in front of API is and is not tied to any specific database or storage engine now this is actually really cool as it means we can stand up graphical servers that interact with existing services and then we can build other components or clients around s new graphical server instead of having to worry about modifying existing rest DPI's kills with the basic CD of the way it let's dive into Visual Studio code and start creating a really simple graph QL application angle okay so as you can see here I've got an incredibly simple MnDOT cofell that just simply prints a graph key off the toriel no let's start to modify this so that we can build up a graph a simple graphical application now I'm gonna start off by importing the graph gear package that we're gonna be using and this is gonna be github.com slash graph QL - go slash graph QL now within the main function we're gonna start off by defining the fields and how they resolve so I'm gonna start off with fields is equal to graph QL dot fields and first we're gonna do is the hello field and this is gonna be graph QL dot field like so and we're gonna say this type as a graph GL string and then we're gonna define the resolve our function so func P graph QL dot resolved badams and this is going to return an interface or an error now I send here that we typically do the subsequent HTTP requests to retrieve the information that we then resolve for this particular field but for now we're gonna keep it nice and simple and just returned were told if I can spell it right and there's no error so we'll just return Nell like so now let's add the comments so just then here and here and then just below where we've defined this fields object or the angular define real queenie is equal to graph QL object config I so be a name which will be Rick query and then we're gonna pass in the fields so fields fields like so next we're going to find the schema config and I'll go into DES and more information in just a second but for now graph kelskiy my config pass in the query and that's going to be gradual the new object query like so and then finally schema or error is equal to a new graph QL schema so graph QL new schema schema conflict and then as we're getting an error we want to do F and er does not equal now logged or fatal F field to create new graph QL schema and then we're gonna throw the error like so cool cool so let's break down work that here and a lot but more detail no effectively these three lines of code set up the schema for our graph QL application with an ischemic we define in the query tape and the squiggly tape acts as the mean entry point for every graph QL quit e coming into our graphical application here now we've then map that's to a new graph QL object and which is called Rick query and within this we pass in the fields that we want to resolve which has hello and there is all of our function like so cool so now that we have the basic schema up and running let's have a look at how you can then query against the schema now I'm gonna start off by defining the query itself which using backticks we're just gonna define curly braces and then hello now that's hollow field is gonna map up to this hollow field within our schema and then I'll resolve to this function here so we should see that whenever we pass unless query to our graph key or schema here it will return the data what old that now let's see if that is the case so the first thing we're gonna do Pan Am's equal to graph QL params I'm gonna pass on the schema we're trying to query against and request string that will be the query and what to do R equals R after L do basically execute our query and then F length of headers is greater than zero we want to log the fatal have failed to execute graph QL operation errors and we want to use percentage plus V to promote the errors so our tour errors like so okay now if there are no errors then we want to print out in jason so our jason or header which we're gonna leave blank is equal to Jason Marshall the response and then we want to print necessary so fmt dr. Neff and percentage s slash n wrap these in quotes and then pass NR Jason like so go fix that one we taper there and as we're importing or using the JSON package ensured that encoding jason has also been added to the list of imports at the top of your application now with everything in place let's taste two CFS runs so go run mean doc cool and as you can see our queries been successfully against our graph geo service and the field has resolved successfully to return the data wild awesome so now that we have got a really simple graph Gale server up and running and we're able to query against that let's take a step further and build a more complex example there we're gonna be creating a graph Gale server the returns are series of anonymity tutorials as well as their author and any comments made on those particular tutorials so let's start off by going up to the top of our application and defining a couple of strokes that will represent a tutorial and author and a comment so tape tutorial will be a struct this will have a title string we'll give it an ID to be an int and an author and which will be tape author and comments which will be an array our slice of comment next let's do the tape author strucked give that a name to be Strang and toriel's which will be slice of tape n and then finally let's do tape comment which will be a struct which will just have a body of tape strength no the next thing I want to do is to create a simple private function called populate which is gonna return a slice of tutorials soryo like so so unless I'm gonna define an author which will equal author author can tape name it forbs and tutorials we're gonna map in one like so next let's define a simple tutorial so tutorial is equal to tutorial and give an ID of 1 comma at the end a title of go graph QL tutorial give an author which will be a pointer to an author and comments so comment and the body is gonna be first comment cool next we're gonna do VAR tutorials to to reals like so tutorial and then tutorials equals append tutorials tutorial nice sweet tongue-twister never return tutorials go and I'm just gonna fix the ID here cool so with this in place let's start off by creating a new object in graph QL using the graph Gail dot new object function that we used before we're gonna define the three types using graph Gail's string straight to taping sorry these will match up with three strokes that we've already defined our comments doctors arguably our simplest as it just contains a body so we can represent this as a comment type fairly easily like so so within our main function we're gonna do VAR comment type equals graph to L dot new object and then graph QL door object config which will have a name comment and then the fields are going to define fields I ought to graph girl fields tape graph QL dot string oops sorry so the fields are gonna equal body I should say master step graph yield up field and this field is going to be taped graph your strength it's bear then we add the commas like so and we comma at the end there as well okay next we'll tackle the author struct and define that as a new graphical object as well so the VAR author tape because graft yield new object graph QL dot object config name is equal to author and then the fields are gonna equal graph QL dot fields and then we're gonna have the name which will be graphed y l dot field again it's gonna be type string graph your string and then we're going to do it tutorials tutorials like so which is gonna be another graph QL field and this is going to be tape graph QL dot new blessed graph QL dot and like so go and then finally we want to define this tutorial tape so just below where we've done the author tape let's do the following some fire tutorial type equals graph QL dot new object once again and wrap your l dot object config the name is gonna be tutorial for this one and then we're gonna have a number of fields here so graph QL dot fields first one is gonna be ID Kraft yelled out field and this is gonna be you tape graph your back end capitalize the end maybe another title sort of title another graph ko field this is gonna be tape graph cured string and then author which is the first more complex one it's going to be graph key field and then the tape is going to be author tape which we defined above and then comments just below this is going to be on a graph G field and this is gonna be tape graph killed our new list and comment the tape awesome and let's just close off these commas perfect so now they've defined the ER tape system let's save a updating our schema to reflect these new types well define two distinct fields the first will be our tutorial field which will allow us to retrieve ndividual tutorials based on an ID passed into the query and the second will be a list field which will allow us to retrieve the full array of tutorials that we have defined enemity so for this we're gonna overwrite the existing fields that we have and I'm gonna change hello to tutorial I'm gonna change the type to tutorial type and I'm gonna add a description so get tutorial by ID I'm gonna pass on the arcs which are the arguments passed on to this field and graph QL taught field config argument this is going to be an ID at graph QL dot argument config and then let's type for this IP is gonna be graphed yellow dot and like so now just below this we want to define the resolver function so I'm gonna want to do the following so ID or okay as equal to P the arcs ID so this is basically trying to retrieve the ID from the map of arguments passed into this query and then I'm going to typecast us to end I'm gonna say ok FS as part of that argument then I'm gonna do the following so for four underscore to ignore the index tutorial and range tutorials I want to do F and tutorial the ID is equal to the ID passed on to this query and I want to return that tutorial or no like so awesome and then I want to do no and no for now so just quickly we're trying to s3 over this range of tutorials which is currently undefined so I want to do the following so tutorials just at the top of my main function altitude tutorials is equal to our populate function so that will populate less less than two toriel's with the tutorials that we've defined here you know the next field that we said we're gonna define as the ol Westfield so I'm gonna come down here and I'm gonna do the following so make sure it's in line and is and list is gonna be and graph QL dot filled the tape for this is going to be graphed ul da new lest and this is going to pass them tutorial tape and then the description as get fill tutorial list and then we're gonna define the resolver function so resolve equal func p graph kill thought resolve params there's old pans and this is again gonna return an interface or a Nader now with an S we're not doing any filtering we're just returning the Lester populated tutorials like so awesome so now that we've updated our graphical schema and the field list let's try having some of these fields now so I'm gonna hit the left field first of all and I want to retrieve the ID and the title for every tutorial within my tutorial list now opening up the terminal I'm just mean to slave the beggar I'm gonna do a cool run window cool and as you can see here having this last field with this particular query has returned and a realist of all of the tutorials that we have currently populated and it's only returned to the ID and the title for those tutorials which is a perfect cool so the second field to be defined was the tutorial field that featured a couple of arguments so let's try and update equity and taste to see whether this works within our query and now so instead of last I'm gonna do tutorial and then I'm gonna pass in an ID of one I'm gonna remove this field here cuz we don't need it and then I'm also going to query the author of that's tutorial with the name and the tutorials that that author has also written alright once again go run Minto go and as you can see here is successfully returned the tutorial matching the ID of one and it's passing back the author with my name and the IDS of all the tutorials I've written as well as the title of this tutorial so that's all we're going to cover in this tutorial now in this tutorial we've covered quite a lot of grounds such as the theory Bank graph QL and why you should use it we've also covered the basic hello world example from the graph graph GL package and then we've looked at how we could extend this to expose a number of tutorials are a number of fields that would query against an end memory database in the next tutorial we're gonna be looking at graphical mutations and we're also gonna be looking at how we can expose this graphical application as an endpoint so that other clients can interface with it and retrieve the data that the Wesch know if you enjoyed this tutorial then please leave a like in the video and let me know in the comment section down below what else you would like to see as always thanks for watching and I'll see you in the next video Cheers
Info
Channel: TutorialEdge
Views: 16,427
Rating: undefined out of 5
Keywords: programming, tutorials, coding tutorials, tutorialedge
Id: AlLBG6HrE7E
Channel Id: undefined
Length: 22min 22sec (1342 seconds)
Published: Sat May 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.