You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/05/05 14:52:20 UTC
[sling-whiteboard] branch master updated: documents query
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 3448915 documents query
3448915 is described below
commit 34489159b0877def85adb1363bb80e2d5232cbff
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed May 5 16:52:02 2021 +0200
documents query
---
remote-content-api/sample-graphql-api/README.md | 9 ++-
.../graphql/DocumentsDataFetcher.java | 93 ++++++++++++++++++++++
.../apps/samples/graphql/GQLschema.jsp | 4 +-
3 files changed, 104 insertions(+), 2 deletions(-)
diff --git a/remote-content-api/sample-graphql-api/README.md b/remote-content-api/sample-graphql-api/README.md
index 816273c..6462225 100644
--- a/remote-content-api/sample-graphql-api/README.md
+++ b/remote-content-api/sample-graphql-api/README.md
@@ -36,4 +36,11 @@ we don't really care about the details of these node types besides their names.
selectors
body
}
- }
\ No newline at end of file
+ }
+
+ {
+ documents(query:"//content/*/*") {
+ path
+ body
+ }
+ }
\ No newline at end of file
diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontentapi/graphql/DocumentsDataFetcher.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontentapi/graphql/DocumentsDataFetcher.java
new file mode 100644
index 0000000..55a9da6
--- /dev/null
+++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontentapi/graphql/DocumentsDataFetcher.java
@@ -0,0 +1,93 @@
+/*
+ * 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.remotecontentapi.graphql;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.documentmapper.api.DocumentMapper;
+import org.apache.sling.documentmapper.api.MappingTarget;
+import org.apache.sling.documentmapper.api.DocumentMapper.UrlBuilder;
+import org.apache.sling.graphql.api.SlingDataFetcher;
+import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component(service = SlingDataFetcher.class, property = {"name=samples/documents"})
+public class DocumentsDataFetcher implements SlingDataFetcher<Object> {
+
+ @Reference(target="(" + MappingTarget.TARGET_TYPE + "=map)")
+ private MappingTarget mappingTarget;
+
+ // TODO should be "summary"
+ @Reference(target="(" + DocumentMapper.ROLE + "=navigation)")
+ private DocumentMapper summaryMapper;
+
+ @Reference(target="(" + DocumentMapper.ROLE + "=content)")
+ private DocumentMapper bodyMapper;
+
+ private static final UrlBuilder URL_BUILDER = new UrlBuilder() {
+ @Override
+ public String pathToUrl(String path) {
+ return getClass().getName();
+ }
+ };
+
+ private void addDocumentData(final Map<String, Object> data, String key, Resource r, DocumentMapper mapper) {
+ final MappingTarget.TargetNode target = mappingTarget.newTargetNode();
+ mapper.map(r, target, URL_BUILDER);
+ target.close();
+ data.put(key, target.adaptTo(Map.class));
+
+ }
+
+ private Map<String, Object> toDocument(Resource r) {
+ final Map<String, Object> data = new HashMap<>();
+ data.put("path", r.getPath());
+
+ // TODO how to find out whether those fields are actually needed
+ // or how to evaluate them lazily
+ addDocumentData(data, "body", r, bodyMapper);
+ addDocumentData(data, "summary", r, summaryMapper);
+
+ return data;
+ }
+
+ @Override
+ public @Nullable Object get(@NotNull SlingDataFetcherEnvironment e) throws Exception {
+ final String query = e.getArgument("query");
+ final List<Map<String, Object>> result = new ArrayList<>();
+
+ final ResourceResolver resolver = e.getCurrentResource().getResourceResolver();
+ final Iterator<Resource> it = resolver.findResources(query, "xpath");
+ while(it.hasNext()) {
+ result.add(toDocument(it.next()));
+ }
+ return result;
+ }
+
+}
diff --git a/remote-content-api/sample-graphql-api/src/main/resources/SLING-INF/initial-content/apps/samples/graphql/GQLschema.jsp b/remote-content-api/sample-graphql-api/src/main/resources/SLING-INF/initial-content/apps/samples/graphql/GQLschema.jsp
index 352ca03..c7a2546 100644
--- a/remote-content-api/sample-graphql-api/src/main/resources/SLING-INF/initial-content/apps/samples/graphql/GQLschema.jsp
+++ b/remote-content-api/sample-graphql-api/src/main/resources/SLING-INF/initial-content/apps/samples/graphql/GQLschema.jsp
@@ -21,11 +21,13 @@
scalar Object
type Query {
- document(path : String, selectors : String) : Document @fetcher(name:"samples/document")
+ document(path : String, selectors : [String]) : Document @fetcher(name:"samples/document")
+ documents(query : String, selectors : [String]) : [Document] @fetcher(name:"samples/documents")
}
type Document {
path : String
selectors : String
body : Object
+ summary : Object
}
\ No newline at end of file