|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Functional\App\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Error\Error; |
|
6
|
|
|
use GraphQL\Language\AST\StringValueNode; |
|
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
8
|
|
|
use GraphQL\Utils; |
|
9
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
|
10
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverMap; |
|
11
|
|
|
|
|
12
|
|
|
class SchemaLanguageQueryResolverMap extends ResolverMap |
|
13
|
|
|
{ |
|
14
|
|
|
protected function map() |
|
15
|
|
|
{ |
|
16
|
|
|
return [ |
|
17
|
|
|
'Query' => [ |
|
18
|
|
|
self::RESOLVE_FIELD => function ($value, Argument $args, \ArrayObject $context, ResolveInfo $info) { |
|
19
|
|
|
if ('character' === $info->fieldName) { |
|
20
|
|
|
$characters = Characters::getCharacters(); |
|
21
|
|
|
$id = (int) $args['id']; |
|
22
|
|
|
if (isset($characters[$id])) { |
|
23
|
|
|
return $characters[$id]; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return null; |
|
28
|
|
|
}, |
|
29
|
|
|
'findHumansByDateOfBirth' => function ($value, Argument $args) { |
|
30
|
|
|
$years = $args['years']; |
|
31
|
|
|
|
|
32
|
|
|
return array_filter(Characters::getHumans(), function ($human) use ($years) { |
|
33
|
|
|
return in_array($human['dateOfBirth'], $years); |
|
34
|
|
|
}); |
|
35
|
|
|
}, |
|
36
|
|
|
'humans' => [Characters::class, 'getHumans'], |
|
37
|
|
|
'direwolves' => [Characters::class, 'getDirewolves'], |
|
38
|
|
|
], |
|
39
|
|
|
'Character' => [ |
|
40
|
|
|
self::RESOLVE_TYPE => function ($value) { |
|
41
|
|
|
return Characters::TYPE_HUMAN === $value['type'] ? 'Human' : 'Direwolf'; |
|
42
|
|
|
}, |
|
43
|
|
|
], |
|
44
|
|
|
'Human' => [ |
|
45
|
|
|
'direwolf' => function ($value) { |
|
46
|
|
|
$direwolves = Characters::getDirewolves(); |
|
47
|
|
|
if (isset($direwolves[$value['direwolf']])) { |
|
48
|
|
|
return $direwolves[$value['direwolf']]; |
|
49
|
|
|
} else { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
}, |
|
53
|
|
|
], |
|
54
|
|
|
// enum internal values |
|
55
|
|
|
'Status' => [ |
|
56
|
|
|
'ALIVE' => 1, |
|
57
|
|
|
'DECEASED' => 0, |
|
58
|
|
|
], |
|
59
|
|
|
// custom scalar |
|
60
|
|
|
'Year' => [ |
|
61
|
|
|
self::SERIALIZE => function ($value) { |
|
62
|
|
|
return sprintf('%s AC', $value); |
|
63
|
|
|
}, |
|
64
|
|
|
self::PARSE_VALUE => function ($value) { |
|
65
|
|
|
if (!is_string($value)) { |
|
66
|
|
|
throw new Error(sprintf('Cannot represent following value as a valid year: %s.', Utils::printSafeJson($value))); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return (int) str_replace(' AC', '', $value); |
|
70
|
|
|
}, |
|
71
|
|
|
self::PARSE_LITERAL => function ($valueNode) { |
|
72
|
|
|
if (!$valueNode instanceof StringValueNode) { |
|
73
|
|
|
throw new Error('Query error: Can only parse strings got: '.$valueNode->kind, [$valueNode]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return (int) str_replace(' AC', '', $valueNode->value); |
|
77
|
|
|
}, |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|