Skip to content

Commit

Permalink
Minor optimization in QuerydslDataFetcher
Browse files Browse the repository at this point in the history
Use single MultiValueMap in method that flattens arguments vs
creating a new one at each level of recursion.

See gh-1085
  • Loading branch information
rstoyanchev committed Dec 4, 2024
1 parent 2cafc7c commit 3921a3e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,29 @@ public String getDescription() {
* @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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,13 @@ void shouldNestForSingleArgumentInputType() {
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())
Expand Down
2 changes: 1 addition & 1 deletion spring-graphql/src/test/resources/books/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 3921a3e

Please sign in to comment.