Minor optimization in QuerydslDataFetcher

Use single MultiValueMap in method that flattens arguments vs
creating a new one at each level of recursion.

See gh-1085
This commit is contained in:
rstoyanchev
2024-12-04 11:58:00 +00:00
parent 2cafc7cc1b
commit 3921a3ec6d
3 changed files with 12 additions and 16 deletions

View File

@@ -143,33 +143,29 @@ public abstract class QuerydslDataFetcher<T> {
* @return the resulting predicate
*/
protected Predicate buildPredicate(DataFetchingEnvironment environment) {
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
QuerydslBindings bindings = new QuerydslBindings();
EntityPath<?> path = SimpleEntityPathResolver.INSTANCE.createPath(this.domainType.getType());
this.customizer.customize(bindings, path);
parameters.putAll(flatten(null, getArgumentValues(environment)));
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
addParameters(null, getArgumentValues(environment), parameters);
return BUILDER.getPredicate(this.domainType, parameters, bindings);
}
@SuppressWarnings("unchecked")
private MultiValueMap<String, Object> flatten(@Nullable String prefix, Map<String, Object> inputParameters) {
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
private void addParameters(
@Nullable String prefix, Map<String, Object> arguments, MultiValueMap<String, Object> parameters) {
for (Map.Entry<String, Object> entry : inputParameters.entrySet()) {
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
Object value = entry.getValue();
if (value instanceof Map<?, ?> nested) {
parameters.addAll(flatten(entry.getKey(), (Map<String, Object>) nested));
}
else {
List<Object> values = (value instanceof List) ? (List<Object>) value : Collections.singletonList(value);
parameters.put(((prefix != null) ? prefix + "." : "") + entry.getKey(), values);
addParameters(entry.getKey(), (Map<String, Object>) nested, parameters);
continue;
}
List<Object> values = (value instanceof List) ? (List<Object>) value : Collections.singletonList(value);
parameters.put(((prefix != null) ? prefix + "." : "") + entry.getKey(), values);
}
return parameters;
}
/**

View File

@@ -346,13 +346,13 @@ class QuerydslDataFetcherTests {
assertThat(books.get(0).getName()).isEqualTo(book1.getName());
}
@Test
@Test // gh-1081
void shouldConsiderNestedArguments() {
Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
mockRepository.saveAll(Arrays.asList(book1, book2));
String queryName = "booksByNestableCriteria";
String queryName = "booksByNestedCriteria";
Mono<ExecutionGraphQlResponse> responseMono =
graphQlSetup(queryName, QuerydslDataFetcher.builder(mockRepository).many())

View File

@@ -2,7 +2,7 @@ type Query {
bookById(id: ID): Book
booksById(id: [ID]): [Book]
books(id: ID, name: String, author: String): [Book!]!
booksByNestableCriteria(id: ID, name: String, author: AuthorCriteria): [Book!]!
booksByNestedCriteria(id: ID, name: String, author: AuthorCriteria): [Book!]!
booksByCriteria(criteria:BookCriteria): [Book]
booksByProjectedArguments(name: String, author: String): [Book]
booksByProjectedCriteria(criteria:BookCriteria): [Book]