Skip to content

Commit 1edcd0b

Browse files
committed
First working version
1 parent 68d63e6 commit 1edcd0b

6 files changed

Lines changed: 245 additions & 1 deletion

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.lock
2+
phpunit
3+
.phpunit*
4+
phpunit.xml
5+
vendor/

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Unofficial EdgeDB PHP client
2+
3+
Not - yet - ready for production as it needs review from the community.
4+
5+
## Quickstart
6+
7+
Add this package to your project:
8+
9+
```bash
10+
$ composer require teddybear06/edgedb-php
11+
```
12+
13+
Require Composer's autoloader (if it's not already done):
14+
15+
```php
16+
require 'vendor/autoload.php';
17+
```
18+
19+
Require two needed classes:
20+
21+
```php
22+
use TeddyBear06\EdgeDbPhp\EdgeDbClient;
23+
use TeddyBear06\EdgeDbPhp\EdgeDbQuery;
24+
```
25+
26+
Create an new EdgeDbClient with your EdgeDB server configuration:
27+
28+
```php
29+
$edgeDbClient = new EdgeDbClient('127.0.0.1', '10700', 'edgedb');
30+
```
31+
32+
Create an EdgeDbQuery:
33+
34+
```php
35+
$edgeDbQuery = new EdgeDbQuery('select Author {firstname} filter .firstname = <str>$firstname;', ['firstname' => 'John']);
36+
```
37+
38+
Get the response:
39+
40+
```php
41+
$edgeDbResponse = $edgeDbClient->send($edgeDbQuery);
42+
```
43+
44+
Use the response with your own logic:
45+
46+
```php
47+
if (isset($edgeDbResponse['data'])) {
48+
echo $edgeDbResponse['data'][0]['firstname'];
49+
} else {
50+
echo 'No matches...';
51+
}
52+
```
53+
54+
## Debug
55+
56+
If needed, you can set an extra parameter to the EdgeDbClient constructor like so:
57+
58+
```php
59+
$edgeDbClient = new EdgeDbClient('127.0.0.1', '10700', 'edgedb', true);
60+
```
61+
62+
You will get an extra response index 'debug' and it will activate Guzzle debug mode:
63+
64+
```php
65+
var_dump($edgeDbResponse['debug']);
66+
```

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"edgedb"
77
],
88
"require": {
9-
"php": ">=8.0"
9+
"php": ">=8.0",
10+
"guzzlehttp/guzzle": "^7.0"
11+
},
12+
"require-dev": {
13+
"phpunit/phpunit": "^9"
1014
},
1115
"autoload": {
1216
"psr-4": {

src/EdgeDbClient.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TeddyBear06\EdgeDbPhp;
4+
5+
use TeddyBear06\EdgeDbPhp\EdgeDbQuery;
6+
use GuzzleHttp\Client;
7+
8+
/**
9+
* An unofficial naive EdgeDB PHP client using EdgeDB HTTP protocol.
10+
*
11+
* See https://www.edgedb.com/docs/clients/90_edgeql/index for more informations about
12+
* how to activate it on your EdgeDB instance.
13+
*/
14+
class EdgeDbClient {
15+
16+
/**
17+
* EdgeDB hostname (or IP).
18+
*
19+
* @var string
20+
*/
21+
private string $hostname;
22+
23+
/**
24+
* EdgeDB port.
25+
*
26+
* @var integer
27+
*/
28+
private int $port;
29+
30+
/**
31+
* EdgeDB database name.
32+
*
33+
* @var string
34+
*/
35+
private string $database;
36+
37+
/**
38+
* Indicates whether or not the client should display debug infos.
39+
*
40+
* @var bool
41+
*/
42+
private bool $debug;
43+
44+
/**
45+
* Guzzle client.
46+
*
47+
* @var Client
48+
*/
49+
private $client;
50+
51+
/**
52+
* Creates an EdgeDB PHP client instance.
53+
*
54+
* @var string $hostname The EdgeDB server hostname (or IP).
55+
* @var int $port The EdgeDB server port.
56+
* @var string $database The EdgeDB database name.
57+
* @var bool $debug Indicates whether or not the client should display debug infos.
58+
*/
59+
public function __construct(string $hostname = '127.0.0.1', int $port = 10700, string $database = 'edgedb', bool $debug = false)
60+
{
61+
$this->hostname = $hostname;
62+
$this->port = $port;
63+
$this->database = $database;
64+
$this->debug = $debug;
65+
$this->client = new Client([
66+
'base_uri' => sprintf('http://%s:%d/db/%s/edgeql', $this->hostname, $this->port, $this->database)
67+
]);
68+
}
69+
70+
/**
71+
* Send an HTTP request against an EdgeDB server instance.
72+
*
73+
* @var EdgeDbQuery $query The EdgeDB query.
74+
* @return array
75+
*/
76+
public function send(EdgeDbQuery $query): array
77+
{
78+
$options = [
79+
'json' => $query->getQuery(),
80+
'debug' => $this->debug
81+
];
82+
83+
$response = $this->client->request('POST', '', $options);
84+
85+
$body = $response->getBody();
86+
87+
$json = json_decode($body->__toString(), true);
88+
89+
if ($this->debug) {
90+
$json['debug'] = [
91+
'jsonQuery' => $query->getQuery()
92+
];
93+
}
94+
95+
return $json;
96+
}
97+
98+
}

src/EdgeDbQuery.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TeddyBear06\EdgeDbPhp;
4+
5+
/**
6+
* An EdgeDB query helper.
7+
*/
8+
class EdgeDbQuery {
9+
10+
/**
11+
* EdgeDB query
12+
* @var string
13+
*/
14+
private string $query;
15+
16+
/**
17+
* EdgeDB query's variables
18+
* @var \stdClass
19+
*/
20+
private \stdClass $variables;
21+
22+
/**
23+
* Creates an EdgeDB PHP query instance
24+
*
25+
* @var string $query The query (see https://www.edgedb.com/docs/edgeql/index)
26+
* @var array $variables The variables required by your query
27+
*/
28+
public function __construct(string $query, array $variables = [])
29+
{
30+
$this->query = $query;
31+
$this->variables = (Object) $variables;
32+
}
33+
34+
/**
35+
* Generate an array with required EdgeDB fields.
36+
*
37+
* @return array
38+
*/
39+
public function getQuery(): array
40+
{
41+
return [
42+
'query' => $this->query,
43+
'variables' => $this->variables
44+
];
45+
}
46+
47+
}

tests/EdgeDbQueryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TeddyBear06\EdgeDbPhp;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use TeddyBear06\EdgeDbPhp\EdgeDbQuery;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class EdgeDbQueryTest extends TestCase
11+
{
12+
public function testSimpleEdgeQueryIsValid(): void
13+
{
14+
$edgeDbQuery = new EdgeDbQuery('select Author {firstname};');
15+
16+
$this->assertEquals(
17+
$edgeDbQuery->getQuery(),
18+
[
19+
'query' => 'select Author {firstname};',
20+
'variables' => (Object) []
21+
]
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)