Skip to content

Latest commit

 

History

History
224 lines (168 loc) · 4.12 KB

File metadata and controls

224 lines (168 loc) · 4.12 KB

Todo GraphQL API

A Laravel-based GraphQL API for managing todos with user authentication.

Tech Stack

  • Framework: Laravel 12
  • GraphQL: Nuwave Lighthouse
  • Authentication: Laravel Sanctum (Bearer tokens)
  • Database: MySQL
  • Frontend: Vite + Vue.js (optional, for development)

Features

  • User registration and login
  • JWT-like token authentication via Sanctum
  • CRUD operations for todos
  • GraphQL pagination support
  • Graceful error handling (returns null for not found objects)
  • GraphiQL playground for testing

Documentation

Installation

Prerequisites

  • PHP 8.2+
  • Composer
  • Node.js & npm
  • MySQL

Setup Steps

  1. Clone the repository:

    git clone <repository-url>
    cd todo-graphql
  2. Install PHP dependencies:

    composer install
  3. Install Node dependencies:

    npm install
  4. Environment setup:

    cp .env.example .env
    php artisan key:generate
  5. Configure database in .env:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=todo_app
    DB_USERNAME=your_username
    DB_PASSWORD=your_password
  6. Run migrations:

    php artisan migrate
  7. Seed test data (optional):

    php artisan db:seed
  8. Build assets:

    npm run build

Development

Running the Application

# Start Laravel server
php artisan serve

# Start Vite dev server (for frontend assets)
npm run dev

# Run both concurrently
composer run dev

GraphQL Playground

Access the GraphiQL interface at: http://localhost:8000/graphiql

Testing

# Run PHP tests
php artisan test

# Run with coverage
php artisan test --coverage

Code Quality

# Run PHP CS Fixer
./vendor/bin/php-cs-fixer fix

# Run Pint (Laravel's code style)
./vendor/bin/pint

Project Structure

app/
├── GraphQL/
│   ├── Mutations/
│   │   ├── CreateTodo.php
│   │   ├── DeleteTodo.php
│   │   ├── Login.php
│   │   ├── Logout.php
│   │   ├── Register.php
│   │   └── UpdateTodo.php
│   └── Queries/
│       ├── Me.php
│       ├── TodoQuery.php
│       ├── Todos.php
│       └── UserQuery.php
├── Models/
│   ├── Todo.php
│   └── User.php
└── ...

config/
├── lighthouse.php
├── sanctum.php
└── ...

database/
├── migrations/
│   ├── ..._create_users_table.php
│   └── ..._create_todos_table.php
└── seeders/
    └── DatabaseSeeder.php

graphql/
└── schema.graphql

routes/
└── web.php

tests/
└── Feature/
    └── ExampleTest.php

Authentication

The API uses Laravel Sanctum for token-based authentication:

  1. Register/Login to get a bearer token
  2. Include token in Authorization header for protected operations
  3. Logout to revoke the token

Token Usage

const headers = {
  'Authorization': 'Bearer your_token_here',
  'Content-Type': 'application/json'
};

GraphQL Schema

The schema is defined in graphql/schema.graphql and includes:

  • Types: User, Todo, AuthPayload, Message
  • Queries: me, todos, todo, user, users
  • Mutations: register, login, logout, createTodo, updateTodo, deleteTodo

Environment Variables

Key environment variables:

APP_NAME="Todo GraphQL API"
APP_ENV=local
APP_KEY=base64:your_app_key
APP_DEBUG=true
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=todo_app
DB_USERNAME=user
DB_PASSWORD=password

SANCTUM_STATEFUL_DOMAINS=localhost:3000

Deployment

  1. Set APP_ENV=production and APP_DEBUG=false
  2. Configure production database
  3. Run php artisan config:cache and php artisan route:cache
  4. Set up web server (Apache/Nginx) to serve public/ directory
  5. Ensure proper permissions for storage/ and bootstrap/cache/

License

This project is licensed under the MIT License.