An example implementation of an auto-generated GraphQL CRUD API with Laravel.
Included with this example:
Run the following script in the root of this project:
./install.sh
Alternatively, you can run the following commands yourself:
composer install
cp -r .env.example .env
Update the DB_*
values in the .env
file to the database you want to use,
then run the migrations and seeds using the following command:
php artisan migrate --seed
When that's finished run php artisan serve
and go to the URL provided in your console.
(Usually localhost:8000)
In order to use the GraphQL interface, you need to generate the schema.
The schema generator reads the *.graphql
files in the app/Http/GraphQL/Types
folder.
the Schema is generated by running following command:
php artisan lighthouse-utils:generate-schema
Note: Every time you modify the GraphQL types, the schema has to be re-generated.
Now that everything is up and running, you can try out the GraphQL API by navigating to: http://localhost:8000/graphql-playground
On the right you will see a button Schema
, you can inspect all the available queries and mutations there.
Some example queries:
{
articles {
title
author{
name
}
}
}
{
articles(title_contains: "turtle") {
title
}
}
{
user(id: 1) {
name
comments {
article {
title
}
}
}
}