- How do I integrate webonyx/graphql-php into a Laravel application?
- Install via Composer (`composer require webonyx/graphql-php`), then use Laravel-specific packages like `graphql-laravel` or `overblog/graphql-bundle` for routing, middleware, and schema management. These packages bridge GraphQL with Laravel’s service container and middleware stack.
- Does this package support Laravel’s Eloquent ORM for database queries?
- Yes, you can use Eloquent models in resolvers by injecting the database connection or repository services. For batching queries (to avoid N+1 issues), implement `DataFetcherInterface` or use `graphql-php-extensions` for DataLoader integration.
- Can I use GraphQL with Laravel’s API Resources for serialization?
- Not directly, but you can transform Eloquent models into GraphQL types by mapping fields in resolvers. For complex cases, use Laravel’s `ApiResource` logic inside resolvers or leverage `graphql-laravel`’s built-in support for Eloquent.
- What Laravel versions are compatible with webonyx/graphql-php?
- The package itself is framework-agnostic, but Laravel integrations (e.g., `graphql-laravel`) typically support Laravel 8.x and 9.x. Check the specific package’s docs for version constraints, as they may vary.
- How do I handle authentication in GraphQL queries for Laravel?
- Use Laravel’s built-in auth system by passing the `Auth` facade or `Request` object in the GraphQL execution context. For field-level permissions, create custom directives (e.g., `@auth`) that check Laravel’s `Gate` or `Policy` system.
- Is GraphQL introspection safe to enable in production?
- No, disable introspection in production to avoid exposing schema details. Use environment variables or middleware to toggle it. For development, enable it via `GraphQLServer::addType(new IntrospectionType())` or config files.
- How do I test GraphQL queries in Laravel’s PHPUnit?
- Use Laravel’s `Http` facade with `GraphQL::query()` or mock the schema and execution context. For complex tests, leverage `graphql-laravel`’s testing helpers or create custom test cases that validate resolver outputs against expected GraphQL responses.
- What’s the best way to optimize resolver performance in Laravel?
- Use async resolvers with `ReactPHP` or `Amp` for non-blocking I/O, and implement batching via `DataLoader` (from `graphql-php-extensions`) to reduce database queries. Avoid synchronous DB calls in resolvers for high-traffic endpoints.
- Can I use GraphQL subscriptions with Laravel and webonyx/graphql-php?
- Yes, but you’ll need additional tooling like `graphql-php-extensions` for WebSocket support. Integrate with Laravel’s `Broadcast` system or use third-party packages like `laravel-websockets` to handle real-time subscriptions.
- What are the alternatives to webonyx/graphql-php for Laravel?
- Consider `overblog/graphql-bundle` (Symfony/Laravel bundle) or `graphql-php/graphql-http` for HTTP server integration. For simpler needs, Laravel’s `ApiResource` with REST may suffice, but GraphQL offers better flexibility for client-driven queries.