Build a GraphQL Powered API with Laravel
Time to read: 7 minutes
Often referred to as a platform or programming language agnostic query language, GraphQL is a comprehensive approach to guide software engineers in building data-driven applications. It is, in my opinion, the new API standard, offering client applications the flexibility to request exact data and not an entire record from the database. If you are not yet conversant with this technology or looking for simplified technical content to guide you through, you are in the right place.
In this tutorial, you will learn some of the fundamental concepts required to build a Laravel API powered by GraphQL. This is a shift from the conventional architecture of building REST APIs, as we will take a different approach in this tutorial. To help us explore GraphQL, we will build a bookstore API with an ability to carry out CRUD ( Create, Read, Update, Delete ) operations on data, in your database.
Prerequisites
You should have basic knowledge of building web applications with Laravel to easily complete this tutorial. Lastly, you need to installComposer globally on your computer to manage dependencies.
Creating a New Laravel Project
To begin, we will create a new Laravel application using Composer, by running the following command from the terminal:
The preceding command will create a Laravel project in a new directory named lara-bookstore
in your local development folder, or whenever you ran the command from. Go to the project folder and run the Laravel project to interact with the default functionality, using the following command:
Install GraphQL Laravel library
Several GraphQL libraries have been created by developers to make working with GraphQL easy within a Laravel project. Here, we will use the Laravel GraphQL library (rebing/graphql-laravel) Stop the application from running using CTRL + C
, hit Enter
, and then issue the following command instructing Composer to install the library:
Once the installation is completed, publish a copy of the configuration file from the vendor
folder using this command:
You will see the following output:
This will copy the configuration file into config.graphql.php
. We will return to modify its content later in the tutorial.
Creating Book Model
In this section, we will simultaneously create a Book
model and generate a migration file. Issue the following command for that purpose:
Next, the migration file will be used to create the Books
table and its corresponding fields. Modify the newly generated migration file by replacing its content with the following:
The Books
table will have title
, author
, language
, year_published
, and isbn
fields amongst others.
Now, add these fields as $fillable
properties for the Book
model. Open app/Book.php
and replace its content with:
Creating a Seeder File for the Books Table
Let’s create some initial data to populate our database tables using the Seeder class in Laravel. To generate a seeder file specifically for the Book
table, type the following command:
This will generate a new file named BookSeeder.php
within the database/seeds
folder. Open the newly created file and populate it with the following:
Here, we created an instance of the Book model and inserted an array of data containing the list of books with a title
, author
, language
, year_published
, and isbn
respectively. For these columns to be seeded with the data, we need the BookSeeder
from the default DatabaseSeeder
class. Open database/seed/DatabaseSeeder.php
file and update it with:
Create a Database and Set Up a Database Connection
We are all set to create a database table and insert some dummy data into it. Before you proceed, ensure that you have a database created locally and update the .env
file with your database credentials as shown below:
NOTE: Replace the YOUR_DB_NAME, YOUR_DB_USER, and YOUR_DB_PASSWORD placeholders with the appropriate details of your database.
You can now run the migration to create the database schema:
The included --seed
flag will ensure that the database seeder is run after the database schema has been created.
Now that we have an application with the appropriate data for testing the functionality of our API, we will proceed to set up the GraphQL server.
Building GraphQL Server
Earlier, we installed the Laravel GraphQL library that will be used for this application. In this section, we will start by defining the GraphQL schema. Schema provides the exact description of the fields in the database and defines how the created data will be retrieved through GraphQL.
Begin by creating a GraphQL
folder in the app
folder of your application. Within this folder, create three new subfolders namely:
Mutations
: This folder will house classes that will be used to carry out the insert, update, and delete operations.Queries
: GraphQL queries will be used to fetch data from the database and the classes for that functionality will be defined here.Types
: Types are objects that represent the kind of objects that can be retrieved from the database. TheBookType
class will be housed here.
In the next section, we will start creating the necessary classes within the folders above
Creating a GraphQL Type for the Application
Create a new file BookType.php
within the app/GraphQL/Types
folder and add the following code:
You will notice we created an $attributes
array that defines the name, a brief description, and the model linked, with the type defined here. Lastly, we defined a function that returns the properties of the fields as an array.
Creating GraphQL Queries
As stated before, queries will be used to retrieve data from the database. In this case, we will create queries to fetch a particular book and another to retrieve the details of all the books in the database.
The Book Query
Let’s create a file with the name BookQuery.php
within the app/GraphQL/Queries
folder and paste the following code in it:
Here, we defined the query name and referenced the type Book
that was created earlier as the GraphQL type to be used. The function args()
returns the unique identifier that will be used for a particular book, which is an id
in this case. Once resolved, the query will search for the book using the identifier and return its details.
The Books Query
Unlike the class defined in the previous section, here we will define a query to retrieve a collection of books. Navigate into the app/GraphQL/Queries
folder and create a file with the name BooksQuery.php
. Use the following content for it:
Creating the Mutation
Any query implemented that causes data to be written or altered is often sent through mutation. Mutations can be created to insert, update, and delete a record in the database. Let’s start by defining one to create a new record for a book.
Create a Book
Create a new file within the app/GraphQL/Mutations
folder and name it CreateBookMutation.php
. Open it and paste the following content in it:
It is important to note the name of the mutation as it makes it easy to reference this class later. In addition to defining the GraphQL type, we also returned the properties of each field before finally creating an instance of the Book
model and filling it with the arguments passed along with the query.
Update a Particular Book
To define a mutation to update the records of a particular book, create a new file within the app/GraphQL/Mutations
folder and name it UpdateBookMutation.php
. Open it and paste the following content in it:
Similar to what we had in the previous section, we created an attribute that specified the name of the query and created the field properties for the query arguments. Finally, we used the unique id
to retrieve the book of interest and update its details.
Delete a Book
The last mutation to be created is for deleting a specific book. Create a new file within the app/GraphQL/Mutations
folder and name it DeleteBookMutation.php
. Open it and paste the following content in it:
Registering the Schema and Type
After creating all of the schemas, they need to be registered within the configuration file of the GraphQL library used for this project. To do that, open the config/graphql.php
file, locate the schemas
, and update as shown here. Do the same for the types
property as well:
Run the Application
Start the application with php artisan serve
. This will start the local PHP server and run the Laravel application at http://localhost:8000.
Installing GraphiQL IDE
To test the application, we need to install a tool that can be used for testing GraphQL endpoints. One of the popular tools in the tech community created for this purpose isGraphiQL. It is a GUI for editing and testing GraphQL queries and mutations. Click here to download a version suitable for your operating system.
Once you are done, open it and use http://localhost:8000/graphql as the GraphQL Endpoint.
Testing the Endpoints
Return all books
Retrieve the list of books by running the query below in the query window:
This will return the collection of books seeded into the database earlier.
Return a specific book
Next, to fetch the details of a particular book, use this query:
Notice that we had to pass an argument that represents the unique identifier for the specific book.
Create a book
Here, we will start with mutations by using the following to create a book:
Update a Book
And to update the details of a specific book:
Conclusion
GraphQL is a game-changer and a great addition to the software development community. It is gaining more popularity because it is robust enough for any kind of application, irrespective of the size.
In this tutorial, we were able to conveniently build a Laravel backend API powered by GraphQL. We also set things up fundamentally and created a complete CRUD operation, which is similar to what is obtainable for any web resources.
Check this repository for the complete source code of the application we built and don’t hesitate to explore and build upon it. Looking forward to what you are going to build with the knowledge acquired from this tutorial. Happy coding!
Olususi Oluyemi is a tech enthusiast, programming freak, and a web development junkie who loves to embrace new technology.
- Twitter: https://twitter.com/yemiwebby
- GitHub: https://github.com/yemiwebby
- Website: https://yemiwebby.com.ng/
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.