What Is GraphQL?
GraphQL is a new API standard that provides a more efficient, powerful, and flexible alternative to REST. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world.
Please check The Fullstack Tutorial to know more about it.
What Are We Trying To Build?
We will do simple CRUD operations using GraphQL. We will use lighthouse framework which is designed for GraphQL and Laravel. Read more about lighthouse framework.
Setting up Laravel and Lighthouse
Install the Laravel Installer as a global Composer dependency:
$ composer global require laravel/installer
Clone Lighthouse Tutorial
$ git clone https://github.com/nuwave/lighthouse-tutorial.git graphql-site
Go to the graphql-site directory and run the below command
$ composer install
Copy your .env.example file to .env file
$ cp .env.example .env
Modify .env file as per your DB credentials
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=graphql_site
DB_USERNAME=root
DB_PASSWORD=root
Create Database, Run Migration, And Seeding
Create a database by running below command in your terminal
$ mysql -u username -pPassword
mysql> create database graphql_site
Run migrations to create tables
$ cd graphql_site
$ php artisan migrate
Do database seeding
Add below code in database/seeders/DatabaseSeeder.php file which will insert data in your tables.
public function run()
{
\App\Models\User::factory(10)->create();
\App\Models\Post::factory(10)->create();
\App\Models\Comment::factory(10)->create();
}
Run below command
$ cd graphql_site
$ php artisan db:seed
Database seeding completed successfully.
Verify the tables and data in your DB graphql_site
Time To Browse GraphQL API
Start the server by running the below command
$ php artisan serve
Laravel development server started: <http://127.0.0.1:8000>
Install GraphQL Playground and interface should look like below
Browse:
http://localhost:8000/graphql
Try to query below data:
{
posts {
id
title
author {
name
}
comments {
id
reply
}
}
}
Output should look like below:
{
"data": {
"posts": [
{
"id": "1",
"title": "Prof.",
"author": {
"name": "Hettie Kohler"
},
"comments": []
},
{
"id": "2",
"title": "Prof.",
"author": {
"name": "Wade Ledner"
},
"comments": []
},
{
"id": "3",
"title": "Prof.",
"author": {
"name": "Ms. Beryl Reinger PhD"
},
"comments": []
},
{
"id": "4",
"title": "Mrs.",
"author": {
"name": "Dr. Ebba Collier"
},
"comments": []
}
]
}
}
Similarly to insert, update, and delete you can modify your graphql/schema.graphql file and check https://lighthouse-php.com/master/eloquent/getting-started.html#create
type Mutation {
createUser(
name: String!
email: String! @rules(apply: ["email", "unique:users"])
password: String! @bcrypt @rules(apply: ["min:6"])
): User @create
updateUser(
id: ID!
name: String!
email: String! @rules(apply: ["email", "unique:users"])
): User @update
deleteUser(id: ID!): User @delete
}
To test insert, update, and delete try to run below examples in your GraphQl playground
mutation {
createUser(name: "Tan", email: "tan@mailinator.com", password: "secret") {
id
name
}
}
mutation {
updateUser(id:29, name: "Test Update", email: "testUpdate@mailinator.com") {
id
name
}
}
mutation {
deleteUser(id: "31") {
name
}
}
Don’t forget to experiment and learn. Hope you find this article useful!
References:
https://www.howtographql.com/basics/0-introduction/
https://www.toptal.com/graphql/laravel-graphql-server-tutorial
4 Comments
Prava · April 6, 2021 at 6:05 pm
I went through this article and the wordings are awesome and simple to understand. The story really makes sense on how to setup and test GraphQL. I really liked this article.
Tanmaya Biswal · April 9, 2021 at 9:18 am
Thanks, Prava!
Arun · April 14, 2021 at 11:37 am
Great article !!
Tanmaya Biswal · April 17, 2021 at 3:01 pm
Thanks, Arun!
Comments are closed.