Skip to content

juffalow/slim-graphql-eloquent-example

Repository files navigation

Slim, GraphQL and Eloquent example

License codecov

How to run the project

Install dependencies :

composer install

Create database for the project by importing eloquent_example.sql and update the config/config.php:

<?php

return [
  'settings' => [
    '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 :

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:

  ...
  'log' => [
    'file' => [
      'level' => 100,
      'file' => 'path/to/file',
    ]
  ],
  ...

Debug

GraphQL debug modes:

  • INCLUDE_DEBUG_MESSAGE (1)
  • INCLUDE_TRACE(2)
  • RETHROW_INTERNAL_EXCEPTIONS (4)
  ...
  'graphql' => [
    'debug' => INCLUDE_DEBUG_MESSAGE | INCLUDE_TRACE,
  ],
  ...

Security

Limiting Query Depth:

  ...
  'graphql' => [
    'maxDepth' => 10,
  ],
  ...

Introspection:

  ...
  'graphql' => [
    'introspection' => true,
  ],
  ...

Examples

Queries

Get list of authors

query {
  authors {
    edges {
      node {
        id
        _id
        firstName
        lastName
      }
    }
  }
}

Filter authors by first name

query {
  authors(firstName: "John") {
    edges {
      node {
        id
        _id
        firstName
        lastName
      }
    }
  }
}

Order authors by first name and last name:

query {
  authors(orderBy:[
    {
      field:FIRST_NAME
      direction:ASC
    }
    {
      field:LAST_NAME
      direction:ASC
    }
  ]) {
    edges {
      cursor
      node {
        _id
        firstName
        lastName
      }
    }
  }
}

Mutations

Create new author

mutation {
  createAuthor(input:{firstName:"Fredrick", lastName:"Brooks"}) {
    id
    _id
    firstName
    lastName
  }
}

Tests

Run PHPUnit:

./vendor/bin/phpunit

# or

./vendor/bin/phpunit --bootstrap ./vendor/autoload.php --testdox tests

Use PHP Stan to analyse php files for basic errors:

./vendor/bin/phpstan analyse src

License

MIT license