Sennen Desouza
Sennen Desouza

Reputation: 43

How to return custom error in GraphQL

How can I return a custom error from within the resolve function of my Laravel project when using GraphQL?

public function resolve($root, $args, $context, ResolveInfo $info)
{
    $objDefaultPage = FunnelTypeDefaultPage::where('id', $args['id'])->first();

    if (!$objDefaultPage) {
        return null; // Here I want to return some message instead of returning null
    }

    $objDefaultPage->update($args);
    return $objDefaultPage;
}

Upvotes: 4

Views: 1417

Answers (1)

Muhammad Nauman
Muhammad Nauman

Reputation: 1309

If you are using this package then it is fairly simple. If there is nothing special in the error then just return as:

return new Error('Default page not found');

Don't forget to include use GraphQL\Error\Error; Or if you want to customise it then you need to create a class DefaultPageError which should extend the GraphQL\Error\Error class and call that class instead. like:

return new DefaultPageError('Page not found');

Or you can play around more to customise it.

Upvotes: 5

Related Questions