You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2021/01/12 09:52:17 UTC
[sling-org-apache-sling-graphql-core] branch master updated:
SLING-10018 - As a developer I want to access the selectionset in my
fetcher context
This is an automated email from the ASF dual-hosted git repository.
radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git
The following commit(s) were added to refs/heads/master by this push:
new 6485bba SLING-10018 - As a developer I want to access the selectionset in my fetcher context
6485bba is described below
commit 6485bbab2fe1ad2a37bab249e729fdf04dc472f7
Author: Thierry Ygé <ty...@adobe.com>
AuthorDate: Tue Jan 12 10:52:08 2021 +0100
SLING-10018 - As a developer I want to access the selectionset in my fetcher context
* return proper direct fields and mapping
---
.../graphql/core/engine/SelectionSetWrapper.java | 12 +++++++--
.../core/engine/DefaultQueryExecutorTest.java | 30 ++++++++++++++++++++++
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/sling/graphql/core/engine/SelectionSetWrapper.java b/src/main/java/org/apache/sling/graphql/core/engine/SelectionSetWrapper.java
index 40f0bfd..7db2229 100644
--- a/src/main/java/org/apache/sling/graphql/core/engine/SelectionSetWrapper.java
+++ b/src/main/java/org/apache/sling/graphql/core/engine/SelectionSetWrapper.java
@@ -41,7 +41,13 @@ public class SelectionSetWrapper implements SelectionSet {
public SelectionSetWrapper(@Nullable DataFetchingFieldSelectionSet selectionSet) {
if (selectionSet != null) {
- selectionSet.get().getSubFieldsList().forEach(s -> fields.add(new SelectedFieldWrapper(s.getSingleField())));
+ selectionSet.get().getSubFields().forEach((k, v) -> {
+ SelectedFieldWrapper selectedField = new SelectedFieldWrapper(v.getSingleField());
+ fieldsMap.put(k, selectedField);
+ if (!k.contains("/")) {
+ fields.add(selectedField);
+ }
+ });
initFlatMap(fields, "");
}
}
@@ -49,7 +55,9 @@ public class SelectionSetWrapper implements SelectionSet {
private void initFlatMap(List<SelectedField> parentList, String qualifiedPath) {
parentList.forEach(s -> {
String qualifiedName = qualifiedPath + s.getName();
- fieldsMap.put(qualifiedName, s);
+ if (!fieldsMap.containsKey(qualifiedName)) {
+ fieldsMap.put(qualifiedName, s);
+ }
initFlatMap(s.getSubSelectedFields(), qualifiedName + "/");
});
}
diff --git a/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorTest.java b/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorTest.java
index 130c942..228e6c6 100644
--- a/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorTest.java
+++ b/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorTest.java
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Objects;
import org.apache.sling.graphql.api.SchemaProvider;
+import org.apache.sling.graphql.api.SelectedField;
import org.apache.sling.graphql.api.SelectionSet;
import org.apache.sling.graphql.api.SlingDataFetcher;
import org.apache.sling.graphql.api.SlingGraphQLException;
@@ -50,6 +51,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@@ -205,6 +207,19 @@ public class DefaultQueryExecutorTest extends ResourceQueryTestBase {
// Access the computed SelectionSet
SelectionSet selectionSet = echoDataFetcher.getSelectionSet();
+ assertEquals(5, selectionSet.getFields().size());
+
+ String[] expectedFieldNames = new String[] {
+ "boolValue",
+ "resourcePath",
+ "aTest",
+ "items"
+ };
+ final List<SelectedField> selectionSetFields = selectionSet.getFields();
+ for (String expectedFieldname : expectedFieldNames) {
+ assertTrue(selectionSetFields.stream().anyMatch(f -> expectedFieldname.equals(f.getName())));
+ }
+
// Assert it contains the expected results
String[] expectedQualifiedName = new String[] {
"boolValue",
@@ -253,5 +268,20 @@ public class DefaultQueryExecutorTest extends ResourceQueryTestBase {
for (String expectedInlineQN : expectedInlineQNs) {
assertTrue(Objects.requireNonNull(selectionSet.get(expectedInlineQN)).isInline());
}
+
+ String[] expectedSubFieldNames = new String[] {
+ "test",
+ "boolValue",
+ "resourcePath"
+ };
+
+ SelectedField allTests = selectionSet.get("allTests");
+ assert allTests != null;
+ List<SelectedField> subSelectedFields = allTests.getSubSelectedFields();
+ for (String expectedSubFieldname : expectedSubFieldNames) {
+ assertTrue(subSelectedFields.stream().anyMatch(f -> expectedSubFieldname.equals(f.getName())));
+ }
+
+
}
}