GraphQL with Laravel Lighthouse: First Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today on youtube you will see another free video of my newest course about graphql in laravel this one it's about graphql query with lighthouse server and with postman api client and with laravel graphql playground so practical example of installing it all running the first query and see how it all works if you want the full course the link will be in the description below or you can go to laraveldaily.teachable.com check out my other courses and maybe sign up for yearly membership to support this youtube channel for me to continue publishing free video here on youtube for you guys enjoy this one and see you guys in other videos so let's start with graphql by building a graphql server or installing a package that can help us with that i will remind you that graphql is more like a language like a schema but you need separate tools that can help you to utilize that schema and one of the most popular ones is called lighthouse at lighthouse php.com if you google laravel graphql there are a few alternatives for example ribbing graphql laravel is another package to do that and they are quite similar so it's a personal preference both are good throughout this course we will use lighthouse you can prefer another package and test it out but we will move on with lighthouse and to install that we go to the installation page get started and we just install it with composer require so i have a fresh laravel project totally fresh liable project no features with just 10 users seated in the database so 10 fake users and we will try to get them not from the rest api so we won't create api endpoint we will create a graphql server and ask for id and name of the user or id and email we will experiment with that so i have zoomed it in and let's install composer require paste into my terminal it installs the package of lighthouse then we need to publish the default schema schema is probably the most important thing for graphql we publish the default schema which is in graphql schema.graphql and let's open that file in our phpstorm there's graphql schema graphql and this is how it looks like so in your schema you define the types the queries some more syntax so in this default case there's a type user which corresponds to default user table of laravel with types and we will talk about all of those in detail later in the course for now i just want the first successful experiment so we can launch a query to get users or to get a specific user and it could return a user object like this or if we ask for specific fields it would return only those at this point we can already make those requests make those queries but to finish the installation in a more convenient way we should add a thing called graphql playground it will be kind of a web page that would be able to query the graphql so we can do composer require and then we go to our home page with slash graphql-playground url it will open on the left hand side you can run queries on the right side you will see the result so i will zoom it in and the query for example i want to query users with id and name and by default it will have an error because there is no paginator properly set up and default graphql schema has pagination so we will talk about how pagination works later in the course for now we will just change paginate with all so get all users we save the schema then we relaunch the same query and we have data with users id and name so we don't have any api endpoint all we do is launch this url graphql and in fact it's a post request and i will show you that in postman in a minute but for now what you need to understand is that we can define for example we need to return id name and email and then it returns whatever we need so this is graphql playground and it's actually quite similar to any api client like postman so to do the same thing in postman it's pretty similar how you would launch any api endpoint but you have to launch slash graphql for any request for any data whether it's users or later other eloquent models the same endpoint is graphql with post requests specifically and how do you define the parameters in the body section there's a separate thing called graphql and here you type in the query that you want so i copy and paste it from graphql playground for example and paste here and i send the request and we have the result with users the same thing just in postman with graphql so this is the minimum functionality how to serve the graphql powered data with just a few installation commands and you can publish that schema somewhere the graphql file to your front-end team and reading that schema they can perform various queries that they need for the front-end
Info
Channel: Laravel Daily
Views: 16,945
Rating: undefined out of 5
Keywords:
Id: cvEjaP631ew
Channel Id: undefined
Length: 5min 8sec (308 seconds)
Published: Tue Nov 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.