Vavr try fold want an exception in the first argument

583 Views Asked by At

I use GraphQL with Vavr.

In the first function argument, the Try.fold is waiting to raise an exception but in my case, I want to return a DataFetcherResult.

When compiling, I get the following error

Object / Required type: DataFetcherResult<Result>

when compiling the line vehicle -> DataFetcherResult.newResult().data(vehicle).build().

public DataFetcherResult<Vehicle> getVehicle() {
     return vehicleService.getVehicle()
        .fold(
            // Error because doesn't throw return an exception
            exception -> DataFetcherResult.newResult().error(new CustomGraphqlException(exception)),
            vehicle -> DataFetcherResult.newResult().data(vehicle).build()
        );
}

vehicleService.getVehicle() returns Try of Vehicle.

public Try<Vehicle> getVehicle() {
    return Try.of(() -> buildVehicle().stream().findFirst().get());
}

How I can recover from a Try<Vehicle>, a DataFetcher in the case of a success and in the case of an error ?

0

There are 0 best solutions below