diff --git a/README.md b/README.md index 6d57a207..81305cef 100644 --- a/README.md +++ b/README.md @@ -119,3 +119,53 @@ The [OsgiGraphQLServlet](https://github.com/graphql-java/graphql-java-servlet/bl * [GraphQLTypesProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLTypesProvider.java): Provides type information to the GraphQL schema. * [ExecutionStrategyProvider](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/ExecutionStrategyProvider.java): Provides an execution strategy for running each query. * [GraphQLContextBuilder](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLContextBuilder.java): Builds a context for running each query. + +## Examples + +You can now find some example on how to use graphql-java-servlet. + +### OSGi Examples + +#### Requirements + +The OSGi examples use Maven as a build tool because it requires plugins that are not (yet) available for Gradle. +Therefore you will need Maven 3.2+. + +#### Building & running the OSGi examples + +You can build the OSGi examples sub-projects by simply executing the following command from the examples/osgi directory: + + mvn clean install + +This will generate a complete Apache Karaf distribution in the following files: + + examples/osgi/apache-karaf-package/target/graphql-java-servlet-osgi-examples-apache-karaf-package-VERSION.tar.gz(.zip) + +You can simply uncompress this file and launch the OSGi server using the command from the uncompressed directory: + + bin/karaf + +You should then be able to access the GraphQL endpoint at the following URL once the server is started: + + http://localhost:8181/graphql/schema.json + +If you see the JSON result of an introspection query, then all is ok. If not, check the data/log/karaf.log file for +any errors. + +We also provide a script file to do all of the building and running at once (only for Linux / MacOS ): + + ./buildAndRun.sh + +#### Deploying inside Apache Karaf server + +You can use the graphql-java-servlet as part of an Apache Karaf feature, as you can see in the example project here: +* [pom.xml](examples/osgi/apache-karaf-feature/pom.xml) + +And here is a sample src/main/feature/feature.xml file to add some dependencies on other features: +* [feature.xml](examples/osgi/apache-karaf-feature/src/main/feature/feature.xml) + +#### Example GraphQL provider implementation + +Here's an example of a GraphQL provider that implements three interfaces at the same time. + +* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java) diff --git a/examples/osgi/apache-karaf-feature/pom.xml b/examples/osgi/apache-karaf-feature/pom.xml new file mode 100644 index 00000000..1df8843a --- /dev/null +++ b/examples/osgi/apache-karaf-feature/pom.xml @@ -0,0 +1,94 @@ + + + 4.0.0 + + com.graphql-java + graphql-java-servlet-osgi-examples + 3.0.1 + + + graphql-java-servlet-osgi-examples-karaf-feature + feature + + + + + com.fasterxml.jackson.core + jackson-core + 2.8.4 + + + com.fasterxml.jackson.core + jackson-annotations + 2.8.4 + + + com.fasterxml.jackson.core + jackson-databind + 2.8.4 + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + 2.8.4 + + + com.google.guava + guava + 20.0 + + + commons-fileupload + commons-fileupload + 1.3.1 + + + org.antlr + antlr4-runtime + 4.5.1 + + + + com.graphql-java + graphql-java-servlet + ${graphql.java.servlet.version} + + + com.graphql-java + graphql-java + ${graphql.java.version} + + + com.graphql-java + graphql-java-annotations + 0.13.1 + + + + com.graphql-java + graphql-java-servlet-osgi-examples-providers + ${project.version} + + + + + + + + org.apache.karaf.tooling + karaf-maven-plugin + 4.0.8 + true + + 80 + true + true + + + + + + + \ No newline at end of file diff --git a/examples/osgi/apache-karaf-feature/src/main/feature/feature.xml b/examples/osgi/apache-karaf-feature/src/main/feature/feature.xml new file mode 100644 index 00000000..c85c9ebd --- /dev/null +++ b/examples/osgi/apache-karaf-feature/src/main/feature/feature.xml @@ -0,0 +1,7 @@ + + + + scr + war + + diff --git a/examples/osgi/apache-karaf-package/pom.xml b/examples/osgi/apache-karaf-package/pom.xml new file mode 100644 index 00000000..85b706ed --- /dev/null +++ b/examples/osgi/apache-karaf-package/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + com.graphql-java + graphql-java-servlet-osgi-examples + 3.0.1 + + + graphql-java-servlet-osgi-examples-apache-karaf-package + karaf-assembly + + + + + org.apache.karaf.features + framework + ${karaf.version} + kar + + + + org.apache.karaf.features + standard + ${karaf.version} + features + xml + runtime + + + + org.apache.karaf.features + enterprise + ${karaf.version} + features + xml + runtime + + + + com.graphql-java + graphql-java-servlet-osgi-examples-karaf-feature + ${project.version} + features + xml + runtime + + + + + + + + src/main/resources + false + + **/* + + + + src/main/filtered-resources + true + + **/* + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + process-resources + + resources + + + + + + org.apache.karaf.tooling + karaf-maven-plugin + ${karaf.version} + true + + + + minimal + wrapper + graphql-java-servlet-osgi-examples-karaf-feature + + + + + + + + \ No newline at end of file diff --git a/examples/osgi/buildAndRun.sh b/examples/osgi/buildAndRun.sh new file mode 100755 index 00000000..582379e4 --- /dev/null +++ b/examples/osgi/buildAndRun.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +mvn clean install +pushd apache-karaf-package/target +tar zxvf graphql-java-servlet-osgi-examples-apache-karaf-package-3.0.1.tar.gz +cd graphql-java-servlet-osgi-examples-apache-karaf-package-3.0.1/bin +./karaf debug +popd \ No newline at end of file diff --git a/examples/osgi/pom.xml b/examples/osgi/pom.xml new file mode 100644 index 00000000..468780b0 --- /dev/null +++ b/examples/osgi/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.graphql-java + graphql-java-servlet-osgi-examples + 3.0.1 + pom + + + 4.1.1 + 3.0.1 + 2.4.0 + + + + providers + apache-karaf-feature + apache-karaf-package + + + \ No newline at end of file diff --git a/examples/osgi/providers/pom.xml b/examples/osgi/providers/pom.xml new file mode 100644 index 00000000..91ca964f --- /dev/null +++ b/examples/osgi/providers/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + com.graphql-java + graphql-java-servlet-osgi-examples + 3.0.1 + + + graphql-java-servlet-osgi-examples-providers + bundle + + + + com.graphql-java + graphql-java-servlet + ${graphql.java.servlet.version} + provided + + + com.graphql-java + graphql-java + ${graphql.java.version} + provided + + + org.osgi + osgi.enterprise + 6.0.0 + provided + + + org.apache.felix + org.apache.felix.scr.ds-annotations + 1.2.4 + provided + + + + + + + org.apache.felix + maven-bundle-plugin + 3.3.0 + true + + + + + + + + + \ No newline at end of file diff --git a/examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java b/examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java new file mode 100644 index 00000000..129c38bc --- /dev/null +++ b/examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java @@ -0,0 +1,42 @@ +package graphql.servlet.examples.osgi; + +import graphql.schema.GraphQLFieldDefinition; +import graphql.schema.GraphQLType; +import graphql.servlet.GraphQLMutationProvider; +import graphql.servlet.GraphQLQueryProvider; +import graphql.servlet.GraphQLTypesProvider; +import org.osgi.service.component.annotations.Component; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static graphql.Scalars.GraphQLString; +import static graphql.schema.GraphQLFieldDefinition.*; + +@Component( + name="ExampleGraphQLProvider", + immediate=true +) +public class ExampleGraphQLProvider implements GraphQLQueryProvider, GraphQLMutationProvider, GraphQLTypesProvider { + + public Collection getQueries() { + List fieldDefinitions = new ArrayList(); + fieldDefinitions.add(newFieldDefinition() + .type(GraphQLString) + .name("hello") + .staticValue("world") + .build()); + return fieldDefinitions; + } + + public Collection getMutations() { + return new ArrayList(); + } + + public Collection getTypes() { + + List types = new ArrayList(); + return types; + } +} diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 85063e7e..8d0aa6b5 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -39,8 +39,8 @@ @Slf4j @Component( - service=javax.servlet.http.HttpServlet.class, - property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"} + service={javax.servlet.http.HttpServlet.class,javax.servlet.Servlet.class}, + property = {"osgi.http.whiteboard.servlet.pattern=/graphql/*", "jmx.objectname=graphql.servlet:type=graphql"} ) public class OsgiGraphQLServlet extends GraphQLServlet {