Skip to content

Commit 03bc77d

Browse files
committed
Drafting something
1 parent a22f2ed commit 03bc77d

File tree

13 files changed

+261
-27
lines changed

13 files changed

+261
-27
lines changed

bin/server.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdsWarehouse;
4+
5+
use AdsWarehouse\Http\Handler;
6+
use function Siler\GraphQL\schema;
7+
use function Siler\Swoole\http;
8+
9+
$basedir = dirname(__DIR__, 1);
10+
require_once "$basedir/vendor/autoload.php";
11+
12+
$type_defs = graphql_files("$basedir/src/");
13+
$resolvers = require_once "$basedir/src/resolvers.php";
14+
$schema = schema($type_defs, $resolvers);
15+
16+
$context = new Context();
17+
$context->schema = $schema;
18+
19+
http(new Handler($context), 8000)->start();

composer.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"description": "Data warehouse for multi-data-source ads campaings.",
44
"type": "project",
55
"require": {
6-
"leocavalcante/siler": "^1.6",
6+
"leocavalcante/siler": "dev-master",
77
"monolog/monolog": "^2.0",
8-
"firebase/php-jwt": "^5.0"
8+
"firebase/php-jwt": "^5.0",
9+
"webonyx/graphql-php": "^0.13.8"
910
},
1011
"require-dev": {
1112
"swoole/ide-helper": "^4.4",
@@ -18,5 +19,16 @@
1819
"email": "[email protected]"
1920
}
2021
],
21-
"minimum-stability": "stable"
22+
"minimum-stability": "stable",
23+
"autoload": {
24+
"psr-4": {
25+
"AdsWarehouse\\": [
26+
"src/",
27+
"lib/"
28+
]
29+
},
30+
"files": [
31+
"lib/graphql_files.php"
32+
]
33+
}
2234
}

composer.lock

Lines changed: 79 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
services:
3+
ads_warehouse:
4+
container_name: ads_warehouse
5+
image: leocavalcante/dwoole:dev
6+
ports:
7+
- 8000:8000
8+
volumes:
9+
- ./:/app
10+
environment:
11+
ENTRY_POINT_FILE: /app/bin/server.php

lib/graphql_files.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdsWarehouse;
4+
5+
use RecursiveDirectoryIterator;
6+
use RecursiveFilterIterator;
7+
use RecursiveIteratorIterator;
8+
use SplFileInfo;
9+
10+
function graphql_files(string $basedir): string
11+
{
12+
$dir_iter = new RecursiveDirectoryIterator($basedir);
13+
14+
$filter_iter = new class ($dir_iter) extends RecursiveFilterIterator
15+
{
16+
public function accept()
17+
{
18+
return preg_match('/\.graphql$/', $this->current()->getFilename());
19+
}
20+
};
21+
22+
$iter = new RecursiveIteratorIterator($filter_iter);
23+
24+
return array_reduce(iterator_to_array($iter), function (string $schema, SplFileInfo $fileInfo): string {
25+
return "$schema\n" . file_get_contents($fileInfo->getFilename());
26+
}, "");
27+
}

src/Ad/Ad.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Ad {
2+
id: ID!
3+
name: String!
4+
}

src/Ad/Ad.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdsWarehouse\Ad;
4+
5+
class Ad
6+
{
7+
/** @var string */
8+
public $id;
9+
/** @var string */
10+
public $name;
11+
}

src/Ad/V1__Ad.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create table ad
2+
(
3+
id uuid not null primary key,
4+
name varchar not null,
5+
timestamp timestamp not null default current_timestamp
6+
);

src/Context.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdsWarehouse;
4+
5+
use GraphQL\Type\Schema;
6+
7+
class Context
8+
{
9+
/** @var Schema */
10+
public $schema;
11+
/** @var mixed */
12+
public $rootValue;
13+
}

src/Http/Handler.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace AdsWarehouse\Http;
4+
5+
use AdsWarehouse\Context;
6+
use GraphQL\Error\FormattedError;
7+
use Siler\Monolog as Log;
8+
use Swoole\Http\Request;
9+
use Swoole\Http\Response;
10+
use Throwable;
11+
use function Siler\Encoder\Json\decode;
12+
use function Siler\GraphQL\execute;
13+
use function Siler\Swoole\{cors, json, raw};
14+
15+
class Handler
16+
{
17+
/** @var Context */
18+
private $context;
19+
20+
public function __construct(Context $context)
21+
{
22+
$this->context = $context;
23+
}
24+
25+
public function __invoke(Request $request, Response $response)
26+
{
27+
try {
28+
$input = decode(raw());
29+
$response = execute($this->context->schema, $input, $this->context->rootValue, $this->context);
30+
} catch (Throwable $exception) {
31+
Log\error('Internal error', ['exception' => $exception]);
32+
$response = FormattedError::createFromException($exception);
33+
} finally {
34+
cors();
35+
json($response);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)