Graphql-laravel: How to return errors manually

Created on 25 Aug 2017  路  8Comments  路  Source: rebing/graphql-laravel

Hello again.
Thanks for the library.
I can't find the documentation on how to manually return errors.
By default GraphQL has it's own errors it will return but what if I wanted to do something with a query or mutation and return my own error message, or just a basic error message instead of
returning null which is what I've been doing. How do I go about it? Thanks again.

Most helpful comment

Just add a field to the Type that is returned with your Mutations

public function fields()
{
...
'exception' => [
    'type' => Type::string(),
],
...
}

All 8 comments

Hello @niiapa ,
You can create a new class that extends from the main "GraphQL" and customize the "formatError" method to your need

I could well:

`
namespace App\GraphQL\Support;

use GraphQL\Error\Error;
use Rebing\GraphQL\Error\ValidationError;
use Rebing\GraphQL\GraphQL;

class ErrorCatch extends GraphQL
{

public static function formatError(Error $e)
{
    $error = [
        'message' => 'Your messages', // $e->getMessage(),
    ];

    $locations = $e->getLocations();
    if (!empty($locations)) {
        $error['locations'] = array_map(function ($loc) {
            return $loc->toArray();
        }, $locations);
    }

    $previous = $e->getPrevious();
    if ($previous && $previous instanceof ValidationError) {
        $error['validation'] = $previous->getValidatorMessages();
    }

    return $error;
}

}
`
And change the file config/graphql.php
'error_formatter' => [App\GraphQL\Support\ErrorCatch::class, 'formatError'],

I hope it works for you

Hello @emiliogrv
Thanks for the reply. Took a while to respond because I didn't have time to try it out. My aim is to return an error instead of null everytime. Is there a pre-built way to do this without overwriting extending the ErrorFormatter?

For example, say I check for password matching and they don't match, I want to basically make the result be an error so I can parse that on the frontend. Is that possible or do I have to extend the class to do that?

You have to extend the Formatter. GraphQL will always return data: null and error: {...}, when an error occurs.

@rebing I understand it will always return that info. All I want to do is to throw my own message for one error. Not for the entire API. Maybe I'm not explaining this right.

I have a Model that I'm saving information to in pieces (it's across several pages on the frontend). I make a change, I save. At the end, I want to "publish" my Model by checking if all the other columns are filled like date, description, and if they are, set the publish column to true. If they aren't, that's where I want to throw an error that says maybe,

date is not set or description is not set. Anything to alert my frontend that certain fields are not present. Maybe I'm overthinking but I've been stuck in a rut. Thanks again for the help.

To me it seems like the exception should be thrown on the business logic (API) side. This way you could swap GraphQL with RESTful or other backend endpoint systems and still keep the exception logic intact.

Alternatively, create an exception field on your return type?

I'll like to try the exception field on my return type thing. Know any links/resources to help with this? Thanks.

Just add a field to the Type that is returned with your Mutations

public function fields()
{
...
'exception' => [
    'type' => Type::string(),
],
...
}

Oh yeah lmao. I was overthinking this. Lmao yeah. I feel so dumb rn. Thanks.

EditEdit: Sorry. Works now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirgy picture kirgy  路  8Comments

hemratna picture hemratna  路  3Comments

AdrianCarreno picture AdrianCarreno  路  5Comments

nozols picture nozols  路  8Comments

dongkaipo picture dongkaipo  路  4Comments