Laravel wrapper for Facebook's GraphQL
This package provides a code-first integration of GraphQL for Laravel. It is based on the PHP port of GraphQL reference implementation. You define your schema entirely in PHP classes (types, queries, mutations) rather than in .graphql schema files. You can find more information about GraphQL in the Introduction to GraphQL or you can read the GraphQL specifications.
select() and eager-loaded with() callsNote: GraphQL subscriptions are not supported by this package. If you need real-time push functionality, consider a dedicated solution like Lighthouse (which has subscription support) or implement subscriptions separately via Laravel broadcasting / WebSockets.
| Dependency | Version |
|---|---|
| PHP | ^8.2 |
| Laravel | 12.x - 13.x |
| webonyx/graphql-php | ^15.22.1 |
Optional dependencies:
| Package | Purpose |
|---|---|
open-telemetry/api ^1.0 |
Required for the OpenTelemetry tracing driver |
mll-lab/laravel-graphiql |
Interactive in-browser GraphiQL IDE |
Require the package via Composer:
composer require rebing/graphql-laravelPublish the configuration file via Laravel artisan:
php artisan vendor:publish --provider="Rebing\GraphQL\GraphQLServiceProvider"Review the configuration file:
config/graphql.phpGet a working GraphQL endpoint in under 5 minutes -- no database required.
Use the artisan generator to scaffold a type:
php artisan make:graphql:type BookTypeEdit the generated app/GraphQL/Types/BookType.php:
…php artisan make:graphql:query BooksQueryEdit app/GraphQL/Queries/BooksQuery.php:
…Add the type and query to the default schema in config/graphql.php:
'schemas' => [
'default' => [
'query' => [
App\GraphQL\Queries\BooksQuery::class,
],
'mutation' => [],
'types' => [
App\GraphQL\Types\BookType::class,
],
],
],Start the dev server and send a query:
php artisan servecurl -X POST -H "Content-Type: application/json" \
-d '{"query": "{ books { id title author } }"}' \
http://localhost:8000/graphqlExpected response:
{
"data": {
"books": [
{"id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
{"id": 2, "title": "1984", "author": "George Orwell"},
{"id": 3, "title": "To Kill a Mockingbird", "author": "Harper Lee"}
]
}
}Try filtering with an argument:
curl -X POST -H "Content-Type: application/json" \
-d '{"query": "{ books(title: \"1984\") { id title } }"}' \
http://localhost:8000/graphqlTip: For an interactive experience, install GraphiQL (
composer require mll-lab/laravel-graphiql --dev) and visit/graphiqlin your browser.
Note: Introspection is disabled by default. To enable it during development (required for GraphiQL and IDE tooling), set
GRAPHQL_DISABLE_INTROSPECTION=falsein your.envfile.
You now have a working GraphQL API. From here you can:
rebing/graphql-laravel-select-fields packagephp artisan list make:graphql to see all 12 available scaffolding commandsBefore diving head first into code, it's good to familiarize yourself with the concepts surrounding GraphQL. If you've already experience with GraphQL, feel free to skip this part.
Typically, all queries/mutations/types are defined using the $attributes
property and the args() / fields() methods as well as the resolve() method.
args/fields again return a configuration array for each field they supported. Those fields usually support these shapes
type (required): a GraphQL specifier for the type supported hereOptional keys are:
description: made available when introspecting the GraphQL schemaresolve: override the default field resolverdeprecationReason: document why something is deprecatednonNullIt's quite common, and actually good practice, to see the gracious use of
Type::nonNull() on any kind of input and/or output fields.
The more specific the intent of your type system, the better for the consumer.
Some examples
No open issues yet, or sync has not completed.