GraphQL with Laravel and Vue
GraphQL is often looked at as the successor to REST APIs. It provides a powerful query language and allows you to make requests for exactly the information you want, nothing more and nothing less.
Many services now offer GraphQL APIs making it easier for users to interact with their data. With its powerful type system, the syntax is intuitive, and error messages provide helpful feedback. In this series, we’ll take a look at using GraphQL within the Laravel and Vue ecosystem. We’ll build out the obligatory blog as a full-stack GraphQL application. In the backend we’ll make use of the popular lighthouse package which allows us to define our GraphQL schema. In the frontend, we’ll leverage Vue Apollo to allow us to communicate with the backend and display the data in the browser.
Progress
Series Info
- Episodes
- 16
- Run Time
- 2h 56m
- Difficulty
- Advanced
- Last Updated
- Jan 4, 2022
- Version
- Latest
Series Episodes
- Episodes (1)
- Episodes (3)
Lighthouse Queries for posts
We start building our blog application and set up our Post model, migration, and relationships. After that, we install and set up the lighthouse package and GraphQL playground to easily make requests. We explore a few directives that allow us to query all posts and single posts.Lighthouse Mutations for posts
We continue working with lighthouse and take a look at writing to our posts using mutations. We make use of directives that allow us to create, update and delete posts.Lighthouse Resolvers
Whenever any of the built-in directives in lighthouse aren’t enough, we can make use of custom Resolvers and write our own Eloquent queries. We re-create some of our queries using custom resolvers instead.
- Episodes (2)
It’s Just an AJAX Request
If you only need to make one or two requests to a GraphQL endpoint, you can just make use of the tools you’re already familiar with on the frontend. In this video, we make use of thefetchAPI to grab the posts data from our GraphQL endpoint.Make Requests from Laravel
You can also make requests to a GraphQL endpoint from within Laravel itself. In this video, we take a look at using Laravel’s built-in HTTP client to query our GraphQL backend.
- Episodes (6)
Vue Apollo Queries
Let's move on to using Vue Apollo as a dedicated library for handling GraphQL requests on the front-end. We take a look at installing Vue Apollo and then work on queries for grabbing all posts and grabbing a single post.Vue Apollo Query Components
An alternate way to perform queries is to make use of Query Components. Everything is done in the template and scoped slots are used to pass any necessary data into the template. Let's take a quick look at this process.Vue Apollo Mutations
Let's move on to mutations and work on creating a post. We take a look at using themutatemethod to perform GraphQL mutations within our app. We also review how to handle the loading state as well as errors.Vue Apollo Mutations - Updating Posts
Let's finish off our CRUD functionality and work on mutations for updating and deleting posts. We also take a brief look at some caching options within Vue Apollo.Vue Apollo Pagination
Our backend is already setup to return results with paginated chunks. In our front-end, we have to make sure that we are querying for results based on the page variable in the URL.Vue Apollo Composition API
If you're making use of Vue 3 and Vue Apollo v4, you have the option to use the Composition API as well as composition functions to perform your GraphQL queries and mutations.
- Episodes (2)
Authentication in Lighthouse
Let's next review how to perform token-based authentication using Sanctum in a traditional REST API. We then convert the login functionality over to a GraphQL mutation. Finally, we make use of a package to convert all of the auth functionality to GraphQL.Authentication in Vue Apollo
In the front-end, we store the token that is generated when the user is logged in. We then make sure to use that token to make requests to protected GraphQL queries.
- Episodes (2)
Authorization on the Server
Let's make sure that we have the proper authorization in place to ensure that logged in users can only perform certain actions.Authorization Links
Let's finish up the series by ensuring that links only display if the logged in user's authorization is correct. Thanks for watching!
