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
|
|
|
const DEFAULT_COMPLEXITY_FN = '\Overblog\GraphQLBundle\Definition\FieldDefinition::defaultComplexity'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
private $complexityFn; |
25
|
|
|
|
26
|
38 |
|
public static function getDefinition() |
27
|
|
|
{ |
28
|
38 |
|
return array_merge( |
29
|
38 |
|
parent::getDefinition(), |
30
|
|
|
[ |
31
|
38 |
|
'complexity' => Config::CALLBACK, |
32
|
|
|
] |
33
|
38 |
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
37 |
|
public static function createMap(array $fields) |
37
|
|
|
{ |
38
|
37 |
|
$map = []; |
39
|
37 |
|
foreach ($fields as $name => $field) { |
40
|
37 |
|
if (!isset($field['name'])) { |
41
|
37 |
|
$field['name'] = $name; |
42
|
37 |
|
} |
43
|
37 |
|
$map[$name] = static::create($field); |
44
|
37 |
|
} |
45
|
|
|
|
46
|
37 |
|
return $map; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $field |
51
|
|
|
* |
52
|
|
|
* @return FieldDefinition |
53
|
|
|
*/ |
54
|
37 |
|
public static function create($field) |
55
|
|
|
{ |
56
|
37 |
|
Config::validate($field, static::getDefinition()); |
57
|
|
|
|
58
|
37 |
|
return new static($field); |
59
|
|
|
} |
60
|
|
|
|
61
|
37 |
|
protected function __construct(array $config) |
62
|
|
|
{ |
63
|
37 |
|
parent::__construct($config); |
64
|
|
|
|
65
|
37 |
|
$this->complexityFn = isset($config['complexity']) ? $config['complexity'] : static::DEFAULT_COMPLEXITY_FN; |
66
|
37 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return callable|\Closure |
70
|
|
|
*/ |
71
|
10 |
|
public function getComplexityFn() |
72
|
|
|
{ |
73
|
10 |
|
return $this->complexityFn; |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
public static function defaultComplexity($childrenComplexity) |
77
|
|
|
{ |
78
|
12 |
|
return $childrenComplexity + 1; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|