You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2021/01/05 13:55:15 UTC

[GitHub] [sling-org-apache-sling-graphql-core] stefangrimm commented on a change in pull request #15: SLING-10018 Implement SelectionSet / SelectedField wrappers

stefangrimm commented on a change in pull request #15:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/15#discussion_r551941984



##########
File path: src/main/java/org/apache/sling/graphql/api/SelectionSet.java
##########
@@ -0,0 +1,47 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.graphql.api;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+/**
+ * Interface to wrap GraphQL DataFetchingFieldSelectionSet.
+ */
+public interface SelectionSet {
+
+    /**
+     * @return the immediate list of fields in the selection.
+     */
+    @NotNull
+    List<SelectedField> getFields();
+
+    /**
+     * @return true if the field qualified name exist.
+     */
+    boolean contains(String qualifiedName);
+
+    /**
+     * @return SelectedField for qualified name.

Review comment:
       The concept of "qualified name" should be explained (I don't want to have a look at the unit tests if I want to make use of an API, furthermore the unit tests only cover unions as a more complex use-case):
   
   - How does a "qualified name" look like?
   - How are things like unions and nested structures are reflected in the qualified name?

##########
File path: src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorTest.java
##########
@@ -188,4 +191,30 @@ public void unionFetcherTest() throws Exception {
         assertThat(json, hasJsonPath("$.data.unionFetcher.items[0].testingArgument", equalTo("1, 2, 3")));
         assertThat(json, hasJsonPath("$.data.unionFetcher.items[1].path", equalTo(resource.getPath())));
     }
+
+    @Test
+    public void selectionSetTest() throws Exception {

Review comment:
       This only covers unions. I would suggest to add a test for "normal" nested structures as well, as it would also be interesting how they would be reflected in the output, mostly if the type name would be included as well or if that's only true for `union`s. Also it would be interesting for the latter case to determine if an item of the selection set determines a union differentiator or actual data, as we couldn't simply follow the result data in the fetcher implementation in this case.

##########
File path: src/main/java/org/apache/sling/graphql/core/engine/SelectedFieldWrapper.java
##########
@@ -0,0 +1,88 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.graphql.core.engine;
+
+import graphql.language.Field;
+import graphql.language.InlineFragment;
+import graphql.language.Selection;
+import graphql.language.SelectionSet;
+import org.apache.sling.graphql.api.SelectedField;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Implement a wrapper for GraphQL SelectedField.
+ */
+public class SelectedFieldWrapper implements SelectedField {
+
+    private String name;
+    private boolean isInline;
+    private HashMap<String, SelectedField> subFieldMap = new HashMap<>();
+    private List<SelectedField> subFields;
+
+    public SelectedFieldWrapper(Selection selection) {
+        SelectionSet selectionSet = null;
+        if (selection instanceof InlineFragment) {
+            InlineFragment inline = (InlineFragment) selection;
+            this.name = inline.getTypeCondition().getName();
+            this.isInline = true;
+            selectionSet = inline.getSelectionSet();
+        }
+        if (selection instanceof Field) {
+            Field subField = (Field) selection;
+            this.name = subField.getName();
+            selectionSet = subField.getSelectionSet();
+        }
+        if (selectionSet != null) {
+            selectionSet.getSelections().forEach(s -> {
+                SelectedFieldWrapper wrappedField = new SelectedFieldWrapper(s);
+                subFieldMap.put(wrappedField.getName(), wrappedField);
+            });
+        }
+        subFields = subFieldMap.values().stream().collect(Collectors.toList());
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public List<SelectedField> getSubSelectedFields() {

Review comment:
       Typically we don't return `List`s directly (in AEM Sites). Not sure how Sling handles this.
   
   If we stay with `List`, I would at least suggest to wrap the list with `Collections.unmodifiableList()`.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org