Bonus routes in Statamic provide a way to easily set up additional collection and taxonomy-based routes while handling dynamic mounting and data retrieval without the need for manually hardcoding URLs. These routes are defined via a `Route::bonus()` method and live in the Laravel `routes/web.php` file. Bonus routes can be collection show routes (targeting individual entries) or index routes for listing or non-entry-specific pages. Similarly, taxonomy routes can also be show or index types with specific slug parameters required for show routes.
Statamic's GraphQL support, on the other hand, offers a read-only API for delivering content from the CMS to frontends, external applications, or SPAs. Enabling GraphQL is done through configuration in `.env` or the `config/statamic/graphql.php` by enabling specific content resources such as collections and taxonomies. GraphQL access is structured via interfaces (e.g., `EntryInterface` for entries) and their blueprint-specific implementations with detailed fields exposed. This lets developers query content deeply and selectively.
Regarding interaction, bonus routes and GraphQL serve different but complementary roles in a Statamic site. Bonus routes enhance how URLs and pages are generated and accessed dynamically in the Laravel routing layer, tailored strongly towards the CMS frontend or any Laravel-rendered views. GraphQL exposes the same content data through an API, enabling applications to fetch structured content programmatically, independent of URLs or frontend route structure.
Bonus routes do not modify or interfere with Statamic's own routing layer but add Laravel routes that take precedence when matching URIs. This can override standard Statamic routes if the same URI is used, though it is advisable to avoid this. In contrast, GraphQL queries operate on the content data model level and are abstracted from URL routing concerns.
Both features integrate tightly yet distinctly within Statamic's architecture:
- Bonus Routes allow flexible and additional URL schemes on collections and taxonomies for frontend page rendering, including custom views and extra data injection.
- GraphQL offers a comprehensive, secure content API, where enabled resources reflect the same underlying content structures that the bonus routes might expose via URLs, but in a fully decoupled manner for programmatic access.
For developers, using bonus routes with GraphQL means you can:
- Provide rich, custom URL structures and pages for frontend users.
- Enable client-side or third-party apps to query the same content via GraphQL APIs.
- Keep routing and API concerns separated, each optimized for their use case.
- Benefit from Laravel's native route caching and naming, along with Statamic's fine-grained GraphQL resource control.