Skip to content

Adding support for symfony validator bridge #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Resources/config/container/graphqlite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<tag name="graphql.parameter_middleware"/>
</service>

<service id="TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\AssertParameterMiddleware">
<argument type="service" id="validator.validator_factory" />
<tag name="graphql.parameter_middleware"/>
</service>

<service id="TheCodingMachine\Graphqlite\Bundle\Controller\GraphQL\LoginController" public="true">
<argument key="$firewallName">%graphqlite.security.firewall_name%</argument>
</service>
Expand Down
12 changes: 12 additions & 0 deletions Tests/Fixtures/Controller/TestGraphqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GraphQL\Error\Error;
use Porpaginas\Arrays\ArrayResult;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assertion;
use TheCodingMachine\GraphQLite\Annotations\FailWith;
use TheCodingMachine\GraphQLite\Annotations\Logged;
use TheCodingMachine\GraphQLite\Annotations\Right;
Expand All @@ -16,6 +17,8 @@
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Exceptions\GraphQLAggregateException;
use TheCodingMachine\GraphQLite\Exceptions\GraphQLException;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
use TheCodingMachine\Graphqlite\Validator\Fixtures\Types\User;

class TestGraphqlController
{
Expand Down Expand Up @@ -125,4 +128,13 @@ public function getUri(Request $request): string
{
return $request->getPathInfo();
}

/**
* @Query
* @Assert(for="email", constraint=@Assertion\Email())
*/
public function findByMail(string $email = '[email protected]'): string
{
return $email;
}
}
32 changes: 29 additions & 3 deletions Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ public function testExceptionHandler(): void
$this->assertSame(404, $response->getStatusCode());

$result = json_decode($response->getContent(), true);
var_dump($result);

//$this->assertSame('Cannot query field "me" on type "Query".', $result['errors'][0]['message']);

$this->assertSame('foo', $result['errors'][0]['message']);
$this->assertSame('bar', $result['errors'][1]['message']);
$this->assertSame('MyCat', $result['errors'][1]['extensions']['category']);
$this->assertSame('baz', $result['errors'][1]['extensions']['field']);
}

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

public function testValidation(): void
{
$kernel = new GraphqliteTestingKernel(true, 'off', true, 'off');
$kernel->boot();

$session = new Session(new MockArraySessionStorage());
$container = $kernel->getContainer();
$container->set('session', $session);

$request = Request::create('/graphql', 'POST', ['query' => '
{
findByMail(email: "notvalid")
}
']);

$response = $kernel->handle($request);

$result = json_decode($response->getContent(), true);
$errors = $result['errors'];

$this->assertSame('This value is not a valid email address.', $errors[0]['message']);
$this->assertSame('email', $errors[0]['extensions']['field']);
$this->assertSame('Validate', $errors[0]['extensions']['category']);
}

private function logIn(ContainerInterface $container)
{
// put a token into the storage so the final calls can function
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
],
"require" : {
"php" : ">=7.2",
"thecodingmachine/graphqlite" : "^4",
"thecodingmachine/graphqlite" : "~4.0.0",
"thecodingmachine/graphqlite-symfony-validator-bridge" : "~4.0.0",
"symfony/framework-bundle": "^4.1.9",
"symfony/validator": "^4.1.9",
"symfony/translation": "^4.1.9",
"doctrine/annotations": "^1.6",
"doctrine/cache": "^1.8",
"symfony/psr-http-message-bridge": "^1",
Expand Down