1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\GraphQL\Field; |
6
|
|
|
|
7
|
|
|
use Andi\GraphQL\Attribute\Argument; |
8
|
|
|
use Andi\GraphQL\Attribute\MutationField; |
9
|
|
|
use Andi\GraphQL\Attribute\QueryField; |
10
|
|
|
use Andi\GraphQL\Definition\Field\TypeAwareInterface; |
11
|
|
|
use App\GraphQL\Type\AnimalEnum; |
12
|
|
|
use App\GraphQL\Type\DirectionEnum; |
13
|
|
|
use App\GraphQL\Type\ExampleAbstractObjectType; |
14
|
|
|
use App\GraphQL\Type\Money; |
15
|
|
|
use App\GraphQL\Type\User; |
16
|
|
|
use App\GraphQL\Type\UserInterface; |
17
|
|
|
|
18
|
|
|
final class SimpleService |
19
|
|
|
{ |
20
|
|
|
#[QueryField(name: 'echo')] |
21
|
|
|
#[MutationField(name: 'echo')] |
22
|
|
|
public function echoMessage(#[Argument] string $message): string |
23
|
|
|
{ |
24
|
|
|
return 'echo: ' . $message; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
#[QueryField] |
28
|
|
|
public function getUser(): UserInterface |
29
|
|
|
{ |
30
|
|
|
return new User('Gagarin', 'Yuri', 'Alekseyevich'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
#[MutationField] |
34
|
|
|
public function login(#[Argument(type: 'LoginRequest', mode: TypeAwareInterface::IS_REQUIRED)] array $input): ?User |
35
|
|
|
{ |
36
|
|
|
if ('yuri' === $input['login']) { |
37
|
|
|
return new User('Gagarin', 'Yuri', 'Alekseyevich'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
#[MutationField] |
44
|
|
|
public function signUp(#[Argument(type: 'RegistrationRequest!')] \stdClass $request): User |
45
|
|
|
{ |
46
|
|
|
return new User($request->lastname, $request->firstname, $request->middlename); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
#[QueryField] |
50
|
|
|
public function inverseDirection(#[Argument] DirectionEnum $direction): DirectionEnum |
51
|
|
|
{ |
52
|
|
|
return $direction === DirectionEnum::asc |
53
|
|
|
? DirectionEnum::desc |
54
|
|
|
: DirectionEnum::asc; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
#[QueryField(type: 'Animal')] |
58
|
|
|
public function inverseAnimal(#[Argument(type: AnimalEnum::class)] int $animal): int |
59
|
|
|
{ |
60
|
|
|
return AnimalEnum::DOG === $animal |
61
|
|
|
? AnimalEnum::CAT |
62
|
|
|
: AnimalEnum::DOG; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
#[QueryField(type: 'UserPetUnion!')] |
66
|
|
|
public function userOrPet(): mixed |
67
|
|
|
{ |
68
|
|
|
if (random_int(0, 9) < 5) { |
69
|
|
|
return $this->getUser(); |
70
|
|
|
} else { |
71
|
|
|
return 'Tom'; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
#[QueryField(type: 'ExampleAbstractUnionType!')] |
76
|
|
|
public function exampleAbstractUnionType(): mixed |
77
|
|
|
{ |
78
|
|
|
if (random_int(0, 9) < 5) { |
79
|
|
|
return $this->exampleAbstractObjectType(); |
80
|
|
|
} else { |
81
|
|
|
return 'Jerry'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
#[QueryField(type: Money::class)] |
86
|
|
|
public function randomSum(): int |
87
|
|
|
{ |
88
|
|
|
return random_int(10000, 50000); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
#[QueryField(type: ExampleAbstractObjectType::class)] |
92
|
|
|
public function exampleAbstractObjectType(): User |
93
|
|
|
{ |
94
|
|
|
return new User('Armstrong', 'Neil', 'Alden'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
#[QueryField(type: 'CoinSides!')] |
98
|
|
|
public function tossUpACoin(): bool |
99
|
|
|
{ |
100
|
|
|
return (bool) (random_int(0, 9) & 1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
#[QueryField] |
104
|
|
|
public function currentTime(): \DateTimeInterface |
105
|
|
|
{ |
106
|
|
|
return new \DateTimeImmutable(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
#[QueryField(type: 'Date!')] |
110
|
|
|
public function currentDate(): \DateTimeInterface |
111
|
|
|
{ |
112
|
|
|
return new \DateTimeImmutable(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|