Skip to content

Instantly share code, notes, and snippets.

@SerafimArts
Created August 18, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerafimArts/c0096432ad8d1cad004c5c86c2352f3b to your computer and use it in GitHub Desktop.
Save SerafimArts/c0096432ad8d1cad004c5c86c2352f3b to your computer and use it in GitHub Desktop.
Railgun example app
#
# file ~/schema/index.graphqls
#
schema {
query: Query
}
type Query {
user: User
}
type User {
id: ID!
login: String!
createdAt: String
updatedAt: String
}
<?php
/**
* This file is part of Railgun package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Railgun\Endpoint;
use Railgun\Http\Request;
use Railgun\Routing\Router;
use Railgun\Adapters\RequestInterface;
/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so we do not have to manually load any of
| our application's PHP classes. It just feels great to relax.
|
*/
require __DIR__ . '/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Create The Endpoint
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Railgun endpoint instance
| which serves as the "glue" for all the components of your application.
|
*/
$endpoint = Endpoint::fromFilePath(__DIR__ . '/schema/index.graphqls');
/*
|--------------------------------------------------------------------------
| Register The GraphQL Auto Loader
|--------------------------------------------------------------------------
|
| Railgun provides a convenient, automatically GraphQL Types loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our GraphQL Types later on. It feels great to relax.
|
*/
$endpoint->autoload()->dir(__DIR__ . '/schema');
/*
|--------------------------------------------------------------------------
| Create The Logger
|--------------------------------------------------------------------------
|
| Let's just use the Monolog library and see what requests are made in
| our greatest application.
|
*/
$logger = new Logger('Railgun', [new StreamHandler('php://stdout')]);
$endpoint->withLogger($logger);
// On all request
$endpoint->on('request:*', function (RequestInterface $request) use ($endpoint) {
$endpoint->debugMessage('Request => ' . $request->getPath());
});
// On responses
$endpoint->on('response:*', function ($response, RequestInterface $request) use ($endpoint) {
$endpoint->debugMessage('<= Response ' . $request->getPath());
$endpoint->debugMessage('<= Body ' . json_encode($response));
});
/*
|--------------------------------------------------------------------------
| GraphQL Routes
|--------------------------------------------------------------------------
|
| Here is where you can register api routes for your application.
| Now create something great!
|
*/
$endpoint->router(function (Router $router) {
$router->group('user', function (Router $router) {
//
// Root query ("query some { user { ... } }")
//
$router->add('.', function () {
return [
'id' => random_int(PHP_INT_MIN, PHP_INT_MAX),
'login' => \Illuminate\Support\Str::random(24),
];
});
$router->add('{timeField}', function (RequestInterface $request) {
return $request->getPath() . ' => ' . (new DateTime())->format(DateTime::RFC3339);
})->where('timeField', 'createdAt|updatedAt');
});
});
/*
|--------------------------------------------------------------------------
| Run The Endpoint
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$response = $endpoint->request(Request::create());
$response->send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment