Skip to content

PsyChaos/qsd

Repository files navigation

QSD - Query Simple Definition

A modern, production-ready API standard for PHP that combines the best of REST and GraphQL.

Tests PHP Version License

Features

  • 🚀 Hybrid API Standard - Combines REST's HTTP fidelity with GraphQL's relational flexibility
  • 🔒 Built-in Security - Complexity analysis, depth limiting, field authorization
  • 🗄️ Multi-ORM Support - Works with Eloquent and Doctrine via adapter pattern
  • N+1 Prevention - Automatic query batching and data loading
  • 🎯 Framework Agnostic - Core library works standalone or with Laravel/Symfony
  • 📝 Type Safe - 13 built-in scalar types with comprehensive validation
  • 🔧 Developer Friendly - CLI tools, schema export, query validation

Quick Start

Installation

composer require qsd/qsd

Laravel

// config/app.php 'providers' => [ QSD\Integrations\Laravel\QSDServiceProvider::class, ], // Publish configuration php artisan vendor:publish --tag=qsd-config

Symfony

// config/bundles.php return [ QSD\Integrations\Symfony\QSDBundle::class => ['all' => true], ];

Basic Usage

1. Define Resources

use QSD\Core\Resource\Resource; use QSD\Core\Resource\Field; $userResource = new Resource('User', [ new Field('id', 'ID'), new Field('name', 'String'), new Field('email', 'Email'), new Field('posts', 'Post', isRelation: true, isList: true), ]); $postResource = new Resource('Post', [ new Field('id', 'ID'), new Field('title', 'String'), new Field('content', 'String'), new Field('author', 'User', isRelation: true), ]);

2. Execute Queries

use QSD\Core\Parser\Parser; use QSD\Core\Tokenizer\Tokenizer; use QSD\Core\Execution\QueryExecutor; // Parse query $tokenizer = new Tokenizer('get user { id: 1 }'); $tokens = $tokenizer->tokenize(); $parser = new Parser($tokens); $ast = $parser->parse(); // Execute $executor = new QueryExecutor($registry, $dataFetcher); $result = $executor->execute($ast, $context);

3. HTTP Endpoint (Laravel)

// Automatic route registration at /qsd POST /qsd { "query": "get user { id: 1, fields: [name, email] }" }

Query Examples

Get Single Record

get user { id: 1 } 

List with Fields

list user { fields: [id, name, email], limit: 10, offset: 0 } 

Create Record

create post { title: "Hello World", content: "My first post" } 

Update Record

update user { id: 1, name: "John Doe" } 

Delete Record

delete post { id: 1 } 

With Relations

get user { id: 1, fields: [name, email], with posts { fields: [title, content] } } 

Aggregations

list user { aggregate { total: count(id), avgAge: avg(age) } } 

Architecture

┌─────────────────────────────────────────────────────┐ │ HTTP Layer │ │ Request → Middleware → Handler → Response │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ │ Security & Validation │ │ Complexity → Depth → Authorization → Validation │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ │ Query Processing │ │ Tokenizer → Parser → AST → Executor │ └─────────────────────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────┐ │ Data Layer │ │ ORM Adapter → DataLoader → Cache │ └─────────────────────────────────────────────────────┘ 

Key Components

Core

  • Tokenizer - Lexical analysis
  • Parser - Syntax analysis and AST generation
  • Executor - Query execution engine

Type System

  • 13 scalar types (ID, String, Int, Float, Boolean, Email, URL, Date, DateTime, Time, JSON, UUID, Enum)
  • Custom validation rules
  • Type coercion

Resources

  • Resource definitions
  • Field configuration
  • CRUD capabilities
  • Relation management

Security

  • Complexity analysis
  • Depth limiting
  • Cost calculation
  • Role-based authorization
  • Field-level permissions

ORM

  • Eloquent adapter (Laravel)
  • Doctrine adapter (Symfony)
  • DataLoader (N+1 prevention)
  • Relation resolvers
  • Query batching

HTTP

  • Request/Response handling
  • Middleware system
  • CORS support
  • Content negotiation
  • Error formatting

CLI Tools

Export Schema

# Laravel php artisan qsd:schema:export --format=json php artisan qsd:schema:export --format=markdown --output=schema.md # Standalone $exporter = new SchemaExporter($registry); echo $exporter->toMarkdown();

Validate Query

# Laravel php artisan qsd:validate "get user { id: 1 }" php artisan qsd:validate --file=query.qsd # Standalone $validator = new QueryValidator($parser, $registry); $result = $validator->validate('get user { id: 1 }');

Configuration

Laravel (config/qsd.php)

return [ 'debug' => env('QSD_DEBUG', false), 'resources' => [ // Register resources ], 'models' => [ 'User' => \App\Models\User::class, ], 'security' => [ 'enabled' => true, 'max_complexity' => 1000, 'max_depth' => 10, ], 'route' => [ 'enabled' => true, 'path' => '/qsd', 'middleware' => ['api'], ], ];

Symfony (config/packages/qsd.yaml)

qsd: debug: '%kernel.debug%' security: enabled: true max_complexity: 1000 max_depth: 10 route: enabled: true path: /qsd

Testing

composer test

All 336 tests passing with 954 assertions.

Performance

  • Query Batching - Automatic N+1 prevention
  • DataLoader - Request-scoped caching
  • Query Caching - Configurable result caching
  • Complexity Limits - Prevents expensive queries

Security

  • Depth Limiting - Prevents deeply nested queries
  • Complexity Analysis - Resource usage estimation
  • Cost Calculation - Rate limiting support
  • Field Authorization - Role-based access control
  • Input Validation - Type-safe with validation rules

Requirements

  • PHP 8.2 or higher
  • Composer

Optional Dependencies

  • Laravel 10+ (for Laravel integration)
  • Symfony 6+ (for Symfony integration)
  • Doctrine ORM (for Doctrine adapter)

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

Credits

Built with Claude Code

About

Query Simple Definition

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors