|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php'; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
6
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
use GraphQL\Type\Schema; |
|
9
|
|
|
use GraphQL\GraphQL; |
|
10
|
|
|
use GraphQL\Type\Definition\ValidatedFieldDefinition; |
|
11
|
|
|
|
|
12
|
|
|
$authors = [ |
|
13
|
|
|
1 => [ |
|
14
|
|
|
'id' => 1, |
|
15
|
|
|
'name'=> 'Cormac McCarthy', |
|
16
|
|
|
'deceased' => false |
|
17
|
|
|
], |
|
18
|
|
|
2 => [ |
|
19
|
|
|
'id' => 2, |
|
20
|
|
|
'name' => 'J.D.Salinger', |
|
21
|
|
|
'deceased' => true |
|
22
|
|
|
], |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
class AuthorType extends ObjectType { |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct([ |
|
29
|
|
|
'fields' => [ |
|
30
|
|
|
'id' => [ |
|
31
|
|
|
'type' => Type::id(), |
|
32
|
|
|
'resolve' => function($author) { |
|
33
|
|
|
return $author['id']; |
|
34
|
|
|
} |
|
35
|
|
|
], |
|
36
|
|
|
'name' => [ |
|
37
|
|
|
'type' => Type::string(), |
|
38
|
|
|
'resolve' => function($author) { |
|
39
|
|
|
return $author['name']; |
|
40
|
|
|
} |
|
41
|
|
|
] |
|
42
|
|
|
] |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
class AuthorAttributes extends InputObjectType { |
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct([ |
|
51
|
|
|
'fields' => [ |
|
52
|
|
|
'name' => [ |
|
53
|
|
|
'type' => Type::string(), |
|
54
|
|
|
'errorCodes' => [ |
|
55
|
|
|
'nameTooLong', |
|
56
|
|
|
'nameNotUnique' |
|
57
|
|
|
], |
|
58
|
|
|
'validate' => function(string $name) { |
|
59
|
|
|
global $authors; |
|
60
|
|
|
|
|
61
|
|
|
if(strlen($name) > 15) { |
|
62
|
|
|
return ['nameTooLong', "Name is too long; please keep it under 15 characters."]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if(array_search($name, array_column($authors, "name")) !== false) { |
|
66
|
|
|
return ['nameNotUnique', 'That name is already in use']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return 0; |
|
70
|
|
|
} |
|
71
|
|
|
], |
|
72
|
|
|
'age' => [ |
|
73
|
|
|
'type' => Type::int(), |
|
74
|
|
|
'errorCodes' => [ |
|
75
|
|
|
'invalidAge' |
|
76
|
|
|
], |
|
77
|
|
|
'validate' => function(int $age) { |
|
78
|
|
|
if($age <= 0) { |
|
79
|
|
|
return ['invalidAge', "Invalid Age; must be positive"]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return 0; |
|
83
|
|
|
} |
|
84
|
|
|
] |
|
85
|
|
|
] |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$authorType = new AuthorType(); |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
$mutationType = new ObjectType([ |
|
94
|
|
|
'name' => 'Mutation', |
|
95
|
|
|
'fields' => [ |
|
96
|
|
|
'updateAuthor' => new ValidatedFieldDefinition([ |
|
97
|
|
|
'name' => 'updateAuthor', |
|
98
|
|
|
'type' => $authorType, |
|
99
|
|
|
'args' => [ |
|
100
|
|
|
'authorId' => [ |
|
101
|
|
|
'type' => Type::id(), |
|
102
|
|
|
'errorCodes' => [ |
|
103
|
|
|
'unknownAuthor', |
|
104
|
|
|
'deceasedAuthor' |
|
105
|
|
|
], |
|
106
|
|
|
'validate' => function(string $authorId) use ($authors) { |
|
107
|
|
|
if (!isset($authors[$authorId])) { |
|
108
|
|
|
return ['unknownAuthor', "We have no record of that author"]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return 0; |
|
112
|
|
|
} |
|
113
|
|
|
], |
|
114
|
|
|
'attributes' => [ |
|
115
|
|
|
'type' => new AuthorAttributes() |
|
116
|
|
|
] |
|
117
|
|
|
], |
|
118
|
|
|
'resolve' => function ($value, $args) use ($authors) { |
|
119
|
|
|
$authorId = $args['authorId']; |
|
120
|
|
|
|
|
121
|
|
|
// AuthorProvider::update($authorId, $args['attributes']); |
|
122
|
|
|
return $authors[$authorId]; |
|
123
|
|
|
}, |
|
124
|
|
|
]), |
|
125
|
|
|
], |
|
126
|
|
|
]); |
|
127
|
|
|
|
|
128
|
|
|
$queryType = new ObjectType([ |
|
129
|
|
|
'name'=>'Query', |
|
130
|
|
|
'fields'=>[ |
|
131
|
|
|
'author'=> [ |
|
132
|
|
|
'type' => $authorType, |
|
133
|
|
|
"args" => [ |
|
134
|
|
|
'authorId' => [ |
|
135
|
|
|
'type' => Type::id() |
|
136
|
|
|
] |
|
137
|
|
|
], |
|
138
|
|
|
'resolve' => function($value, $args) use ($authors) { |
|
139
|
|
|
return $authors[$args['authorId']]; |
|
140
|
|
|
} |
|
141
|
|
|
] |
|
142
|
|
|
] |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
$schema = new Schema([ |
|
146
|
|
|
'mutation' => $mutationType, |
|
147
|
|
|
'query' => $queryType |
|
148
|
|
|
]); |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$rawInput = file_get_contents('php://input'); |
|
152
|
|
|
$input = json_decode($rawInput, true); |
|
153
|
|
|
$query = $input['query']; |
|
154
|
|
|
$variableValues = isset($input['variables']) ? $input['variables'] : null; |
|
155
|
|
|
$result = GraphQL::executeQuery($schema, $query, null, null, $variableValues); |
|
156
|
|
|
$output = $result->toArray(); |
|
157
|
|
|
} catch (\Exception $e) { |
|
158
|
|
|
$output = [ |
|
159
|
|
|
'error' => [ |
|
160
|
|
|
'message' => $e->getMessage() |
|
161
|
|
|
] |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
header('Content-Type: application/json; charset=UTF-8'); |
|
165
|
|
|
echo json_encode($output); |
|
166
|
|
|
|