-
Notifications
You must be signed in to change notification settings - Fork 20
/
UserController.php
42 lines (33 loc) · 1.25 KB
/
UserController.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
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\Controllers;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Validator\Annotations\Assertion;
use TheCodingMachine\GraphQLite\Validator\Fixtures\Types\User;
use TheCodingMachine\GraphQLite\Validator\ValidationFailedException;
class UserController
{
public function __construct(private ValidatorInterface $validator)
{
}
#[Mutation]
public function createUser(string $email, string $password): User
{
$user = new User($email, $password);
// Let's validate the user
$errors = $this->validator->validate($user);
// Throw an appropriate GraphQL exception if validation errors are encountered
ValidationFailedException::throwException($errors);
// No errors? Let's continue and save the user
return $user;
}
#[Query]
#[Assertion(for: '$email', constraint: new Assert\Email())]
public function findByMail(string $email = 'a@a.com'): User
{
return new User($email, 'foo');
}
}