/
GraphQL.php
196 lines (163 loc) · 4.67 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Suribit\GraphQLBundle;
use Doctrine\ORM\EntityManager;
use GraphQL\Error;
use GraphQL\GraphQL as GraphQLBase;
use GraphQL\Schema;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use Suribit\GraphQLBundle\ConfigDrivers\Yaml\YamlDriver;
use Suribit\GraphQLBundle\Error\ValidationError;
use Suribit\GraphQLBundle\Support\Type;
class GraphQL
{
public $em;
protected $config;
protected $mutations = [];
protected $queries = [];
protected $types = [];
protected $typesInstances = [];
public function __construct(EntityManager $em, YamlDriver $config)
{
$this->em = $em;
$this->config = $config;
}
/**
* @return Schema
*/
public function schema()
{
$this->typesInstances = [];
$schema = $this->config->getSchema();
$types = $this->config->getTypes();
foreach($types as $name => $type)
{
$this->addType($type, $name);
}
foreach($types as $name => $type)
{
$this->type($name);
}
$configQuery = $schema['query'];
$configMutation = $schema['mutation'];
$queryFields = array_merge($configQuery, $this->queries);
/** @var ObjectType $queryType */
$queryType = $this->buildTypeFromFields($queryFields, [
'name' => 'Query'
]);
$mutationFields = array_merge($configMutation, $this->mutations);
/** @var ObjectType $queryType */
$mutationType = $this->buildTypeFromFields($mutationFields, [
'name' => 'Mutation'
]);
return new Schema($queryType, $mutationType);
}
/**
* @param array $fields
* @param array $opts
* @return ObjectType
*/
protected function buildTypeFromFields($fields, $opts = [])
{
if (empty($fields)) {
return null;
}
$typeFields = [];
foreach($fields as $key => $field)
{
if(is_string($field))
{
$fieldObj = new $field();
$fieldObj->setManager($this);
$typeFields[$key] = $fieldObj->toArray();
}
else
{
$typeFields[$key] = $field;
}
}
return new ObjectType(array_merge([
'fields' => $typeFields
], $opts));
}
/**
* @param string $query
* @param array $params
* @return array
*/
public function query($query, $params = [])
{
$executionResult = $this->queryAndReturnResult($query, $params);
if (!empty($executionResult->errors))
{
return [
'data' => $executionResult->data,
'errors' => array_map([$this, 'formatError'], $executionResult->errors)
];
}
else
{
return [
'data' => $executionResult->data
];
}
}
public function queryAndReturnResult($query, $params = [])
{
$schema = $this->schema();
$result = GraphQLBase::executeAndReturnResult($schema, $query, null, $params);
return $result;
}
public function addMutation($name, $mutator)
{
$this->mutations[$name] = $mutator;
}
public function addQuery($name, $query)
{
$this->queries[$name] = $query;
}
public function addType($class, $name)
{
$this->types[$name] = $class;
}
public function type($name, $fresh = false)
{
if(!isset($this->types[$name]))
{
throw new \Exception('Type '.$name.' not found.');
}
if(!$fresh && isset($this->typesInstances[$name]))
{
return $this->typesInstances[$name];
}
/** @var Type $type */
$type = $this->types[$name];
$type = new $type();
$type->setManager($this);
$type->toType();
$this->typesInstances[$name] = $type;
//Check if the object has interfaces
if($type->interfaces)
{
InterfaceType::addImplementationToInterfaces($type->original);
}
return $type;
}
public function formatError(Error $e)
{
$error = [
'message' => $e->getMessage()
];
$locations = $e->getLocations();
if(!empty($locations))
{
$error['locations'] = array_map(function($loc) { return $loc->toArray();}, $locations);
}
$previous = $e->getPrevious();
if($previous && $previous instanceof ValidationError)
{
$error['validation'] = $previous->getValidatorMessages();
}
return $error;
}
}