Imported from rebing/graphql-laravel (
AGENTS.md). Install upstream withnpx skills add rebing/graphql-laravel. Copyright stays with the author.
Agent Guidelines for graphql-laravel
A Laravel wrapper for webonyx/graphql-php. PHP 8.2+, Laravel 12+.
For development setup, commands, code style, and the PR workflow, see CONTRIBUTING.md.
Project Structure
src/ # Production code (Rebing\GraphQL namespace)
├── GraphQL.php # Core class: schema building, query execution, type registry
├── GraphQLController.php # HTTP controller for GraphQL requests
├── GraphQLServiceProvider.php # Service provider (config, routes, commands)
├── Console/ # 12 artisan make:graphql:* generators + stubs
├── Error/ # GraphQL-layer errors (client-safe, extend graphql-php Error)
├── Exception/ # PHP exceptions (config errors, not client-safe)
└── Support/ # Base classes, middleware, contracts, tracing, pagination
├── Type.php / Field.php / Query.php / Mutation.php # Core hierarchy
├── InputType.php / EnumType.php / InterfaceType.php / UnionType.php
├── ExecutionMiddleware/ # Pipeline middleware for full execution
├── Tracing/ # OpenTelemetry observability
├── Contracts/ # TypeConvertible, ConfigConvertible
└── Facades/ # GraphQL facade
bin/ # Dev scripts (standalone PHP, no autoload)
└── update-bc-baseline.php # Generates/replaces baseline XML from BC checker output
tools/ # Isolated dev tool installations (separate composer roots)
└── bc-check/ # roave/backward-compatibility-check (own composer.json, no lock committed)
tests/ # Two PHPUnit suites
├── TestCase.php # Base: extends Orchestra Testbench, no DB
├── TestCaseDatabase.php # Base: adds SQLite in-memory DB + migrations
├── Unit/ # Unit tests (no database)
├── Database/ # Database tests (SQLite in-memory)
└── Support/ # Shared fixtures: Models/, Objects/, Queries/, Types/, Traits/
config/config.php # Publishable Laravel config (schemas, types, middleware, security, tracing)
Architecture
Class Hierarchy
Field (abstract) # Core: authorize(), rules(), args(), type(), resolve()
├── Query # Semantic alias (empty, extends Field)
└── Mutation # Semantic alias (empty, extends Field)
Type (abstract) # Base for all GraphQL types: fields(), attributes(), toType()
├── InputType # → InputObjectType
├── EnumType # → EnumType
├── InterfaceType # → InterfaceType (adds resolveType/types)
├── UnionType # → UnionType (abstract types())
└── UploadType # → ScalarType (file uploads)
Two Middleware Layers
Execution Middleware — wraps the full GraphQL execution pipeline:
- Base:
AbstractExecutionMiddlewareinsrc/Support/ExecutionMiddleware/ - Signature:
handle(string $schemaName, Schema $schema, OperationParams $params, $rootValue, $contextValue, Closure $next): ExecutionResult - Built-in:
ValidateOperationParamsMiddleware,AutomaticPersistedQueriesMiddleware,AddAuthUserContextValueMiddleware,UnusedVariablesMiddleware,GraphqlExecutionMiddleware(terminal, always last) - Configured via
graphql.execution_middlewareconfig or per-schema
Resolver Middleware — wraps individual field resolvers:
- Base:
Middlewareinsrc/Support/Middleware.php - Signature:
handle($root, array $args, $context, ResolveInfo $info, Closure $next) - Per-field:
$middlewareproperty onField/Query/Mutation - Global:
graphql.resolver_middleware_appendconfig
Key Classes
GraphQL(src/GraphQL.php): Singleton, usesMacroable. Type registry with modifier parsing (GraphQL::type('[User!]!')). Schema building, query execution, error formatting.Privacy(src/Support/Privacy.php): Abstract base for field-level access control.ConfigConvertible(src/Support/Contracts/ConfigConvertible.php): Interface for class-based schema definitions (returns array withquery,mutation,types,middleware, etc.).ResolverParameterInjector(src/Support/Contracts/ResolverParameterInjector.php): Interface for extensible resolver DI. External packages register injectors viaField::registerParameterInjector().
Resolver Conventions
Field::resolve()supports dependency injection:ResolveInfo, or any container-resolvable class. External packages (e.g.rebing/graphql-laravel-select-fields) can register custom injectors viaField::registerParameterInjector().- Types auto-discover resolvers via
resolve{StudlyFieldName}Field()methods on the Type class - Authorization: override
authorize()onField/Query/Mutation - Validation: override
rules()to return Laravel validation rules for args
Test Conventions
Base Classes
TestCase(tests/TestCase.php): Extends Orchestra Testbench. Provides$this->queriesand$this->datafixtures,httpGraphql()helper, schema assertion methods. Pre-configuresdefault,custom, andclass_basedschemas.TestCaseDatabase(tests/TestCaseDatabase.php): ExtendsTestCase. Adds SQLite in-memory DB, runs migrations on each test. Use for any test needing Eloquent models.
Writing Tests
- Namespace:
Rebing\GraphQL\Tests\UnitorRebing\GraphQL\Tests\Database - Use
self::assert*()(static calls), not$this->assert*() - Use PHPUnit attributes:
#[DoesNotPerformAssertions],#[DataProvider('...')] - Override
getEnvironmentSetUp($app)to register test-specific schemas, types, and config (callparent::getEnvironmentSetUp($app)first in Database tests) - Co-locate test-specific support classes (queries, mutations, types) in the same directory as the test
- Shared fixtures go in
tests/Support/Objects/,tests/Support/Queries/,tests/Support/Types/
Test Helpers
httpGraphql(string $query, array $options): Dispatches POST to/graphql. Options:expectErrors(bool),httpStatusCode(int),variables(array),schemaName(string). Auto-strips trace/file/line from errors.SqlAssertionTrait: Records all SQL queries. UseassertSqlCount(int)andassertSqlQueries(string). Available inTestCaseDatabasesubclasses.MakeCommandAssertionTrait: Verifies artisan make command output (file path, namespace, class, graphql name).runCommand(Command $command, array $arguments): Wraps Artisan command testing withCommandTester.
Models and Factories
Database tests use 4 Eloquent models in tests/Support/Models/: User, Post, Comment, Like with corresponding factories and migrations in tests/Support/database/.
Error Handling
Two distinct layers:
- Errors (
src/Error/): ExtendGraphQL\Error\Error, implementProvidesErrorCategory. These are client-safe and returned in GraphQL responses. Classes:ValidationError(wraps Laravel Validator),AuthorizationError,AutomaticPersistedQueriesError. - Exceptions (
src/Exception/): ExtendRuntimeException. These indicate configuration/developer errors and are NOT client-safe. Classes:SchemaNotFound,TypeNotFound.
Artisan Make Commands
All generators follow make:graphql:{type} {name} pattern with stubs in src/Console/stubs/:
| Command | Default Namespace |
|---|---|
make:graphql:type |
App\GraphQL\Types |
make:graphql:query |
App\GraphQL\Queries |
make:graphql:mutation |
App\GraphQL\Mutations |
make:graphql:enum |
App\GraphQL\Enums |
make:graphql:input |
App\GraphQL\Inputs |
make:graphql:interface |
App\GraphQL\Interfaces |
make:graphql:union |
App\GraphQL\Unions |
make:graphql:scalar |
App\GraphQL\Scalars |
make:graphql:field |
App\GraphQL\Fields |
make:graphql:middleware |
App\GraphQL\Middleware |
make:graphql:executionMiddleware |
App\GraphQL\Middleware\Execution |
make:graphql:schemaConfig |
App\GraphQL\Schemas |