UNOMI-180 Implement CXS GraphQL API - Event output field generation is working again, input will be harder because of the way the objects are prefixed with the input naming convention
Signed-off-by: Serge Huber <shu...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/146e7552 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/146e7552 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/146e7552 Branch: refs/heads/UNOMI-180-CXS-GRAPHQLAPI Commit: 146e7552b349670c40e7ff38cd95239bee9bdb43 Parents: c447bef Author: Serge Huber <shu...@apache.org> Authored: Fri Jul 27 22:12:41 2018 +0200 Committer: Serge Huber <shu...@apache.org> Committed: Wed Nov 21 14:56:46 2018 +0100 ---------------------------------------------------------------------- .../java/org/apache/unomi/graphql/CXSQuery.java | 6 +++ .../graphql/builders/CXSBuildersUtils.java | 52 ++++++++++++++++++++ .../graphql/builders/CXSEventBuilders.java | 24 ++------- .../internal/CXSGraphQLProviderImpl.java | 24 +++------ 4 files changed, 70 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/146e7552/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/CXSQuery.java ---------------------------------------------------------------------- diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/CXSQuery.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/CXSQuery.java index 24d11ff..7b548a9 100644 --- a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/CXSQuery.java +++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/CXSQuery.java @@ -26,6 +26,12 @@ import java.util.List; @GraphQLName("CXS_Query") public class CXSQuery { + CXSGraphQLProvider cxsGraphQLProvider; + + public CXSQuery(CXSGraphQLProvider cxsGraphQLProvider) { + this.cxsGraphQLProvider = cxsGraphQLProvider; + } + @GraphQLField public List<CXSEventType> getEventTypes() { return new ArrayList<>(); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/146e7552/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSBuildersUtils.java ---------------------------------------------------------------------- diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSBuildersUtils.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSBuildersUtils.java new file mode 100644 index 0000000..3d9c586 --- /dev/null +++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSBuildersUtils.java @@ -0,0 +1,52 @@ +/* + * 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.builders; + +import graphql.annotations.processor.GraphQLAnnotationsComponent; +import graphql.annotations.processor.ProcessingElementsContainer; +import graphql.schema.GraphQLInputObjectType; +import graphql.schema.GraphQLObjectType; + +public class CXSBuildersUtils { + + public static GraphQLObjectType.Builder getOutputBuilderFromAnnotatedClass( + GraphQLAnnotationsComponent annotationsComponent, + ProcessingElementsContainer container, + String typeName, + Class annotatedClass) { + if (typeName == null) { + typeName = annotatedClass.getName(); + } + return GraphQLObjectType.newObject() + .name(typeName) + .fields(((GraphQLObjectType) annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(annotatedClass, container)).getFieldDefinitions()); + } + + public static GraphQLInputObjectType.Builder getInputBuilderFromAnnotatedClass( + GraphQLAnnotationsComponent annotationsComponent, + ProcessingElementsContainer container, + String typeName, + Class annotatedClass) { + if (typeName == null) { + typeName = annotatedClass.getName(); + } + return GraphQLInputObjectType.newInputObject() + .name(typeName) + .fields(((GraphQLInputObjectType) annotationsComponent.getInputTypeProcessor().getInputTypeOrRef(annotatedClass, container)).getFieldDefinitions()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/146e7552/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSEventBuilders.java ---------------------------------------------------------------------- diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSEventBuilders.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSEventBuilders.java index f5e5777..ff2a27e 100644 --- a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSEventBuilders.java +++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/builders/CXSEventBuilders.java @@ -19,10 +19,7 @@ package org.apache.unomi.graphql.builders; import graphql.annotations.processor.GraphQLAnnotationsComponent; import graphql.annotations.processor.ProcessingElementsContainer; import graphql.schema.*; -import org.apache.unomi.graphql.CXSEvent; -import org.apache.unomi.graphql.CXSEventOccurrenceFilterInput; -import org.apache.unomi.graphql.CXSEventType; -import org.apache.unomi.graphql.PageInfo; +import org.apache.unomi.graphql.*; import org.apache.unomi.graphql.propertytypes.*; import java.util.ArrayList; @@ -276,13 +273,8 @@ public class CXSEventBuilders implements CXSBuilder { } private GraphQLInputType buildCXSEventInputType() { - GraphQLInputObjectType.Builder cxsEventInputType = newInputObject() - .name("CXS_EventInput") - .description("The event input object to send events to the Context Server") - .field(newInputObjectField() - .name("id") - .type(GraphQLID) - ); + GraphQLInputObjectType.Builder cxsEventInputType = CXSBuildersUtils.getInputBuilderFromAnnotatedClass(annotationsComponent, container, "CXS_EventInput", CXSEventInput.class) + .description("The event input object to send events to the Context Server"); for (Map.Entry<String,CXSEventType> cxsEventTypeEntry : eventTypes.entrySet()) { CXSEventType cxsEventType = cxsEventTypeEntry.getValue(); @@ -414,17 +406,9 @@ public class CXSEventBuilders implements CXSBuilder { } private GraphQLOutputType buildCXSEventPropertiesOutputType() { - GraphQLObjectType.Builder eventPropertiesOutputType = newObject() - .name("CXS_EventProperties") + GraphQLObjectType.Builder eventPropertiesOutputType = CXSBuildersUtils.getOutputBuilderFromAnnotatedClass(annotationsComponent, container, "CXS_EventProperties", CXSEventProperties.class) .description("All possible properties of an event"); - // we create a dummy field because GraphQL requires at least one - eventPropertiesOutputType.field(newFieldDefinition() - .type(GraphQLInt) - .name("typeCount") - .description("Total count of different field types") - ); - for (Map.Entry<String,CXSEventType> cxsEventTypeEntry : eventTypes.entrySet()) { CXSEventType cxsEventType = cxsEventTypeEntry.getValue(); eventPropertiesOutputType http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/146e7552/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CXSGraphQLProviderImpl.java ---------------------------------------------------------------------- diff --git a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CXSGraphQLProviderImpl.java b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CXSGraphQLProviderImpl.java index a67cace..5880731 100644 --- a/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CXSGraphQLProviderImpl.java +++ b/graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/internal/CXSGraphQLProviderImpl.java @@ -66,9 +66,7 @@ public class CXSGraphQLProviderImpl implements CXSGraphQLProvider, GraphQLQueryP } public void updateGraphQLTypes() { - - Map<String,GraphQLType> typeRegistry = container.getTypeRegistry(); - + typeRegistry.clear(); typeRegistry.put(PageInfo.class.getName(), annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(PageInfo.class, container)); typeRegistry.put("CXS_GeoPoint", annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(CXSGeoPoint.class, container)); @@ -80,18 +78,13 @@ public class CXSGraphQLProviderImpl implements CXSGraphQLProvider, GraphQLQueryP typeRegistry.put("CXS_EventTypeInput", annotationsComponent.getInputTypeProcessor().getInputTypeOrRef(CXSEventTypeInput.class, container)); typeRegistry.put("CXS_OrderByInput", annotationsComponent.getInputTypeProcessor().getInputTypeOrRef(CXSOrderByInput.class, container)); + cxsEventBuilders.updateTypes(); + typeRegistry.put("CXS_Query", annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(CXSQuery.class, container)); typeRegistry.put("CXS_Mutation", annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(CXSMutation.class, container)); - cxsEventBuilders.updateTypes(); - } - private GraphQLObjectType.Builder getBuilderFromAnnotatedClass(Class annotatedClass) { - return GraphQLObjectType.newObject() - .name(annotatedClass.getName()) - .fields(((GraphQLObjectType) annotationsComponent.getOutputTypeProcessor().getOutputTypeOrRef(annotatedClass, container)).getFieldDefinitions()); - } private GraphQLOutputType getOutputTypeFromRegistry(String typeName) { return (GraphQLOutputType) typeRegistry.get(typeName); @@ -108,17 +101,16 @@ public class CXSGraphQLProviderImpl implements CXSGraphQLProvider, GraphQLQueryP @Override public Collection<GraphQLFieldDefinition> getQueries() { List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<GraphQLFieldDefinition>(); + final CXSGraphQLProvider cxsGraphQLProvider = this; fieldDefinitions.add(newFieldDefinition() .type(getOutputTypeFromRegistry("CXS_Query")) .name("cxs") .description("Root field for all CXS queries") - /* - .dataFetcher(new DataFetcher() { - public Object get(DataFetchingEnvironment environment) { - Map<String,Object> map = environment.getContext(); - return map.keySet(); + .dataFetcher(new DataFetcher<CXSGraphQLProvider>() { + public CXSGraphQLProvider get(DataFetchingEnvironment environment) { + return cxsGraphQLProvider; } - })*/.build()); + }).build()); return fieldDefinitions; }