From d5515fbde2caf1069a781cf6642403364b9275cf Mon Sep 17 00:00:00 2001 From: "arash.shokoufandeh" Date: Mon, 25 Sep 2017 18:34:15 -0700 Subject: [PATCH 1/3] adding a registry to connection creation to allow for reusable connections as long as the underlying GraphQL types is equals --- gradle.properties | 2 +- .../relay/type/PagingOutputTypeConverter.java | 24 +++++++- .../com/nfl/glitr/data/query/QueryType.java | 5 ++ .../data/query/additionalTypes/Cyborg.java | 60 ++++++++++--------- .../glitr/data/query/additionalTypes/Man.java | 60 ++++++++++--------- .../data/query/additionalTypes/Person.java | 5 +- .../data/query/additionalTypes/QueryRoot.java | 6 +- .../TypeRegistryIntegrationTest.groovy | 50 ++++++++++------ 8 files changed, 127 insertions(+), 85 deletions(-) diff --git a/gradle.properties b/gradle.properties index 29b8dac..57f7dc8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,6 +5,6 @@ maven.central.sync=false sonatype.username=DUMMY_SONATYPE_USER sonatype.password=DUMMY_SONATYPE_PASSWORD -PROJECT_VERSION=1.1.6 +PROJECT_VERSION=1.2.1 PROJECT_GITHUB_REPO_URL=https://github.com/nfl/glitr PROJECT_LICENSE_URL=https://github.com/nfl/glitr/blob/master/LICENSE \ No newline at end of file diff --git a/src/main/java/com/nfl/glitr/relay/type/PagingOutputTypeConverter.java b/src/main/java/com/nfl/glitr/relay/type/PagingOutputTypeConverter.java index 83ba626..58060d8 100644 --- a/src/main/java/com/nfl/glitr/relay/type/PagingOutputTypeConverter.java +++ b/src/main/java/com/nfl/glitr/relay/type/PagingOutputTypeConverter.java @@ -2,6 +2,7 @@ import com.google.common.collect.Lists; import com.googlecode.gentyref.GenericTypeReflector; +import com.nfl.glitr.exception.GlitrException; import com.nfl.glitr.relay.RelayHelper; import com.nfl.glitr.util.ReflectionUtil; import com.nfl.glitr.annotation.GlitrForwardPagingArguments; @@ -19,6 +20,8 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Output type converter function for paging arguments annotations. @@ -28,6 +31,8 @@ public class PagingOutputTypeConverter implements Func4 connectionRegistry = new ConcurrentHashMap<>(); + @Override public GraphQLOutputType call(@Nullable Field field, Method method, Class declaringClass, Annotation annotation) { @@ -64,8 +69,23 @@ public GraphQLOutputType call(@Nullable Field field, Method method, Class declar edgeGraphQLOutputType, relayHelper.getNodeInterface(), Collections.emptyList()); - // last build the relay connection! - return relayHelper.connectionType(endEdgeClass.getSimpleName(), edgeType, Lists.newArrayList()); + // build the relay connection + GraphQLObjectType connectionType = relayHelper.connectionType(endEdgeClass.getSimpleName(), edgeType, Lists.newArrayList()); + + // check if a connection with this name already exists + GraphQLObjectType qlObjectType = connectionRegistry.get(connectionType.getName()); + if (qlObjectType != null) { + // TODO: better equality function + if (!qlObjectType.toString().equals(connectionType.toString())) { + throw new GlitrException("Attempting to create two types with the same name. All types within a GraphQL schema must have unique names. " + + "You have defined the type [" + connectionType.getName() + "] as both [" + qlObjectType + "] and [" + connectionType + "]"); + } + return qlObjectType; + } + + // add the connection to the registry and return the connection + connectionRegistry.put(connectionType.getName(), connectionType); + return connectionType; } public PagingOutputTypeConverter setTypeRegistry(TypeRegistry typeRegistry) { diff --git a/src/test/groovy/com/nfl/glitr/data/query/QueryType.java b/src/test/groovy/com/nfl/glitr/data/query/QueryType.java index 21f1225..33c4b99 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/QueryType.java +++ b/src/test/groovy/com/nfl/glitr/data/query/QueryType.java @@ -22,4 +22,9 @@ public Video getVideo() { @GlitrArgument(name = "ids", type = String[].class, nullable = false) public List getNodes() { return null; } + + @GlitrForwardPagingArguments + public List