From 3921a3ec6d0294fff59437fd68daf30625e26d29 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 4 Dec 2024 11:58:00 +0000 Subject: [PATCH] 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 --- .../data/query/QuerydslDataFetcher.java | 22 ++++++++----------- .../data/query/QuerydslDataFetcherTests.java | 4 ++-- .../src/test/resources/books/schema.graphqls | 2 +- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/QuerydslDataFetcher.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/QuerydslDataFetcher.java index 7d0c5852..429ddf24 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/QuerydslDataFetcher.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/QuerydslDataFetcher.java @@ -143,33 +143,29 @@ public abstract class QuerydslDataFetcher { * @return the resulting predicate */ protected Predicate buildPredicate(DataFetchingEnvironment environment) { - MultiValueMap 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 parameters = new LinkedMultiValueMap<>(); + addParameters(null, getArgumentValues(environment), parameters); return BUILDER.getPredicate(this.domainType, parameters, bindings); } @SuppressWarnings("unchecked") - private MultiValueMap flatten(@Nullable String prefix, Map inputParameters) { - MultiValueMap parameters = new LinkedMultiValueMap<>(); + private void addParameters( + @Nullable String prefix, Map arguments, MultiValueMap parameters) { - for (Map.Entry entry : inputParameters.entrySet()) { + for (Map.Entry entry : arguments.entrySet()) { Object value = entry.getValue(); if (value instanceof Map nested) { - parameters.addAll(flatten(entry.getKey(), (Map) nested)); - } - else { - List values = (value instanceof List) ? (List) value : Collections.singletonList(value); - parameters.put(((prefix != null) ? prefix + "." : "") + entry.getKey(), values); + addParameters(entry.getKey(), (Map) nested, parameters); + continue; } + List values = (value instanceof List) ? (List) value : Collections.singletonList(value); + parameters.put(((prefix != null) ? prefix + "." : "") + entry.getKey(), values); } - - return parameters; } /** diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/query/QuerydslDataFetcherTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/query/QuerydslDataFetcherTests.java index e3484f5f..4f7c4f28 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/query/QuerydslDataFetcherTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/query/QuerydslDataFetcherTests.java @@ -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 responseMono = graphQlSetup(queryName, QuerydslDataFetcher.builder(mockRepository).many()) diff --git a/spring-graphql/src/test/resources/books/schema.graphqls b/spring-graphql/src/test/resources/books/schema.graphqls index 9501ce69..052960fa 100644 --- a/spring-graphql/src/test/resources/books/schema.graphqls +++ b/spring-graphql/src/test/resources/books/schema.graphqls @@ -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]