-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphQLSchema.php
92 lines (75 loc) · 2.92 KB
/
GraphQLSchema.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
<?php
namespace MyApp;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class GraphQLSchema {
private $db;
private $jwtHandler;
private $pageSize;
public function __construct($db, $jwtHandler, $pageSize) {
$this->db = $db;
$this->jwtHandler = $jwtHandler;
$this->pageSize = $pageSize;
}
public function createSchema() {
// Fetch all table names
$tables = $this->db->query("SHOW TABLES");
$queryFields = [];
foreach ($tables as $table) {
$tableName = array_values($table)[0];
$columns = $this->db->getColumns($tableName);
$fields = [];
foreach ($columns as $column) {
$fields[$column] = ['type' => Type::string()];
}
$objectType = new ObjectType([
'name' => ucfirst($tableName),
'fields' => $fields,
]);
$queryFields[$tableName] = [
'type' => new ObjectType([
'name' => ucfirst($tableName) . 'Page',
'fields' => [
$tableName => ['type' => Type::listOf($objectType)],
'total' => ['type' => Type::int()],
'page' => ['type' => Type::int()],
'token' => ['type' => Type::string()],
]
]),
'args' => [
'page' => ['type' => Type::int()],
],
'resolve' => function($root, $args, $context) use ($tableName) {
$page = isset($args['page']) ? $args['page'] : 1;
$offset = ($page - 1) * $this->pageSize;
// Validate the JWT token from context
if (!isset($context['token'])) {
throw new \Exception('Token is required.');
}
$token = $context['token'];
$this->jwtHandler->decode($token);
// Fetch total count of records
$total = $this->db->count($tableName);
// Fetch records with limit and offset
$records = $this->db->query("SELECT * FROM $tableName LIMIT ?, ?", ['ii', $offset, $this->pageSize]);
// Generate a new token for the next page
$newToken = $this->jwtHandler->encode(['page' => $page + 1]);
return [
$tableName => $records,
'total' => $total,
'page' => $page,
'token' => $newToken
];
}
];
}
$queryType = new ObjectType([
'name' => 'Query',
'fields' => $queryFields,
]);
return new Schema([
'query' => $queryType
]);
}
}