Skip to content

Commit bd9921f

Browse files
authored
Merge pull request #38 from moufmouf/validator
Adding support for symfony validator bridge
2 parents 64a9470 + 16c4ac2 commit bd9921f

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Resources/config/container/graphqlite.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
<tag name="graphql.parameter_middleware"/>
8282
</service>
8383

84+
<service id="TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\AssertParameterMiddleware">
85+
<argument type="service" id="validator.validator_factory" />
86+
<tag name="graphql.parameter_middleware"/>
87+
</service>
88+
8489
<service id="TheCodingMachine\Graphqlite\Bundle\Controller\GraphQL\LoginController" public="true">
8590
<argument key="$firewallName">%graphqlite.security.firewall_name%</argument>
8691
</service>

Tests/Fixtures/Controller/TestGraphqlController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GraphQL\Error\Error;
88
use Porpaginas\Arrays\ArrayResult;
99
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\Validator\Constraints as Assertion;
1011
use TheCodingMachine\GraphQLite\Annotations\FailWith;
1112
use TheCodingMachine\GraphQLite\Annotations\Logged;
1213
use TheCodingMachine\GraphQLite\Annotations\Right;
@@ -16,6 +17,8 @@
1617
use TheCodingMachine\GraphQLite\Annotations\Query;
1718
use TheCodingMachine\GraphQLite\Exceptions\GraphQLAggregateException;
1819
use TheCodingMachine\GraphQLite\Exceptions\GraphQLException;
20+
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
21+
use TheCodingMachine\Graphqlite\Validator\Fixtures\Types\User;
1922

2023
class TestGraphqlController
2124
{
@@ -125,4 +128,13 @@ public function getUri(Request $request): string
125128
{
126129
return $request->getPathInfo();
127130
}
131+
132+
/**
133+
* @Query
134+
* @Assert(for="email", constraint=@Assertion\Email())
135+
*/
136+
public function findByMail(string $email = '[email protected]'): string
137+
{
138+
return $email;
139+
}
128140
}

Tests/FunctionalTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ public function testExceptionHandler(): void
152152
$this->assertSame(404, $response->getStatusCode());
153153

154154
$result = json_decode($response->getContent(), true);
155-
var_dump($result);
156-
157-
//$this->assertSame('Cannot query field "me" on type "Query".', $result['errors'][0]['message']);
158155

156+
$this->assertSame('foo', $result['errors'][0]['message']);
157+
$this->assertSame('bar', $result['errors'][1]['message']);
158+
$this->assertSame('MyCat', $result['errors'][1]['extensions']['category']);
159+
$this->assertSame('baz', $result['errors'][1]['extensions']['field']);
159160
}
160161

161162
public function testLoggedMiddleware(): void
@@ -396,6 +397,31 @@ public function testAllOff(): void
396397
$this->assertSame('Cannot query field "me" on type "Query".', $result['errors'][0]['message']);
397398
}
398399

400+
public function testValidation(): void
401+
{
402+
$kernel = new GraphqliteTestingKernel(true, 'off', true, 'off');
403+
$kernel->boot();
404+
405+
$session = new Session(new MockArraySessionStorage());
406+
$container = $kernel->getContainer();
407+
$container->set('session', $session);
408+
409+
$request = Request::create('/graphql', 'POST', ['query' => '
410+
{
411+
findByMail(email: "notvalid")
412+
}
413+
']);
414+
415+
$response = $kernel->handle($request);
416+
417+
$result = json_decode($response->getContent(), true);
418+
$errors = $result['errors'];
419+
420+
$this->assertSame('This value is not a valid email address.', $errors[0]['message']);
421+
$this->assertSame('email', $errors[0]['extensions']['field']);
422+
$this->assertSame('Validate', $errors[0]['extensions']['category']);
423+
}
424+
399425
private function logIn(ContainerInterface $container)
400426
{
401427
// put a token into the storage so the final calls can function

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
],
1818
"require" : {
1919
"php" : ">=7.2",
20-
"thecodingmachine/graphqlite" : "^4",
20+
"thecodingmachine/graphqlite" : "~4.0.0",
21+
"thecodingmachine/graphqlite-symfony-validator-bridge" : "~4.0.0",
2122
"symfony/framework-bundle": "^4.1.9",
23+
"symfony/validator": "^4.1.9",
24+
"symfony/translation": "^4.1.9",
2225
"doctrine/annotations": "^1.6",
2326
"doctrine/cache": "^1.8",
2427
"symfony/psr-http-message-bridge": "^1",

0 commit comments

Comments
 (0)