Skip to content

Domain logic error handling for UI display #1415

@Christian-Vasilev

Description

@Christian-Vasilev

I'm trying to wrap my head around for 2 days now and I can't come up with a good solution that follows good practices.

I have the following Query Class.


namespace Ladypopular\api\graphql\App;

use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use Ladypopular\api\graphql\App\Resolver\Event\MapResolver;

class Query extends ObjectType
{
    public function __construct()
    {
        $config = [
            'name' => 'Query',
            'fields' => [
                'map' => [
                    'type' => Types::map(),
                    'description' => 'All information needed for the event',
                    'args' => [
                        'id' => new NonNull(Types::id()),
                    ],
                    'fields' => [
                        'userErrors' => [
                            'type' => Types::listOf(Types::int()),
                            'description' => 'User domain logic errors'
                        ]
                    ],
                    'resolve' => static fn ($rootValue, array $args, \App $context): \Closure
                        => (MapResolver::getInstance($context))->getEventById($args['id']),
                    'interface' => [Types::userErrors(),]
                ]
            ],
        ];
        parent::__construct($config);
    }
}

I'm using the Standart Server and trying to stop the execution of the request in the first resolve if there is a doma
in logic error. I don't want to continue down the child resolvers and load data. The main problem is that the information in the child resolvers is dependent of the main resolver (Like you can see here when I'm calling getEventById. If I have no found event with this ID I want to stop the request here and return response.

Screenshot 2023-07-25 at 14 47 18

I have a response already created, but I can't stop the execution of the other resolvers. So the following questions popup in my head.

  1. Is my approach wrong?
  2. Should I throw exceptions and let the StandartServer handle them, by using custom Error Handler and Formatter? I really don't like this way. I want my domain logic errors to be separated by the application errors.
  3. How do you handle such logic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions