https://github.com/juffalow/slim-graphql-eloquent-example
Example project how to use Slim, GraphQL and Eloquent
https://github.com/juffalow/slim-graphql-eloquent-example
eloquent example-project graphql php phpstan phpunit slim
Last synced: 7 months ago
JSON representation
Example project how to use Slim, GraphQL and Eloquent
- Host: GitHub
- URL: https://github.com/juffalow/slim-graphql-eloquent-example
- Owner: juffalow
- License: mit
- Created: 2018-08-24T19:02:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-01-08T23:46:21.000Z (11 months ago)
- Last Synced: 2025-03-30T22:05:45.977Z (9 months ago)
- Topics: eloquent, example-project, graphql, php, phpstan, phpunit, slim
- Language: PHP
- Homepage: https://graphql.juffalow.com/
- Size: 163 KB
- Stars: 24
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Slim, GraphQL and Eloquent example
[](https://github.com/juffalow/slim-graphql-eloquent-example/blob/master/LICENSE)
[](https://codecov.io/gh/juffalow/slim-graphql-eloquent-example)
## How to run the project
Install dependencies :
```shell
composer install
```
Create database for the project by importing *eloquent_example.sql* and update the *config/config.php*:
```php
[
'displayErrorDetails' => true,
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'eloquent_example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
]
],
];
```
Run the project :
```shell
php -S localhost:8088
```
## Logging
There are multiple ways how to setup logging. The application now supports
rotating file logging and slack logging.
Logging levels:
* DEBUG (100)
* INFO (200)
* NOTICE(250)
* WARNING (300)
* ERROR (400)
* CRITICAL (500)
* ALERT(550)
* EMERGENCY (600)
Rotating file:
```php
...
'log' => [
'file' => [
'level' => 100,
'file' => 'path/to/file',
]
],
...
```
## Debug
GraphQL debug modes:
* INCLUDE_DEBUG_MESSAGE (1)
* INCLUDE_TRACE(2)
* RETHROW_INTERNAL_EXCEPTIONS (4)
```php
...
'graphql' => [
'debug' => INCLUDE_DEBUG_MESSAGE | INCLUDE_TRACE,
],
...
```
## Security
[Limiting Query Depth](https://webonyx.github.io/graphql-php/security/#limiting-query-depth):
```php
...
'graphql' => [
'maxDepth' => 10,
],
...
```
[Introspection](https://webonyx.github.io/graphql-php/security/#disabling-introspection):
```php
...
'graphql' => [
'introspection' => true,
],
...
```
## Examples
### Queries
#### Get list of authors
```graphql
query {
authors {
edges {
node {
id
_id
firstName
lastName
}
}
}
}
```
#### Filter authors by first name
```graphql
query {
authors(firstName: "John") {
edges {
node {
id
_id
firstName
lastName
}
}
}
}
```
#### Order authors by first name and last name:
```graphql
query {
authors(orderBy:[
{
field:FIRST_NAME
direction:ASC
}
{
field:LAST_NAME
direction:ASC
}
]) {
edges {
cursor
node {
_id
firstName
lastName
}
}
}
}
```
### Mutations
#### Create new author
```graphql
mutation {
createAuthor(input:{firstName:"Fredrick", lastName:"Brooks"}) {
id
_id
firstName
lastName
}
}
```
## Tests
Run PHPUnit:
```shell
./vendor/bin/phpunit
# or
./vendor/bin/phpunit --bootstrap ./vendor/autoload.php --testdox tests
```
Use PHP Stan to analyse php files for basic errors:
```shell
./vendor/bin/phpstan analyse src
```
## License
[MIT license](./LICENSE)