besmartand-pro/graphqlite-symfony-validator-bridge
Bridge package connecting Symfony Validator with GraphQLite, enabling automatic validation of GraphQL input/arguments using Symfony constraints and returning structured validation errors in GraphQL responses. Suitable for Symfony apps using GraphQLite.
@Assert\Email) into GraphQL schemas. This is a strong fit for projects already using Symfony or Laravel, where validation logic is often centralized via annotations or YAML/XML configurations.symfony/validator) makes this feasible. However, Laravel-specific validation tools (e.g., Laravel’s Form Request validation) may introduce redundancy or require additional abstraction layers.symfony/validator and symfony/dependency-injection, which are not native to Laravel. This introduces:
Validator interface, which may conflict with Laravel’s built-in validator.ValidationException and convert it to GraphQL errors (e.g., using GraphQLValidationError). This requires:
graphql-php, rebind/laravel-graphql) may not natively support Symfony annotations. Workarounds include:
laravel-validator.@Cache decorator)?Unique, Exists)?symfony/validator is already included in Laravel). Reduces duplication if the team uses Symfony Validator for REST APIs.Validator::make() in resolvers (simpler, but less declarative).Assess Current Validation:
Phase 1: Proof of Concept
@Assert\Email for an email field).// app/GraphQL/Mutations/CreateUser.php
use Besmartand\GraphQLite\SymfonyValidatorBridge\ValidatorBridge;
class CreateUser implements Mutation
{
protected $validator;
public function __construct(ValidatorBridge $validator)
{
$this->validator = $validator;
}
public function resolve($root, $args)
{
$errors = $this->validator->validate($args, [
'email' => ['NotBlank', 'Email'],
'password' => ['NotBlank', 'Length' => ['min' => 8]],
]);
if ($errors) {
throw new GraphQLValidationError($errors);
}
// Business logic...
}
}
Phase 2: Schema-Driven Validation
use GraphQL\Type\Definition\InputType;
use Besmartand\GraphQLite\SymfonyValidatorBridge\Annotation\ValidatedInput;
#[ValidatedInput([
'email' => ['NotBlank', 'Email'],
'password' => ['NotBlank', 'Length' => ['min' => 8]],
])]
class UserInput implements InputType
{
// ...
}
Phase 3: Error Handling Standardization
class GraphQLValidationError extends \GraphQL\Error\Error
{
public function __construct(array $errors)
{
$message = 'Validation failed';
$extensions = [
'errors' => array_map(function ($error) {
return [
'field' => $error['propertyPath'] ?? null,
'message' => $error['message'],
];
}, $errors),
];
parent::__construct($message, [], [], $extensions);
}
}
Phase 4: Optimization
ConstraintValidator caching).How can I help you explore Laravel packages today?