-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphDatabaseAccessLayer.php
468 lines (359 loc) · 14.7 KB
/
GraphDatabaseAccessLayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
namespace app\helpers;
use Yii;
use GuzzleHttp;
use GraphAware\Neo4j\Client\ClientBuilder;
use yii\helpers\FileHelper;
use yii\helpers\Json;
/**
* Class GraphDatabaseAccessLayer. This is the layer to use to communicate with a graphql neo4j database
* @package app\helpers
*/
class GraphDatabaseAccessLayer
{
/**
* Build GraphQL schema models and deploy to database schema provided
* @param $graphqlSchema String
* @throws \yii\base\Exception
*/
public static function buildSchema($graphqlSchema) {
self::updateDatabaseGraph($graphqlSchema);
self::buildSchemaModels($graphqlSchema);
}
/**
* Use this function to update graphql graph in database with provided graphql schema
* @param $graphqlSchema String
*/
private static function updateDatabaseGraph($graphqlSchema) {
// Create a client to perform connection and set schema
$client = ClientBuilder::create()
->addConnection('bolt', 'bolt://' . Yii::$app->params['db_username'] . ':' . Yii::$app->params['db_password'] . '@localhost:7687')
->build();
// Load schema
$schema = file_get_contents(Yii::getAlias("@app/models/$graphqlSchema"));
// Deploy schema to database
$query = "CALL graphql.idl('$schema')";
$client->run($query);
}
/**
* @param $graphqlSchema String GraphQL schema filename. Example: Models.graphql
* @return string
* @throws \yii\base\Exception
*/
public static function buildSchemaModels($graphqlSchema) {
$modelsPath = Yii::getAlias('@' . self::getModelsPath(false, true));
// Get schema data from
$schema = file_get_contents($modelsPath . $graphqlSchema);
// Normalize datas and extract types
$types = self::extractTypes($schema);
// Create path if not exists
FileHelper::createDirectory($modelsPath . 'graphql');
// Create model classes
foreach ($types as $type) {
self::createModel($type);
}
// Return status
return Json::encode([
'status' => 'success',
]);
}
/**
* Extract types from provided schema
* @param $schema String Schema provided from a .graphql file
* @return array
*/
private static function extractTypes($schema) {
$types = [];
// Normalize and split data
$entries = preg_split("/}\n|}$/", $schema);
for($i = 0; $i < count($entries); $i++) {
$entries[$i] = ltrim(rtrim(str_replace("\n\n", "\n", $entries[$i])));
}
// Second normalization
foreach ($entries as $entry) {
// Check if entry is a type or an interface
if(substr($entry, 0, 5) == 'type ' || substr($entry, 0, 10) == 'interface ') {
// Regex:
// - content in parenthesis
$parenthesis = '\([^)]+\)';
// - decorators
$decorators = '@.*';
// - extension and implementation
$typeExtImpl = '(implements|extends).+';
// - remaining characters
$other = 'type |interface |{| ';
// Formatted data
$formatted = preg_replace(["/$parenthesis|$decorators|$typeExtImpl|$other/"], '', $entry);
$formattedParts = explode("\n", $formatted, 2);
$attributes = [];
foreach (explode("\n", $formattedParts[1]) as $parameter) {
// Build parameter
$paramParts = explode(':', $parameter);
$attributes[] = [
'name' => lcfirst($paramParts[0]),
'type' => self::typeConversion(str_replace('!', '', $paramParts[1]), true), // Remove required field: !
'isArray' => substr($paramParts[1], 0, 1) == '[',
'required' => substr($paramParts[1], -1) == '!',
'visibility' => 'protected',
];
}
// Sort attributes by required fields
usort($attributes, function ($a, $b) {
return $b['required'] <=> $a['required'];
});
// Create new type
$types[] = [
'className' => $formattedParts[0],
'attributes' => $attributes,
];
}
}
return $types;
}
/**
* Create a single model php file used to interact with database
* @param $type array Particular structure created from types extraction
*/
// TODO improve this method
private static function createModel($type) {
// Get className
$className = $type['className'];
// Generate attributes with documentation
$attributes = self::generateAttributes($type['attributes']);
// Generate methods
$methods = self::generateMethods($type['attributes']);
// Generate constructor
$constructor = self::generateConstructor($type['attributes']);
// Generate getClass method
$getClass = self::generateGetClassMethod($className);
// Add to methods the generated overridden query method
$methods .= self::generateQueryMethod();
// Generate user query attributes
$queryAttributes = self::generateQueryAttributes($type['attributes']);
// Generate template
$template = "<?php\n\nnamespace " . self::getModelsPath(true, false, false) . ";\n\nuse app\helpers\GraphModelType;\n\nclass $className extends GraphModelType { $attributes\n$methods\n$constructor\n$getClass\n}\n\nclass ${className}QueryAttributes {\n$queryAttributes}";
// Save model
file_put_contents(Yii::getAlias('@app/models/graphql/' . $className . '.php'), $template);
}
/**
* Generate attributes with relative documentations
* @param $inputAttributes
* @return string
*/
private static function generateAttributes($inputAttributes) {
$attributes = '';
// Attributes generator loop
foreach ($inputAttributes as $attribute) {
// Check if attribute is required
$required = '';
if($attribute['required']) {
$required = "\n\t * @required This attribute is required for mutations";
}
// Save attribute
$documentation = "\t/**\n\t * @var ${attribute['type']}" . ($attribute['isArray'] ? '[]' : '') . "$required\n\t */";
$attributes .= "\n\n$documentation\n\t${attribute['visibility']} $${attribute['name']};";
}
return $attributes;
}
/**
* Generate all methods for all attributes
* @param $inputAttributes
* @return string
*/
private static function generateMethods($inputAttributes) {
$methods = '';
// Methods generator loop
foreach ($inputAttributes as $attribute) {
// Check if type is standard or GraphModelType
$isGraphModelType = self::isGraphModelType($attribute['type']);
// Generate getter and setter methods for all attributes
$methods .= "\n\n\tpublic function ${attribute['name']}() {\n\t\tparent::requestAttribute('${attribute['name']}', '${attribute['type']}');\n\t\treturn \$this->${attribute['name']};\n\t}"; // Getter
$methods .= "\n\n\tpublic function set" . ucfirst($attribute['name']) . "($${attribute['name']}) {\n\t\t\$this->${attribute['name']} = $${attribute['name']};\n\t}"; // Setter
// Generate methods for defined types
if($isGraphModelType) {
$methods .= "\n\n\tpublic function get" . ucfirst($attribute['name']) . "Class() { return ${attribute['type']}::getClass(); }";
}
}
return $methods;
}
private static function generateConstructor($inputAttributes) {
$params = '';
$directives = '';
$paramsCount = 0;
// Generator loop
foreach ($inputAttributes as $attribute) {
// Check if type is standard or GraphModelType
$isGraphModelType = self::isGraphModelType($attribute['type']);
// Check if is a standard type
if(!$isGraphModelType) {
// Adds parameters and directives to constructor
$params .= "\$${attribute['name']} = null, ";
$directives .= "\n\t\t\$this->${attribute['name']} = $${attribute['name']};";
$paramsCount++;
}
}
// Remove trailing characters from parameters
if($paramsCount > 0) {
$params = substr($params, 0, strlen($params) - 2);
}
return "\n\tpublic function __construct($params) { $directives\n\t}";
}
/**
* Generate a method used to get class name
* @param $className String
* @return string
*/
private static function generateGetClassMethod($className) {
return "\n\tpublic static function getClass() { return '$className'; }";
}
private static function generateQueryMethod() {
return "\n\n\tpublic static function query(\$attributes, \$callback) {\n\t\treturn parent::executeQuery(self::getClass(), \$callback);\n\t}";
}
/**
* Generate query attributes to use when user wants specific query constraints
* @param $attributes array Attributes of particular model
* @return string Attributes list as string
*/
private static function generateQueryAttributes($attributes) {
$queryAttributes = '';
// Generation loop
foreach ($attributes as $attribute) {
$queryAttributes .= "\tconst " . $attribute['name']. " = '" . $attribute['name'] . "';\n";
}
// Return attributes
return $queryAttributes;
}
/**
* Check if provided type is child of GraphModelType: if it's a webmaster defined type
* @param $type String Class name of type you want to check
* @return bool
*/
public static function isGraphModelType($type) {
return is_subclass_of(self::getModelsPath(true) . $type, 'app\helpers\GraphModelType');
}
/**
* Converts a specific graphql type in a standard language defined type
* @param $type String GraphQL input type
* @param bool $ignoreArray Set true if you want to ignore characters like '[' and ']'
* @return string
*/
private static function typeConversion($type, $ignoreArray = false) {
$isArray = false;
// Check if is array
if(self::isArray($type)) {
$isArray = true;
$type = substr($type, 1, strlen($type) - 2);
}
// Do conversion
return str_replace(['Long', 'ID'], ['int', 'mixed'], $type) . ($isArray && !$ignoreArray ? '[]' : '');
}
/**
* Check if provided type is an array of types or single type
* @param $type String GraphQL type
* @return bool
*/
private static function isArray($type) {
return substr($type, 0, 1) == '[';
}
/**
* Execute GraphQL query
* @param $gql String GraphQL query
* @return GraphModelType[]
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GraphDatabaseAccessLayerException
*/
public static function doQuery($gql) {
// Make an HTTP request to endpoint
$client = new GuzzleHttp\Client();
$res = $client->request('POST', Yii::$app->params['api_endpoint'], [
'headers' => [
'Authorization' => 'Basic ' . base64_encode(Yii::$app->params['db_username'] . ':' . Yii::$app->params['db_password']),
],
GuzzleHttp\RequestOptions::JSON => [
'operationName' => null,
'variables' => [],
'query' => $gql,
],
]);
// Get response
$response = Json::decode($res->getBody()->getContents());
// Check if has failed
if(isset($response['errors'])) {
throw new GraphDatabaseAccessLayerException($response['errors'][0]['message']);
}
// Result values
return self::json2Object($response['data']);
}
/**
* Convert GraphQL json response to language objects instances
* @param $json mixed GraphQL json data
* @return array|mixed
*/
private static function json2Object($json) {
$result = [];
// Walking through json data
foreach ($json as $modelName => $instances) {
// Check if is single object or an array of objects
if(array_values($instances) !== $instances) {
$result = self::createModelFromJson($modelName, $instances);
} else {
foreach ($instances as $instance) {
$result[] = self::createModelFromJson($modelName, $instance);
}
}
}
return $result;
}
/**
* Create model from json input data
* @param $modelName String
* @param $params array Parameters of specified model
* @return mixed
*/
private static function createModelFromJson($modelName, $params) {
// Create new model from class name
$modelName = self::getModelsPath(true) . $modelName;
$model = new $modelName();
foreach ($params as $paramName => $paramValue) {
// Method to call, if exists, to get types of attributes
$getClassMethodName = 'get' . ucfirst($paramName) . 'Class';
// Define setter method
$setter = "set$paramName";
if (method_exists($model, $getClassMethodName)) {
// Iterate through other graphql object
$model->$setter(self::json2Object([str_replace(self::getModelsPath(true), '', $model->$getClassMethodName()) => $paramValue]));
} else {
// Set attribute value
$model->$setter($paramValue);
}
}
return $model;
}
/**
* @deprecated
* @param $gql
*/
public static function mutation($gql) {
// TODO
}
/**
* Get models path in project
* @param $isGraphQLModel bool
* @param bool $forwardSlash
* @param bool $finalSlash
* @return bool|string
*/
public static function getModelsPath($isGraphQLModel, $forwardSlash = false, $finalSlash = true) {
// Get proper string basing on input parameters
$absPath = !$forwardSlash ? 'app\models\graphql\\' : 'app/models/graphql/';
if(!$isGraphQLModel) {
$absPath = !$forwardSlash ? 'app\models\\' : 'app/models/';
}
// Remove final slash if required
if(!$finalSlash) {
$absPath = substr($absPath, 0, strlen($absPath) - 1);
}
// Return path
return $absPath;
}
}