- How do I define a GraphQL schema in Laravel using this package without writing `.graphql` files?
- Define your schema entirely in PHP classes. Create `Type`, `Query`, and `Mutation` classes extending the package’s base classes (e.g., `Type`, `ObjectType`). For example, a `UserType` would extend `ObjectType` and define fields like `id()` and `name()`. The package auto-discovers these classes and builds the schema at runtime.
- Can I use this package with Laravel 13? Does it support PHP 8.2+?
- Yes, **rebing/graphql-laravel** explicitly supports Laravel 12.x and 13.x with PHP 8.2+. The package leverages Laravel’s service container and Artisan commands, ensuring compatibility with the latest Laravel features. Check the [GitHub releases](https://github.com/rebing/graphql-laravel/releases) for version-specific notes.
- How do I prevent N+1 queries when fetching Eloquent relationships in GraphQL?
- Use **SelectFields** for optimized `select()` and `with()` clauses based on GraphQL field selections, or **Dataloaders** for batching database queries. Both strategies are built into the package. For example, `SelectFields` automatically generates efficient Eloquent queries, while Dataloaders defer resolution until all fields are requested, reducing redundant queries.
- Is it possible to have multiple GraphQL schemas in a single Laravel app? For example, one for admin and one for public users?
- Yes, the package supports **multiple schemas** with independent queries, mutations, types, and middleware. Configure each schema in its own service provider or use the `graphql:schemaConfig` Artisan command to generate schema-specific configurations. This is ideal for multi-tenancy, feature flags, or microservices.
- How do I add authentication or authorization to specific GraphQL queries or mutations?
- Use **resolver middleware** or **execution middleware** for per-field or global authorization. For example, add `authorize()` methods to your `Query` or `Mutation` classes, or use middleware like `AddAuthUserContextValueMiddleware` to inject the authenticated user into the GraphQL context. You can also apply Laravel middleware to GraphQL routes.
- Does this package support persisted queries for performance optimization?
- Yes, you can enable **persisted queries** via the `AutomaticPersistedQueriesMiddleware`. This reduces payload size and parsing overhead by caching and reusing query hashes. Configure it in your schema’s execution middleware pipeline to validate and resolve persisted queries automatically.
- How do I test GraphQL endpoints in Laravel using this package?
- The package includes built-in test helpers like `TestCase` and `TestCaseDatabase` for unit and integration tests. Use Laravel’s HTTP testing tools to send GraphQL requests (e.g., `Http::post('/graphql', ['query' => '...'])`). Fixtures like `User` and `Post` models are provided for quick setup, and you can mock dependencies with Laravel’s testing utilities.
- Can I integrate third-party GraphQL tools like Apollo Client or Relay with this package?
- Absolutely. This package generates a standard GraphQL endpoint (`/graphql`) that works with any GraphQL client, including Apollo Client, Relay, or GraphQL Playground. Ensure your client sends requests to the correct endpoint (e.g., `POST /graphql` with a `query` field) and handles responses in the GraphQL format.
- What are the alternatives to this package for Laravel GraphQL, and how does it compare?
- Alternatives include **Lighthouse** (supports subscriptions and SDL-first schemas) and **GraphQL for Laravel** (simpler but less feature-rich). **rebing/graphql-laravel** stands out for its **code-first approach**, **multiple schema support**, and **performance optimizations** like SelectFields and Dataloaders. It’s ideal for developers who prefer PHP classes over `.graphql` files and need fine-grained control over middleware and data loading.
- How do I debug performance issues in my GraphQL queries, especially with complex nested relationships?
- Use **OpenTelemetry** integration to trace GraphQL operations per field or schema. Enable it via configuration and inspect traces in tools like Jaeger or Laravel’s built-in debugging. For Eloquent queries, enable Laravel’s query logging (`DB::enableQueryLog()`) and analyze the generated SQL. The package also provides tools like `SelectFields` to optimize queries automatically.