-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·127 lines (108 loc) · 3.4 KB
/
index.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
<?php
require('vendor/autoload.php');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Alow-Headers, X-Requested-With');
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
$db = new PDO('sqlite:' . __DIR__ . '/db.sqlite');
$userType = new ObjectType([
'name' => 'User',
'fields' => [
'id' => Type::int(),
'name' => Type::string(),
'admin' => Type::boolean(),
],
]);
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'users' => [
'type' => Type::listOf($userType),
'resolve' => function ($root, $args) {
global $db;
$results = [];
foreach ($db->query('SELECT * FROM users') as $row) {
$results[] = $row;
}
return $results;
}
],
'user' => [
'type' => $userType,
'args' => [
'id' => Type::nonNull(Type::int()),
],
'resolve' => function ($root, $args) {
global $db;
$query = $db->query('SELECT * FROM users WHERE id=:id');
$query->bindParam(':id', $args['id']);
$query->execute();
return $query->fetch(PDO::FETCH_ASSOC);
}
],
],
]);
$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => [
'addUser' => [
'type' => $userType,
'args' => [
'name' => Type::string(),
'admin' => Type::boolean(),
],
'resolve' => function ($root, $args) {
global $db;
$query = $db->prepare('INSERT INTO users (name, admin) VALUES (:name, :admin)');
$query->bindParam(':name', $args['name']);
$query->bindParam(':admin', $args['admin']);
$query->execute();
return [
'id' => $db->lastInsertId(),
'name' => $args['name'],
'admin' => $args['admin'],
];
}
],
'deleteUser' => [
'type' => $userType,
'args' => [
'id' => Type::nonNull(Type::int()),
],
'resolve' => function ($root, $args) {
global $db;
$query = $db->query('DELETE FROM users WHERE id=:id');
$query->bindParam(':id', $args['id']);
$query->execute();
return [
'id' => null,
];
}
]
]
]);
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
]);
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
try {
$result = GraphQL::executeQuery($schema, $query, null, null, $variableValues);
$output = $result->toArray();
} catch (\Exception $e) {
$output = [
'errors' => [
[
'message' => $e->getMessage()
]
]
];
}
header('Content-Type: application/json');
echo json_encode($output);