-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.php
52 lines (44 loc) · 1.34 KB
/
graphql.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
<?php
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use App\GraphQL\AppType;
use App\GraphQL\Database\User;
use App\GraphQL\Types\QueryType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
try {
$schema = new Schema([
'query' => new QueryType,
'mutation' => new ObjectType([
'name' => 'Mutations',
'fields' => [
'createUser' => [
'type' => AppType::user(),
'args' => [
'name' => Type::nonNull(Type::string()),
'email' => Type::nonNull(Type::string()),
],
'resolve' => function ($value, $args) {
return User::insert($args['name'], $args['email'], 1);
}
]
]
])
]);
$input = file_get_contents('php://input');
$input = json_decode($input, true);
$query = $input['query'];
$variables = $input['variables'] ?? null;
$result = GraphQL::executeQuery($schema, $query, null, null, $variables);
$output = $result->toArray();
} catch (\Exception $e) {
$output = [
'errors' => [
[
'message' => $e->getMessage()
]
]
];
}
header('ContentType: application/json');
echo json_encode($output);