You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2019/05/28 18:14:08 UTC

[unomi] 16/25: UNOMI-180 - Start building a new servlet that loads the SDL schema and will combine it with dynamic type registration. - Deactivated old graphql-java-servlet for the moment.

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch UNOMI-180-CXS-GRAPHQLAPI
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 79127c2f6997f6d5b15e4c77531d7cb7dfeb746d
Author: sergehuber <sh...@jahia.com>
AuthorDate: Sun May 12 22:12:14 2019 +0200

    UNOMI-180
    - Start building a new servlet that loads the SDL schema and will combine it with dynamic type registration.
    - Deactivated old graphql-java-servlet for the moment.
---
 .../unomi/graphql/internal/CDPSDLServletImpl.java  | 105 +++++++++++++++++++++
 .../{cdp-schema.sdl => cdp-schema.graphqls}        | Bin
 graphql/karaf-feature/pom.xml                      |   6 +-
 3 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CDPSDLServletImpl.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CDPSDLServletImpl.java
new file mode 100644
index 0000000..dfd1ca6
--- /dev/null
+++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CDPSDLServletImpl.java
@@ -0,0 +1,105 @@
+/*
+ * 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.unomi.graphql.internal;
+
+import graphql.schema.GraphQLSchema;
+import graphql.schema.StaticDataFetcher;
+import graphql.schema.idl.RuntimeWiring;
+import graphql.schema.idl.SchemaGenerator;
+import graphql.schema.idl.SchemaParser;
+import graphql.schema.idl.TypeDefinitionRegistry;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
+
+@Component(
+        service={javax.servlet.http.HttpServlet.class,javax.servlet.Servlet.class},
+        property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"}
+)
+public class CDPSDLServletImpl extends HttpServlet {
+
+    @Reference
+    private BundleContext bundleContext;
+
+    RuntimeWiring buildRuntimeWiring() {
+        return RuntimeWiring.newRuntimeWiring()
+                // .scalar(CustomScalar)
+                // this uses builder function lambda syntax
+                /*
+                .type("QueryType", typeWiring -> typeWiring
+                        .dataFetcher("hero", new StaticDataFetcher(StarWarsData.getArtoo()))
+                        .dataFetcher("human", StarWarsData.getHumanDataFetcher())
+                        .dataFetcher("droid", StarWarsData.getDroidDataFetcher())
+                )
+                .type("Human", typeWiring -> typeWiring
+                        .dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
+                )
+                // you can use builder syntax if you don't like the lambda syntax
+                .type("Droid", typeWiring -> typeWiring
+                        .dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
+                )
+                // or full builder syntax if that takes your fancy
+                .type(
+                        newTypeWiring("Character")
+                                .typeResolver(StarWarsData.getCharacterTypeResolver())
+                                .build()
+                )
+                */
+                .build();
+    }
+
+    @Override
+    public void init(ServletConfig config) throws ServletException {
+        super.init(config);
+
+        SchemaParser schemaParser = new SchemaParser();
+        SchemaGenerator schemaGenerator = new SchemaGenerator();
+
+        Reader schemaReader = getSchemaReader("cdp-schema.graphqls");
+        //File schemaFile2 = loadSchema("cdp-schema.graphqls");
+        //File schemaFile3 = loadSchema("cdp-schema.graphqls");
+
+        TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
+
+        // each registry is merged into the main registry
+        typeRegistry.merge(schemaParser.parse(schemaReader));
+        //typeRegistry.merge(schemaParser.parse(schemaFile2));
+        //typeRegistry.merge(schemaParser.parse(schemaFile3));
+
+        RuntimeWiring wiring = buildRuntimeWiring();
+        GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, wiring);
+    }
+
+    private Reader getSchemaReader(String resourceUrl) {
+        try {
+            return new InputStreamReader(bundleContext.getBundle().getResource(resourceUrl).openConnection().getInputStream());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+}
diff --git a/graphql/cxs-impl/src/main/resources/cdp-schema.sdl b/graphql/cxs-impl/src/main/resources/cdp-schema.graphqls
similarity index 100%
rename from graphql/cxs-impl/src/main/resources/cdp-schema.sdl
rename to graphql/cxs-impl/src/main/resources/cdp-schema.graphqls
diff --git a/graphql/karaf-feature/pom.xml b/graphql/karaf-feature/pom.xml
index c051b18..0437dba 100644
--- a/graphql/karaf-feature/pom.xml
+++ b/graphql/karaf-feature/pom.xml
@@ -82,7 +82,7 @@
             <version>1.0.2</version>
         </dependency>
 
-
+<!--
         <dependency>
             <groupId>com.graphql-java-kickstart</groupId>
             <artifactId>graphql-java-servlet</artifactId>
@@ -98,6 +98,7 @@
                 </exclusion>
             </exclusions>
         </dependency>
+-->
         <dependency>
             <groupId>com.graphql-java</groupId>
             <artifactId>graphql-java</artifactId>
@@ -109,12 +110,13 @@
                 </exclusion>
             </exclusions>
         </dependency>
+<!--
         <dependency>
             <groupId>io.github.graphql-java</groupId>
             <artifactId>graphql-java-annotations</artifactId>
             <version>${graphql.java.annotations.version}</version>
         </dependency>
-
+-->
         <dependency>
             <groupId>org.apache.unomi</groupId>
                 <artifactId>cdp-graphql-api-impl</artifactId>