SLING-10018 - As a developer I want to access the selectionset in my fetcher context
* return proper direct fields and mapping
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 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 @@
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.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.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 @@
// 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 @@
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())));
+ }
+
+
}
}