From 4bff85b2f02227ff1b401636b5dde9d20ecf4d64 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 13 Apr 2017 15:57:55 +0200 Subject: [PATCH 01/10] Fix OSGi service registration. --- src/main/java/graphql/servlet/OsgiGraphQLServlet.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 19c7cbdd..6e8d3474 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -38,7 +38,10 @@ import static graphql.schema.GraphQLSchema.newSchema; @Slf4j -@Component(property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"}) +@Component( + service=javax.servlet.http.HttpServlet.class, + property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"} +) public class OsgiGraphQLServlet extends GraphQLServlet { private List queryProviders = new ArrayList<>(); From e4e89ba90b40ef8506ad7a0d3ce598a359aa38c0 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 20 Apr 2017 14:30:29 +0200 Subject: [PATCH 02/10] Breaking changes: - Renaming the root query and mutation types from "query" and "mutation" to "Query" and "Mutation" to follow object type naming conventions - Modified the GraphQLQueryProvider implementation to make the old API deprecated and replace with a collection of field definitions, making this aligned with the GraphQLMutationProvider interface. This makes it possible to define data fetcher for the root object type fields. - Added JavaDoc documentation to the GraphQLQueryProvider interface --- .../graphql/servlet/GraphQLQueryProvider.java | 30 +++++++++++++++++-- .../graphql/servlet/OsgiGraphQLServlet.java | 24 ++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLQueryProvider.java b/src/main/java/graphql/servlet/GraphQLQueryProvider.java index c8cc8a13..2eefb055 100644 --- a/src/main/java/graphql/servlet/GraphQLQueryProvider.java +++ b/src/main/java/graphql/servlet/GraphQLQueryProvider.java @@ -14,11 +14,37 @@ */ package graphql.servlet; +import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLObjectType; +import java.util.Collection; + +/** + * This interface is used by OSGi bundles to plugin new field into the root query type + */ public interface GraphQLQueryProvider { - GraphQLObjectType getQuery(); - Object context(); + + /** + * @return a collection of field definitions that will be added to the root query type. + */ + Collection getQueryFieldDefinitions(); + + /** + * @deprecated use query field definitions instead + * @return a GraphQL object type to add to the root query type + */ + default GraphQLObjectType getQuery() { return null; } + + /** + * @deprecated use query field definitions instead + * @return an object that will be used as a staticValue for the root query type field + */ + default Object context() { return null; } + + /** + * @deprecated use query field definitions instead + * @return the name to use for the field for this query provider in the root query type + */ default String getName() { return getQuery().getName(); } diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 6e8d3474..cc9b408d 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -15,6 +15,7 @@ package graphql.servlet; import graphql.execution.ExecutionStrategy; +import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import graphql.schema.GraphQLType; @@ -52,16 +53,23 @@ public class OsgiGraphQLServlet extends GraphQLServlet { private GraphQLSchema readOnlySchema; protected void updateSchema() { - GraphQLObjectType.Builder object = newObject().name("query"); + GraphQLObjectType.Builder object = newObject().name("Query").description("Root query type"); for (GraphQLQueryProvider provider : queryProviders) { GraphQLObjectType query = provider.getQuery(); - object.field(newFieldDefinition(). - type(query). - staticValue(provider.context()). - name(provider.getName()). - description(query.getDescription()). - build()); + if (query != null) { + object.field(newFieldDefinition(). + type(query). + staticValue(provider.context()). + name(provider.getName()). + description(query.getDescription()). + build()); + } + if (provider.getQueryFieldDefinitions() != null && provider.getQueryFieldDefinitions().size() > 0) { + for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueryFieldDefinitions()) { + object.field(graphQLFieldDefinition); + } + } } Set types = new HashSet<>(); @@ -74,7 +82,7 @@ protected void updateSchema() { if (mutationProviders.isEmpty()) { schema = readOnlySchema; } else { - GraphQLObjectType.Builder mutationObject = newObject().name("mutation"); + GraphQLObjectType.Builder mutationObject = newObject().name("Mutation").description("Root mutation type"); for (GraphQLMutationProvider provider : mutationProviders) { provider.getMutations().forEach(mutationObject::field); From c070f2196355ad2eefa6781c0d05d5fe645f3c4e Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 20 Apr 2017 14:33:09 +0200 Subject: [PATCH 03/10] Improve migration documentation --- src/main/java/graphql/servlet/GraphQLQueryProvider.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/graphql/servlet/GraphQLQueryProvider.java b/src/main/java/graphql/servlet/GraphQLQueryProvider.java index 2eefb055..b0628876 100644 --- a/src/main/java/graphql/servlet/GraphQLQueryProvider.java +++ b/src/main/java/graphql/servlet/GraphQLQueryProvider.java @@ -25,7 +25,8 @@ public interface GraphQLQueryProvider { /** - * @return a collection of field definitions that will be added to the root query type. + * @return a collection of field definitions that will be added to the root query type. It is allowed to return null + * or an empty array (for example for migration purposes from the old API to this one). */ Collection getQueryFieldDefinitions(); From ad199cc2ee121f2757bc848aad7e1b347a7f68c1 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 20 Apr 2017 14:46:53 +0200 Subject: [PATCH 04/10] Make new method optional as this breaks automated tests. --- src/main/java/graphql/servlet/GraphQLQueryProvider.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLQueryProvider.java b/src/main/java/graphql/servlet/GraphQLQueryProvider.java index b0628876..133ce54d 100644 --- a/src/main/java/graphql/servlet/GraphQLQueryProvider.java +++ b/src/main/java/graphql/servlet/GraphQLQueryProvider.java @@ -25,10 +25,9 @@ public interface GraphQLQueryProvider { /** - * @return a collection of field definitions that will be added to the root query type. It is allowed to return null - * or an empty array (for example for migration purposes from the old API to this one). + * @return a collection of field definitions that will be added to the root query type. */ - Collection getQueryFieldDefinitions(); + default Collection getQueryFieldDefinitions() { return null; } /** * @deprecated use query field definitions instead From 5d4dc1487d6260581016dcdcb85a8896b61e7e96 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 20 Apr 2017 17:25:18 +0200 Subject: [PATCH 05/10] - Remove all deprecated methods - Modify tests to use new field definition method --- .../graphql/servlet/GraphQLQueryProvider.java | 22 +---------------- .../graphql/servlet/OsgiGraphQLServlet.java | 10 -------- .../servlet/GraphQLVariablesSpec.groovy | 24 +++++++++++-------- .../servlet/OsgiGraphQLServletSpec.groovy | 21 ++++++++-------- 4 files changed, 26 insertions(+), 51 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLQueryProvider.java b/src/main/java/graphql/servlet/GraphQLQueryProvider.java index 133ce54d..1b07da25 100644 --- a/src/main/java/graphql/servlet/GraphQLQueryProvider.java +++ b/src/main/java/graphql/servlet/GraphQLQueryProvider.java @@ -15,7 +15,6 @@ package graphql.servlet; import graphql.schema.GraphQLFieldDefinition; -import graphql.schema.GraphQLObjectType; import java.util.Collection; @@ -27,25 +26,6 @@ public interface GraphQLQueryProvider { /** * @return a collection of field definitions that will be added to the root query type. */ - default Collection getQueryFieldDefinitions() { return null; } + Collection getQueryFieldDefinitions(); - /** - * @deprecated use query field definitions instead - * @return a GraphQL object type to add to the root query type - */ - default GraphQLObjectType getQuery() { return null; } - - /** - * @deprecated use query field definitions instead - * @return an object that will be used as a staticValue for the root query type field - */ - default Object context() { return null; } - - /** - * @deprecated use query field definitions instead - * @return the name to use for the field for this query provider in the root query type - */ - default String getName() { - return getQuery().getName(); - } } diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index cc9b408d..7ea0534d 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -34,7 +34,6 @@ import java.util.Optional; import java.util.Set; -import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; import static graphql.schema.GraphQLSchema.newSchema; @@ -56,15 +55,6 @@ protected void updateSchema() { GraphQLObjectType.Builder object = newObject().name("Query").description("Root query type"); for (GraphQLQueryProvider provider : queryProviders) { - GraphQLObjectType query = provider.getQuery(); - if (query != null) { - object.field(newFieldDefinition(). - type(query). - staticValue(provider.context()). - name(provider.getName()). - description(query.getDescription()). - build()); - } if (provider.getQueryFieldDefinitions() != null && provider.getQueryFieldDefinitions().size() > 0) { for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueryFieldDefinitions()) { object.field(graphQLFieldDefinition); diff --git a/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy b/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy index a30aea54..895e5eb5 100644 --- a/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy +++ b/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy @@ -17,15 +17,29 @@ package graphql.servlet import graphql.annotations.GraphQLAnnotations import graphql.annotations.GraphQLField import graphql.annotations.GraphQLName +import graphql.schema.GraphQLFieldDefinition import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLSchema import lombok.SneakyThrows import spock.lang.Specification +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition + class GraphQLVariablesSpec extends Specification { static class ComplexQueryProvider implements GraphQLQueryProvider { + @Override + Collection getQueryFieldDefinitions() { + List fieldDefinitions = new ArrayList<>(); + fieldDefinitions.add(newFieldDefinition() + .name("data") + .type(GraphQLAnnotations.object(DataQuery.class)) + .staticValue(new DataQuery()) + .build()); + return fieldDefinitions; + } + static class Data { @GraphQLField String field1 @@ -48,16 +62,6 @@ class GraphQLVariablesSpec extends Specification { } } - @Override - @SneakyThrows - GraphQLObjectType getQuery() { - return GraphQLAnnotations.object(DataQuery.class) - } - - @Override - Object context() { - return new DataQuery() - } } GraphQLSchema schema diff --git a/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy b/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy index 454ca199..ed13c449 100644 --- a/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy +++ b/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy @@ -29,22 +29,23 @@ class OsgiGraphQLServletSpec extends Specification { static class TestQueryProvider implements GraphQLQueryProvider { + @Override + Collection getQueryFieldDefinitions() { + List fieldDefinitions = new ArrayList<>(); + fieldDefinitions.add(newFieldDefinition() + .name("query") + .type(GraphQLAnnotations.object(Query.class)) + .staticValue(new Query()) + .build()); + return fieldDefinitions; + } + @GraphQLName("query") static class Query { @GraphQLField public String field; } - @Override - @SneakyThrows - GraphQLObjectType getQuery() { - return GraphQLAnnotations.object(Query.class); - } - - @Override - Object context() { - return new Query(); - } } def "query provider adds query objects"() { From c7fa1d5d894cb1d4eb8db6244d248f3fcd4955ef Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 20 Apr 2017 17:33:04 +0200 Subject: [PATCH 06/10] Rename the method, for consistency with the mutation provider --- src/main/java/graphql/servlet/GraphQLQueryProvider.java | 2 +- src/main/java/graphql/servlet/OsgiGraphQLServlet.java | 4 ++-- src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy | 2 +- src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLQueryProvider.java b/src/main/java/graphql/servlet/GraphQLQueryProvider.java index 1b07da25..54f46e10 100644 --- a/src/main/java/graphql/servlet/GraphQLQueryProvider.java +++ b/src/main/java/graphql/servlet/GraphQLQueryProvider.java @@ -26,6 +26,6 @@ public interface GraphQLQueryProvider { /** * @return a collection of field definitions that will be added to the root query type. */ - Collection getQueryFieldDefinitions(); + Collection getQueries(); } diff --git a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java index 7ea0534d..85063e7e 100644 --- a/src/main/java/graphql/servlet/OsgiGraphQLServlet.java +++ b/src/main/java/graphql/servlet/OsgiGraphQLServlet.java @@ -55,8 +55,8 @@ protected void updateSchema() { GraphQLObjectType.Builder object = newObject().name("Query").description("Root query type"); for (GraphQLQueryProvider provider : queryProviders) { - if (provider.getQueryFieldDefinitions() != null && provider.getQueryFieldDefinitions().size() > 0) { - for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueryFieldDefinitions()) { + if (provider.getQueries() != null && provider.getQueries().size() > 0) { + for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueries()) { object.field(graphQLFieldDefinition); } } diff --git a/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy b/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy index 895e5eb5..985c216d 100644 --- a/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy +++ b/src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy @@ -30,7 +30,7 @@ class GraphQLVariablesSpec extends Specification { static class ComplexQueryProvider implements GraphQLQueryProvider { @Override - Collection getQueryFieldDefinitions() { + Collection getQueries() { List fieldDefinitions = new ArrayList<>(); fieldDefinitions.add(newFieldDefinition() .name("data") diff --git a/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy b/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy index ed13c449..5e9084ad 100644 --- a/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy +++ b/src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy @@ -30,7 +30,7 @@ class OsgiGraphQLServletSpec extends Specification { static class TestQueryProvider implements GraphQLQueryProvider { @Override - Collection getQueryFieldDefinitions() { + Collection getQueries() { List fieldDefinitions = new ArrayList<>(); fieldDefinitions.add(newFieldDefinition() .name("query") From b9f9c7dfe9bfe1421f3b4175f6247f4580a5ecfc Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Fri, 21 Apr 2017 11:26:07 +0200 Subject: [PATCH 07/10] Added documentation on how to deploy with an Apache Karaf feature, as well as provide an example of a GraphQL provider --- README.md | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/README.md b/README.md index 6d57a207..f02cb35d 100644 --- a/README.md +++ b/README.md @@ -119,3 +119,154 @@ 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. + +### Deploying using an Apache Karaf feature + +You can use the graphql-java-servlet as part of an Apache Karaf feature, which makes it easy to setup all the proper +dependencies. Here's an example pom.xml file to setup the Karaf feature: + +```xml + + + 4.0.0 + feature + + graphql-java-servlet-features + + + + + 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 + + + + + + + + org.apache.karaf.tooling + karaf-maven-plugin + 4.0.8 + true + + 80 + true + true + + + + + + + +``` + +And here is a sample src/main/feature/feature.xml file to add some dependencies on other features: + +```xml + + + + scr + war + + +``` + +### Example GraphQL provider implementation + +Here's an example of a GraphQL provider that implements three interfaces at the same time. + +```java +package org.oasis_open.contextserver.graphql; + +import graphql.schema.*; +import org.osgi.service.component.annotations.Component; + +import graphql.servlet.GraphQLQueryProvider; +import graphql.servlet.GraphQLTypesProvider; + +import java.util.*; + +import static graphql.Scalars.GraphQLID; +import static graphql.Scalars.GraphQLString; +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; +import static graphql.schema.GraphQLObjectType.newObject; + +@Component( + name="ExampleGraphQLProvider" +) +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 null; + } + + public Collection getTypes() { + + List types = new ArrayList(); + return types; + } + +} +``` \ No newline at end of file From 0ed7523882848aac9a9470fef92359bc73e8b819 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Mon, 24 Apr 2017 15:40:50 +0200 Subject: [PATCH 08/10] New OSGi examples sub-projects, including a full Apache Karaf package that contains a fully running server, as well as an example of GraphQL providers. --- README.md | 177 ++++-------------- examples/osgi/apache-karaf-feature/pom.xml | 94 ++++++++++ .../src/main/feature/feature.xml | 7 + examples/osgi/apache-karaf-package/pom.xml | 104 ++++++++++ examples/osgi/buildAndRun.sh | 7 + examples/osgi/pom.xml | 24 +++ examples/osgi/providers/pom.xml | 57 ++++++ .../examples/osgi/ExampleGraphQLProvider.java | 42 +++++ 8 files changed, 373 insertions(+), 139 deletions(-) create mode 100644 examples/osgi/apache-karaf-feature/pom.xml create mode 100644 examples/osgi/apache-karaf-feature/src/main/feature/feature.xml create mode 100644 examples/osgi/apache-karaf-package/pom.xml create mode 100755 examples/osgi/buildAndRun.sh create mode 100644 examples/osgi/pom.xml create mode 100644 examples/osgi/providers/pom.xml create mode 100644 examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java diff --git a/README.md b/README.md index f02cb35d..152557ee 100644 --- a/README.md +++ b/README.md @@ -120,153 +120,52 @@ The [OsgiGraphQLServlet](https://github.com/graphql-java/graphql-java-servlet/bl * [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. -### Deploying using an Apache Karaf feature +## Examples -You can use the graphql-java-servlet as part of an Apache Karaf feature, which makes it easy to setup all the proper -dependencies. Here's an example pom.xml file to setup the Karaf feature: +You can now find some example on how to use graphql-java-servlet. -```xml - - - 4.0.0 - feature - - graphql-java-servlet-features - - - - - 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 - - - - - - - - org.apache.karaf.tooling - karaf-maven-plugin - 4.0.8 - true - - 80 - true - true - - - - - - - -``` +### OSGi Examples -And here is a sample src/main/feature/feature.xml file to add some dependencies on other features: +#### Requirements -```xml - - - - scr - war - - -``` +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+. -### Example GraphQL provider implementation +#### Building & running the OSGi examples -Here's an example of a GraphQL provider that implements three interfaces at the same time. +You can build the OSGi examples sub-projects by simply executing the following command from the examples/osgi directory: -```java -package org.oasis_open.contextserver.graphql; - -import graphql.schema.*; -import org.osgi.service.component.annotations.Component; - -import graphql.servlet.GraphQLQueryProvider; -import graphql.servlet.GraphQLTypesProvider; - -import java.util.*; - -import static graphql.Scalars.GraphQLID; -import static graphql.Scalars.GraphQLString; -import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; -import static graphql.schema.GraphQLObjectType.newObject; - -@Component( - name="ExampleGraphQLProvider" -) -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; - } + 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: - public Collection getMutations() { - return null; - } + bin/karaf + +You should then be able to access the GraphQL endpoint at the following URL once the server is started: - public Collection getTypes() { + 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 ): - List types = new ArrayList(); - return types; - } + ./buildAndRun.sh -} -``` \ No newline at end of file +#### 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](https://github.com/graphql-java/graphql-java-servlet/blob/master/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](https://github.com/graphql-java/graphql-java-servlet/blob/master/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](https://github.com/graphql-java/graphql-java-servlet/blob/master/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; + } +} From 03c33cd500869c1466677d29938bb083192d0a68 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Mon, 24 Apr 2017 15:43:46 +0200 Subject: [PATCH 09/10] Switch to relative links --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 152557ee..81305cef 100644 --- a/README.md +++ b/README.md @@ -159,13 +159,13 @@ We also provide a script file to do all of the building and running at once (onl #### 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](https://github.com/graphql-java/graphql-java-servlet/blob/master/examples/osgi/apache-karaf-feature/pom.xml) +* [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](https://github.com/graphql-java/graphql-java-servlet/blob/master/examples/osgi/apache-karaf-feature/src/main/feature/feature.xml) +* [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](https://github.com/graphql-java/graphql-java-servlet/blob/master/examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java) +* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java) From 79d50f34214dc879ec1262b66ee0c132a4417ef5 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Mon, 24 Apr 2017 15:44:58 +0200 Subject: [PATCH 10/10] Fix servlet registration and upgrade to OSGi r6 property --- src/main/java/graphql/servlet/OsgiGraphQLServlet.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 {