Forget REST. Here's How to Make a GraphQL API with Laravel

I am a full-stack web developer that enjoys coding in Laravel and React.
Search for a command to run...

I am a full-stack web developer that enjoys coding in Laravel and React.
No comments yet. Be the first to comment.
Here I will post some articles that help devs with simple and complex topics in Laravel.
Separating validation from your controllers with Form Requests
Switching stacks

If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...

Many developers know that full-stack apps with a Vue frontend can be quickly spun up with Laravel Jetstream. What many don’t know is that recently, the Laravel team made it easy to make an Inertia app with Laravel Breeze. In this article, we'll make ...

Extending the Rule Object with a DateTime Trait

Using Custom Console Commands

GraphQL, in my opinion, has been one of the best technologies to be released in the last ten years. GraphQL is a specification that describes how we can use just one query to get data from an API instead of hitting multiple endpoints with different HTTP request methods as seen with REST APIs. Because it's just a specification, GraphQL can simply be implemented in any programming language or framework. In my opinion, one of the best libraries for Laravel based on a PHP GraphQL implementation is Lighthouse.
Preliminaries:
composer require nuwave/lighthouse.php artisan vendor:publish --tag=lighthouse-schema. This will create the schema in our new graphql folder.php artisan lighthouse:ide-helper and it will create three other files in your root: _lighthouse_ide_helper.php, programmatic-types.graphql, and schema-directives.graphql.composer require mll-lab/laravel-graphql-playground./graphql-playground.First Steps: Before we get to work, we'll need to set up our database and do some seeding. Let's create a tiny and simple e-commerce platform where a seller can only create one item at a time. We'll also make each item have multiple sales.
Item at a time. We'll also be type-hinting our model so we can have an easier time creating our GraphQL schema.<?php
...
class CreateItemTable extends Migration
{
public function up()
{
Schema::create('item', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable(false);
$table->dateTimeTz('expiry_time')->nullable(false);
$table->string('description', 140)->nullable(false);
$table->float('price')->nullable(false);
$table->bigInteger('seller_id')->nullable(false)->unsigned();
$table->timestamps();
});
}
...
}
<?php
...
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Item extends Model
{
use HasFactory;
protected $table = 'item';
public function sales(): HasMany
{
return $this->hasMany('App\Models\Sale');
}
public function seller(): BelongsTo
{
return $this->belongsTo('App\Models\User', 'seller_id');
}
}
<?php
...
class CreateSalesTable extends Migration
{
public function up()
{
Schema::create('sales', function (Blueprint $table) {
$table->id();
$table->integer('quantity')->nullable(false);
$table->float('amount')->nullable(false);
$table->bigInteger('item_id')->nullable(false)->unsigned();
$table->bigInteger('seller_id')->nullable(false)->unsigned();
$table->timestamps();
});
}
...
}
<?php
...
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Sale extends Model
{
use HasFactory;
public function item(): BelongsTo
{
return $this->belongsTo('App\Models\Item');
}
public function seller(): BelongsTo
{
return $this->belongsTo('App\Models\User', 'seller_id');
}
}
<?php
...
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Authenticatable
{
...
public function item(): HasOne
{
return $this->hasOne('App\Models\Item', 'seller_id');
}
public function sales(): HasMany
{
return $this->hasMany('App\Models\Sale', 'seller_id');
}
}
{
user(id: 1){
name
email
}
}
The Work Begins: Here we will be defining our schema. It's important that you have some grasp of the types used in a GraphQL schema. We'll only brush up on this, so for a deeper understanding, I'd advise you to check out GraphQL's docs on the subject.
Date and DateTime. GraphQL's default scalar types are Int, Float, String, Boolean, and ID.Query type. In it, we'll add the query for finding the user, which both Item and Sale models are attached to.type Query {
user(id: ID @eq): User @find
}
The Work Continues:
type User {
id: ID!
name: String!
email: String!
item: Item @hasOne
sales: [Sale] @hasMany
created_at: DateTime!
updated_at: DateTime!
}
type Item {
id: ID!
name: String!
expiry_time: DateTime!
description: String!
price: Float!
sales: [Sale] @hasMany
seller: User! @belongsTo
created_at: DateTime!
updated_at: DateTime!
}
Sale object type on your own.The Work Ends:
{
user(id: 1){
name
email
item {
name
price
description
expiry_time
sales {
quantity
amount
}
}
}
}
Conclusion: