Web Development

GraphQL Implementation in Laravel 12: Step-by-Step Guide

Introduction

Hey there, Laravel enthusiast! If you’re here, chances are you’re curious about how to implement GraphQL in your Laravel 12 application. You’re in for a treat because this guide is designed to walk you through everything you need to know—step by step, with code examples, and in plain English.

Whether you’re building an API for a new app or switching from REST to GraphQL for more flexibility, Laravel 12 makes it easier than ever. Let’s dive in and unlock the full potential of GraphQL in your Laravel projects.


What is GraphQL?

A Quick Overview

GraphQL is a modern query language for APIs, developed by Facebook. Unlike REST, which returns fixed data structures, GraphQL allows clients to request exactly the data they need. This means faster apps, cleaner code, and more control over data flow.


Why Use GraphQL with Laravel 12?

Key Benefits

  • Fine-Grained Queries: Fetch only what you need, saving bandwidth.
  • Single Endpoint: Everything runs through one API route.
  • Developer Productivity: Define types and relationships in one place.
  • Real-time Support: Easily integrate with tools like subscriptions.

Step-by-Step Guide to Implement GraphQL in Laravel 12

Step 1: Install Laravel 12 (Skip if already installed)

composer create-project laravel/laravel graphql-app

Navigate into the project directory:

cd graphql-app

Step 2: Install Lighthouse GraphQL Package

Lighthouse is the most popular GraphQL server for Laravel.

composer require nuwave/lighthouse

Now, publish the config and schema files:

php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider"

This will create a lighthouse.php config file and a graphql/schema.graphql file.

Step 3: Configure Lighthouse

Check config/lighthouse.php to tweak settings if needed, but the defaults work fine for most projects.

Defining Your GraphQL Schema

Step 4: Set Up a Sample Query

Open graphql/schema.graphql and add this:

type Query {
  hello: String @field(resolver: "App\\GraphQL\\Queries\\HelloQuery")
}

Step 5: Create the Resolver Class

Generate the class manually or create the folder and file:

// app/GraphQL/Queries/HelloQuery.php

namespace App\GraphQL\Queries;

class HelloQuery
{
    public function __invoke($_, array $args)
    {
        return 'Hello from Laravel 12 and GraphQL!';
    }
}

Step 6: Test the Query

Start your Laravel server:

php artisan serve

Visit: http://localhost:8000/graphql-playground

Run this query:

query {
  hello
}

You should see:

{
  "data": {
    "hello": "Hello from Laravel 12 and GraphQL!"
  }
}

Boom! You just created your first GraphQL query in Laravel 12.

Add Models and Types in GraphQL

Step 7: Create a Model

php artisan make:model Post -m

Add fields in the migration:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('content');
    $table->timestamps();
});

Run migration:

php artisan migrate

Step 8: Define a Type in schema.graphql

type Post {
  id: ID!
  title: String!
  content: String!
  created_at: DateTime!
}

type Query {
  posts: [Post!]! @all
}

Now, visiting /graphql-playground and running the posts query will return all posts from your database.


Bonus: Mutations in GraphQL

Step 9: Add a Mutation to Create Posts

In schema.graphql:

input CreatePostInput {
  title: String!
  content: String!
}

type Mutation {
  createPost(input: CreatePostInput!): Post @create(model: "App\\Models\\Post")
}

Now you can create posts using GraphQL:

mutation {
  createPost(input: {
    title: "My First GraphQL Post"
    content: "GraphQL with Laravel is awesome!"
  }) {
    id
    title
  }
}

So there you have it—a complete beginner-friendly guide to implementing GraphQL in Laravel 12. From setting up Lighthouse to defining types and running queries, we’ve covered all the essential steps to get you started.

If you’re looking for expert help or want to build your Laravel or GraphQL-based application professionally, consider reaching out to CodeHunger. They offer reliable and high-quality web development services tailored to your needs.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button