-
Notifications
You must be signed in to change notification settings - Fork 7
/
Query.php
74 lines (56 loc) · 2.29 KB
/
Query.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
<?php
namespace CockpitQL;
use ArrayObject;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class Query {
public static function process($query = '{}', $variables = null) {
$app = cockpit();
$queries = new \ArrayObject(['name' => 'Query', 'fields' => []]);
$mutations = new \ArrayObject(['name' => 'Mutation', 'fields' => []]);
$types = new \ArrayObject([]);
$directives = new \ArrayObject(GraphQL::getStandardDirectives());
// load field schema defenitions
foreach ([
'region', // deprecated and will be removed in the future
'collection',
'singleton',
'dynamic_collections',
'dynamic_singletons',
] as $fieldSchemaFile) {
include(__DIR__."/fields/{$fieldSchemaFile}.php");
}
$app->trigger('cockpitql.config', [$queries, $mutations, $types, $directives]);
$queryType = new ObjectType($queries->getArrayCopy());
$mutationType = new ObjectType($mutations->getArrayCopy());
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
'types' => $types->getArrayCopy(),
'directives' => $directives->getArrayCopy(),
]);
try {
$rootValue = [];
$context = new ArrayObject([]);
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)->toArray();
if (isset($result['data'])) {
foreach ($result['data'] as $key => $value) {
if ($value && is_string($value)) {
$start = substr($value,0,1);
$end = substr($value,-1,1);
if (($start == '[' && $end == ']') || ($start == '{' && $end == '}')) {
$result['data'][$key] = json_decode($value);
} elseif ($value == 'null') {
$result['data'][$key] = null;
}
}
}
}
} catch (\Exception $e) {
return $app->stop(json_encode(['error' => [ 'message' => $e->getMessage() ]]), 400);
}
return $result;
}
}