GraphQL Laravel server w/ graphql-laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys Andre here and today I'd like to take a look at a graph QL server package called graph QL laravel this is similar to the lighthouse package which we used in my graph QL series to build out an app they are both similar in that they are both graph QL servers however they take a different approach which we'll take a look at throughout this video if you haven't seen at least in the first video in that graph QL series definitely check it out as I'll be making comparisons between both packages so these packages both make use of the graph QL PHP package which is a vanilla PHP implementation so this graph QL laravel package is more similar to that whereas the lighthouse package sort of conforms more to the graph QL spec and makes use of heavy directives so if you don't know what that means don't worry we'll be going over everything in this video okay so let's get started let's install this I have a level app running already and I didn't do anything through it yeah so let's compose required it okay let's publish the config okay and that is it okay so let's do some quick setup in that first video where we took a look at this lighthouse package which is right here we took a look at an example where it was a basic block implementation so the models the models we had were users comments and posts so we'll do the same thing we'll just have users in posts and the relationship will be a user has many posts and a post belongs to a user so let me quickly set this up in this example which is a blank layer of a lap so let's do this quickly so let's make a post week model sorry let's make a migration okay so let's just copy and paste off so the user has many posts a user has many posts a post it's go to post here and it's just do it belongs to a relationship and like I said we want two comments here it's the same thing same relationship between posts and comments but we'll just keep it simple okay and it's just still the migration so it's create posts table yep so I'll just grab all this and it should be great posts there we go okay so that should do it and let me just set up my environment file I haven't created already so route route oh sorry it's a graph QL there well example route okay let's migrate and hope everything works and it does cool and let me just quickly whip up some seed data it's going to manually in the database so let's make a few users and let's make a few posts okay so I have two posts here two for Aundre and two for John okay so now if you take a look at the original package so let me just close these okay so remember on in the first video of the original package we spent most of our time in this schema graph QL file so there are three things or two things depending on how you want to look at it that we have to do here so the first thing we should do is define the types and these types are just the models and they have these same fields on the models and this is how they are defined and in the lighthouse package they conform to the graphic UL specification so all of this is the graphical specification but it does have some differences in that it uses directives to get things accomplished in this graph QL package graph QL level package I mean it is the same thing but everything is in PHP so there's more code to write so let's go ahead and start doing that so let's go into the docs and let's take a look here say create a query there's no dedicated documentation page like lighthouse has everything is in the readme here so so says first you have to create a type and that's what we want to do so again we want to mimic we'll start with the user this okay so you have to make a new file in this folder app graph UL type just makes bigger and then we have to define everything in PHP like I said so everything here we have to define it in PHP okay so let's do that I kind of wish this package had a CLI tool to scaffold everything out so that's one improvement I think they can improve on maybe if I have time I'll look at putting in a PR okay so where does it go it goes into app graph QL type and it's called user type so app new folder graph QL and type let's make it a folder type okay and new town new file so user type so each type is separated into their own file whereas in this package everything isn't here you can actually separate them in different files but in general everything is in this schema that graph QL file so that's one difference I'll just paste that in sorry I have to open PHP and it's based at it okay let's take a look at this from the top any other errors here okay looks good okay so all your imports and the attributes and just specifying the model a name and a description this is fine and this public function fields returns an array of fields so this is the equivalent of this these are the fields and see within each field they have a type and that corresponds to this so this field has this type and this exclamation mark means it's required so to do required we have to use this non null function so let's go ahead and do this so we don't need this alias because the ID is just ID in a database so we have email which is a string so again that is this and it's not it's required here so should be required what we can add that and if we like let's add in type string okay and there is no name here for some reason so let's add in a name and it's put it above email so let's say name name is required to write yeah same same thing name string and description is the name of the user and let's remove this is me this has to do with authentication which we won't look at in this video okay so this is just a resolver method so if you want to return something different from what the actual value of the field is then you can define a resolver like this in this format and it will resolve it here so we're not going to do that but it's nice to have that option okay so let me save this we have the type defined and the next thing we did in the lighthouse package was define the queries so we'll define the post in a second let's just define the queries first and we'll also look at the relationship in a second so here we have to define all the queries available to the consumer and the most basic one is just returning all users so let's see how to do that so back into the docs so we have the type defined and we also have we have to add the type to the config graph QL which is already there by default I believe so config graph QL and your section 4 types here somewhere so here's the section 4 queries and there's a section 4 types it's actually not defined so let's go ahead and do that so they have these examples here so we don't have this so I'm going to comment this out and same with well when we get to queries and mutations I'll show you that but these don't exist so we have to remove those okay so to define the types we just have to let me grab this and it is a user type and that lives in app graph QL type user type class okay so our type is now defined and now we can make a query okay so it says you can use the facade we won't be using that okay so now we can define the query so let me just grab this again I kind of wish they had a CLI generator and this goes into app graph QL query and it's called users query so let's do that I've graphed kewal query just make a new folder query and you file called users query I think that was called users query okay let's paste this in and let's take a look we have so this is just the name of the query and this type let's go back to our lighthouse 1 so yeah this is the name so users and this type is what it returns so in this case in lighthouse we want to return a list of users and this is what this is saying it's a list of users so again it's the same thing just in PHP format and this arguments function sort of specifies arguments which can be confusing because this query can list all users but it can also filter in them down to using an ID and email and in this implementation we have them separate so we have one for all users and then we have one where you can find by an ID but this one allows you to do both I'll show you in a second so after that this looks fine this is the resolver so this is where it gets the eloquent query and retrieves the users so in lighthouse we just used the all directive or the fine directive and you can also use custom resolvers if you want to do it that way but here it it's more of the custom resolvers approach where you've got to specify the query yourself so this is just checking if there are arguments again I kind of think it's better to separate it out and then if there are arguments then you just find them using this eloquent query that there isn't then you just return everything so let's go ahead and see if this works I think we have to define it first in the config so yeah right here so we have to specify that this is one of the queries so let's go into I can figure again let's go up here and again these all exist so I mean it's calling them out same with the mutation which we'll look at in a second and it's just paste that in so it's exposing a user's query here and this is where it lives and all the logic for it we put a comma there okay so now finally if we go into our craft cue well client and what is called graph QL they're about example tests so this is the original URL as playing with graph QL laravel example test and let me just erase this so this is oh yeah so sorry that shouldn't have brackets okay so this should work and it does cool so it's returning all the users in our database so let me show you the arguments so like I said it does take arguments and they are optional which should be separate but let me show you that it works and they take two arguments and I think the ID takes precedence again this should be a separate query but let me just show you so if I search for ID and they defined it as a string should be an int but it's say 1 and then it should just return Andre there we go okay let's quickly take a look at relationships because that doesn't work yet so essentially you would like to be able to grab the posts from the user and then list that out as well so let's do that so the first thing we have to do is define a post type so let's go ahead and do that so I'm gonna do a new file post type PHP and I already have it copied in my clipboard I'm gonna paste it in so it's basically the same thing as the user type just specified for the fields of a post so posts always post so a post has an ID a title and content and here's the relation which we didn't take a look at in the user which we'll do in a second but we just have to specify the type so graph QL type user and they can see find it from the config and it's just this one that we have already and here's a description for it and we don't need any custom resolvers or anything like that so I'll save that and it's defined the relationship here too so I'm gonna grab this for the user I mean for the post in the user right here base that in and the relation is posts so a user has many posts the type is a list of posts so if you type the list of graph QL type and select post okay and let me just say a list of posts written by the user okay and I think after you just import the graph QL here and I think it's just graph QL and I think I have that in here already sorry and the post type I mean and I do have it cool and yeah I think that's it now we have to specify the post and I'll have to the post query as well so let me just type that paste that in as well let's make a new file here called posts query posts query PHP and pasting it in but again it's basically the same thing as the users query that we did already so it is a list of posts and again there's an optional argument which I think should be separate and to resolve it you just have to post all which is the one who are interested in but if you want to pass in an ID you can do that as well and now we just want to our config and it's add those so posts and posts query and add the type the type is down here post post a type and yes let's test this out save last refresh ok let's try posts and it's just say title ok so it looks like it does work content yep ok so there is ID 1 if you want everything just remove this and there are all the posts cool so let's try the relationship so we want the 4 posts we want the user associated with it it looks like it works and we want the username and email there we go so this post was written by Andre that's one - this one is written by John and John and let's take a look at the other way so let me just comment this out query users ID aim but we also want their posts and then we want the title and that seems to work - go ok now let's quickly look at mutations so let's go back into the documentation and let's take a look at this mutation and it's almost the same as a query except it's a mutation so let's go ahead and copy this and this one is updating the password but we'll make one for creating a user so let's copy that and this goes into app graphic oil mutation so let's go to that app crafty well new folder mutation and what's it called okay we have to make our own say create user mutation so file create user mutation a PHP space you're saying and it's like changes create user and mutation create user so the type let's go back to our lighthouse example so here's a create user the type is just a thing it returns so in our case we want to return the created user so that's all we're doing here and the arguments are the things we need to create a user so here we have a name email and password so we don't need this ID but we need a name and email and I'm just gonna paste them in to save some time and it's basically the same thing just we need a name and email and they are of type string and then in the resolve function is where it happens so this is for updating the password we don't want to do that we want to user create and then we want a name and that's coming from arcs name so this is what the user is specifying in the graph QL query we want an email and we want a password but we want to make sure that the password is hashed so we do decrypt okay let's see if this works and we have to specify this in the graph QL file so there is a section here for mutations so I just copy this space it in it's called create user and app graph QL mutation create user mutation and I think that is it see if this works so let me just do it here so it is a mutation okay see if it appears create user and then see if you know there you go name email password so let's try this out Tony Stark email Tony at Avengers comm and password is Iron Man and let's return the ID and the name okay hopefully it works there we go it created a new user and if we just uncomment this you should see that in the list of users so let me just change this to users let's get to names there we go cool so there you have it guys we've taken a look at this graph QL laravel package so you can build out your graph QL server again it is a similar package to the lighthouse package but takes a different approach and makes use of PHP more so I think they're both great it's just a matter of preference so try both out and see which one you like better please like comment and subscribe if you haven't already done so thanks for watching guys see you in the next one Kate thanks bye [Music]
Info
Channel: Andre Madarang
Views: 4,888
Rating: undefined out of 5
Keywords: laravel graphql, laravel graphql client, graphql laravel, laravel graphql query, laravel graphql mutation, laravel graphql api, andre madarang, drehimself, laravel graphql server, graphql server laravel, graphql laravel graphql-laravel, laravel graphql lighthouse, laravel graphql vs lighthouse, graphql laravel vs lighthouse, graphql laravel server package, laravel graphql server package
Id: 4-kCbBFIYOw
Channel Id: undefined
Length: 26min 8sec (1568 seconds)
Published: Mon Jan 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.