- How do I install and set up rebing/graphql-laravel in a Laravel project?
- Run `composer require rebing/graphql-laravel` and publish the config with `php artisan vendor:publish --provider="Rebing\GraphQL\GraphQLServiceProvider"`. Then register the service provider in `config/app.php` and define your schema in a PHP class extending `Rebing\GraphQL\GraphQL`. The package includes Artisan commands like `make:graphql:query` to scaffold components.
- Does rebing/graphql-laravel support Laravel’s Eloquent ORM, and how can I optimize queries to avoid N+1 problems?
- Yes, it fully supports Eloquent. Use the optional `rebing/graphql-laravel-select-fields` package to auto-generate optimized `select()` and `with()` calls based on the GraphQL query. Alternatively, leverage Dataloaders for batching external API calls or complex resolvers. Both methods prevent N+1 queries efficiently.
- Can I use multiple GraphQL schemas in the same Laravel application, and how do I route requests to the correct schema?
- Absolutely. Define multiple schemas by extending `Rebing\GraphQL\GraphQL` and register them in the config. Use middleware like `graphql.execution_middleware` to dynamically select schemas based on request attributes (e.g., tenant ID or API version). This is ideal for multi-tenant or modular applications.
- How does rebing/graphql-laravel handle authentication and authorization for GraphQL queries?
- It integrates seamlessly with Laravel’s built-in auth system. Use Laravel’s middleware (e.g., `auth:api`) to protect routes, and leverage resolver middleware or the `authorize()` method in types/queries to enforce field-level permissions. For example, you can check policies or gates directly in your PHP-defined resolvers.
- What Laravel versions are supported by rebing/graphql-laravel, and how do I check compatibility?
- The package supports Laravel 8.x, 9.x, and 10.x. Check the [Packagist page](https://packagist.org/packages/rebing/graphql-laravel) for the latest version’s requirements. The `composer.json` of the package also lists supported Laravel versions. Always test in a staging environment before deploying to production.
- How can I test GraphQL endpoints in Laravel using PHPUnit or Pest?
- Use Laravel’s HTTP testing helpers (e.g., `Http::post()`) to send GraphQL queries to your endpoint (typically `/graphql`). The package works with Testbench, so you can mock dependencies like Eloquent models or services. Example: `$response = Http::post('/graphql', ['query' => '{ user { name } }']); $response->assertOk()->assertJson(['data' => [...]]).`
- Are there performance best practices for production deployments with rebing/graphql-laravel?
- Enable `AutomaticPersistedQueries` to cache queries and reduce parsing overhead. Use `SelectFields` for Eloquent queries and Dataloaders for external data sources. Disable introspection in production (`GRAPHQL_DISABLE_INTROSPECTION=true`) and set query depth/complexity limits to prevent abuse. Monitor performance with OpenTelemetry tracing.
- How do I handle schema evolution, such as deprecating fields or making breaking changes?
- Use the `@deprecated` directive in your PHP-defined types to mark fields as deprecated. For breaking changes, leverage Laravel’s package versioning and communicate updates via changelogs. The package supports backward-compatibility checks via `roave/backward-compatibility-check` to catch issues early.
- Does rebing/graphql-laravel support GraphQL subscriptions, and if not, what are the alternatives?
- No, subscriptions are not supported. For real-time features, consider using a dedicated solution like [Lighthouse](https://lighthouse-php.com/), which includes subscription support, or integrate a third-party service like Pusher or Laravel Echo with a custom GraphQL implementation.
- What are the alternatives to rebing/graphql-laravel for Laravel GraphQL, and how does this package compare?
- Alternatives include [Lighthouse](https://lighthouse-php.com/) (schema-first, supports subscriptions) and [GraphQL for Laravel](https://github.com/FolkloreLabs/graphql-laravel) (simpler but less feature-rich). rebing/graphql-laravel stands out for its code-first approach, middleware flexibility, and deep Laravel integration (e.g., Eloquent, validation, and testing). It’s ideal for teams needing fine-grained control over schema logic.