laravel-graphql
laravel-graphql copied to clipboard
Localization
For all built in errors (e.g. 'In field "x": Expected type "String", found "y"), can these be localized? Laravel now offers a way for packages to present localized errors in a custom folder.
The way I am handling it is but having all of them set to "String!" and using validation rules with custom messages, like:
public function args()
{
return [
'id' => [
// Do not verify type here because we want to have custom validation messages.
'type' => Type::string(),
'rules' => ['required', 'integer'],
],
'token' => [
'type' => Type::string(),
'rules' => ['required'],
],
];
}
public function validationErrorMessages($root, $args, $context)
{
return [
'id.required' => \Lang::get('validation.required', ['attribute' => 'ID']),
'id.integer' => \Lang::get('validation.integer', ['attribute' => 'ID']),
'token.required' => \Lang::get('validation.required', ['attribute' => 'token']),
];
}
I am not sure if it's the best way, but it is what I've found.
Note that for Query you also need to add:
class UserQuery extends Query
{
use ShouldValidate;
...
}
@SonarSoftware, You can see issues #266 and #261 for more info.