Skip to content

transform method in DataFetcherResult is unable to modify/clear existing errors #2717

@rabbitvirus

Description

@rabbitvirus

Hello,

DataFetcherResult class has nice transform method that basically creates a new Builder from the existing object and allows us to modify its content using nice functional Consumer<Builder<T>>. We can replace data and localContext, however we cannot replace errors.

I found this problem in postprocessing in DGS framework. I wanted to add missing paths from context to errors if they were not set. I tried to use the transform method and I quickly found out that I cannot replace or modify errors list. Also, the list is immutable, so I cannot use a getter method and somehow modify it. What is more, errors stored there are also immutable.

However, it is still possible to manually recreate a result with replaced errors, for example like this:

DataFetcherResult<?> newResult = DataFetcherResult.newResult()
                .data(previousResult.getData())
                .localContext(previousResult.getLocalContext())
                .errors(newErrors)
                .build();

But, if anything will be added to the implementation of DatafetcherResult in future, one needs to remeber to add it there to not lose any important info from the result.

My proposition is to add a simple clearErrors method to the existing implementation of Builder<T>. It is a new method, so backwards compatibility is not broken. The method basically clears a list underneath. When someone calls error(...) or errors(...) methods, new errors will be added to an empty list.

With that method it will be possible to use transform method for the described purpose.

Here is my proposition:
#2716

I am looking forward to any feedback from you :-)

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