1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Definition; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Config; |
15
|
|
|
use GraphQL\Type\Definition\FieldDefinition as BaseFieldDefinition; |
16
|
|
|
|
17
|
|
|
class FieldDefinition extends BaseFieldDefinition |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var callable |
21
|
|
|
*/ |
22
|
|
|
private $complexityFn; |
23
|
|
|
|
24
|
|
|
public static function getDefinition() |
25
|
|
|
{ |
26
|
|
|
return array_merge( |
27
|
|
|
parent::getDefinition(), |
28
|
|
|
[ |
29
|
|
|
'complexity' => Config::CALLBACK, |
30
|
|
|
] |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function createMap(array $fields) |
35
|
|
|
{ |
36
|
|
|
$map = []; |
37
|
|
|
foreach ($fields as $name => $field) { |
38
|
|
|
if (!isset($field['name'])) { |
39
|
|
|
$field['name'] = $name; |
40
|
|
|
} |
41
|
|
|
$map[$name] = static::create($field); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $map; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array|Config $field |
49
|
|
|
* |
50
|
|
|
* @return FieldDefinition |
51
|
|
|
*/ |
52
|
|
|
public static function create($field) |
53
|
|
|
{ |
54
|
|
|
Config::validate($field, static::getDefinition()); |
|
|
|
|
55
|
|
|
|
56
|
|
|
return new static($field); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function __construct(array $config) |
60
|
|
|
{ |
61
|
|
|
parent::__construct($config); |
62
|
|
|
|
63
|
|
|
$this->complexityFn = isset($config['complexity']) ? $config['complexity'] : [$this, 'defaultComplexity']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return callable|\Closure |
68
|
|
|
*/ |
69
|
|
|
public function getComplexityFn() |
70
|
|
|
{ |
71
|
|
|
return $this->complexityFn; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function defaultComplexity($childrenComplexity) |
75
|
|
|
{ |
76
|
|
|
return $childrenComplexity + 1; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.