Skip to content

How to modify the BatchMapping Source Object after an operation #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ShreeGiriKumar opened this issue Apr 28, 2025 · 2 comments
Open
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@ShreeGiriKumar
Copy link

ShreeGiriKumar commented Apr 28, 2025

I am trying to modify the BatchMapping source object right before sending back the response. Imagine there is some db/api exception while fetching ratings for a specific movie and I would like to send that exception info part of the Movie object. Below is a sample snippet and I would like to know if its achievable.

@QueryMapping(name = "getMovies")
public List getAllMovies() {
return Movie.getMovies();
}

@BatchMapping(typeName = "Movie", field = "ratings")
public Map<Movie, List<Rating>> rating(List<Movie> movies) {
    // Use the context to get the batch loader

    Map<Movie, List<Rating>> movieRatings = movies.stream()
            .collect(Collectors.toMap(movie -> movie, movie -> Rating.getById(movie.getId())));
    return movieRatings;
}

private Movie modify(Movie movie){
    if(movie.getId().equalsIgnoreCase("movie-1")) { //simulating an exception
        movie.setError("error while fetching ratings");
        return movie;
    }
    return movie;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 28, 2025
@bclozel
Copy link
Member

bclozel commented Apr 29, 2025

I don't see this modify method being called in this code snippet. Maybe I am missing something?

Also could you explain why the data loader isn't throwing an error in the first place?

@ShreeGiriKumar
Copy link
Author

ShreeGiriKumar commented Apr 29, 2025

@BatchMapping(typeName = "Movie", field = "ratings")
public Map<Movie, List<Rating>> rating(List<Movie> movies) {
    // Use the context to get the batch loader

    Map<Movie, List<Rating>> movieRatings = movies.stream()
            .collect(Collectors.toMap(movie -> modify(movie), movie -> Rating.getById(movie.getId())));
    return movieRatings;
}
private Movie modify(Movie movie){
    if(movie.getId().equalsIgnoreCase("movie-1")) { //simulating an exception
        movie.setError("error while fetching ratings");
        return movie;
    }
    return movie;
}

So, we have the exceptions handled in the service layer. So, the expected response be like,

id: 1
name: "abc"
error: null

id: 0
name: null
error: {
    code: 500
    message: "exception fetching ratings"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants