Skip to content

Latest commit

 

History

History
122 lines (101 loc) · 8.58 KB

File metadata and controls

122 lines (101 loc) · 8.58 KB

Build GraphQL API with Laravel

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to GraphQL

GraphQL serves as a powerful alternative to REST APIs for building robust APIs in Laravel. It allows querying specific data from the frontend without needing custom resources for attributes.

  • Key Takeaway/Example: The tutorial builds a simple CRUD app to create, read, update, and delete company data, demonstrated in GraphQL Playground where you can query company names or additional fields like domains. Ask AI: Introduction to GraphQL

Prerequisites and Project Setup

Start with PHP, Composer, NPM (optional), and Docker installed. Use Composer to create a new Laravel project.

Configuring Laravel Sail

Laravel Sail simplifies Docker setup for local development. Install it via Composer and publish the configuration.

  • Key Takeaway/Example: Run composer require laravel/sail --dev, then php artisan sail:install and select MySQL. Create a bash alias for easier commands, and start with sail up -d. Ask AI: Configuring Laravel Sail

Installing GraphQL Library

Choose between Lighthouse and Rebing for GraphQL in Laravel; Rebing is used here for simplicity.

  • Key Takeaway/Example: Install with composer require rebing/graphql-laravel, then publish the config via php artisan vendor:publish --provider="Rebing\GraphQL\GraphQLServiceProvider". Ask AI: Installing GraphQL Library

Database Migrations and Models

Create a Company model with migration and factory. Define fields like name, contact email, street address, city, country, and domain.

  • Key Takeaway/Example: Use sail artisan make:model Company -m -f. In the migration, add string columns for each field. Update the model's $fillable array and factory with Faker for dummy data.
// Example in CompanyFactory
return [
    'name' => $this->faker->company,
    'contact_email' => $this->faker->companyEmail,
    // ... other fields
];

Seed with Company::factory(15)->create() and sail artisan db:seed. Ask AI: Database Migrations and Models

Defining GraphQL Types

Each model needs a corresponding GraphQL type to define its structure.

  • Key Takeaway/Example: Run sail artisan make:graphql:type CompanyType. Define fields matching the model (id as ID, others as non-null strings) and link to the Company model.
// In CompanyType
public function fields(): array
{
    return [
        'id' => ['type' => Type::id(), 'description' => 'The auto-incremented company ID'],
        'name' => ['type' => Type::nonNull(Type::string()), 'description' => 'The name of a company'],
        // ... other fields
    ];
}

Ask AI: Defining GraphQL Types

Implementing Queries

Create queries to read single or multiple companies.

  • Key Takeaway/Example: Run sail artisan make:graphql:query CompanyQuery and CompaniesQuery. For CompanyQuery, return a single CompanyType by ID with Company::findOrFail($args['id']). For CompaniesQuery, return a list with Company::all(). Add to config/graphql.php under queries. Ask AI: Implementing Queries

Implementing Mutations

Add mutations for creating, updating, and deleting companies.

  • Key Takeaway/Example: Create mutations like CreateCompanyMutation (returns CompanyType, uses Company::create($args)), UpdateCompanyMutation (find by ID and update), and DeleteCompanyMutation (returns boolean, uses Company::findOrFail($args['id'])->delete()). Add to config/graphql.php under mutations. Ask AI: Implementing Mutations

Testing in GraphQL Playground

Access /graphiql to test queries and mutations.

  • Key Takeaway/Example: Query companies for names and domains. Create a company with fields like name: "Apple", then update or delete by ID. Errors like internal server error occur on invalid IDs. Ask AI: Testing in GraphQL Playground

Conclusion and Advanced Topics

GraphQL enables flexible frontend querying, used daily in production apps.


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: