I have created a custom policy for a model and the policy logic works really well with a GraphQL mutation. I'm just wondering can I somehow pass my custom error message as a GraphQL response?
This is an example of a policy class:
use App\Models\MyModel;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class MyModelPolicy
{
use HandlesAuthorization;
public function update(User $user, MyModel $my_object)
{
if (!$my_object->checkSomething()) {
// Throws an exception
$this->deny('My custom error message should be delivered to the GraphQL client..');
}
return true;
}
}
But the message in the exception gets discarded:
- by Laravel 5 here: https://github.com/illuminate/auth/blob/8f0603a1d5b90c045a1ce5365ead0f0ba20fc6ce/Access/Gate.php#L279-L281
- or by Laravel 6 (and onward) here: https://github.com/illuminate/auth/blob/478cf31f02831ec45194fec5428f666f85b4f1b0/Access/Gate.php#L277