From 4a5ca5430ed0e88f1da592b9f0ccb018c3f36ccc Mon Sep 17 00:00:00 2001 From: "arash.shokoufandeh" Date: Wed, 17 Jan 2018 16:19:26 -0800 Subject: [PATCH] bumping to the latest release of graphql-java and adding field visibility to glitr builder na --- build.gradle | 2 +- gradle.properties | 2 +- src/main/java/com/nfl/glitr/Glitr.java | 25 +++++++++++++------ src/main/java/com/nfl/glitr/GlitrBuilder.java | 13 +++++++--- .../glitr/util/QueryComplexityCalculator.java | 4 +-- .../registry/GlitrAdditionalTypesTest.groovy | 2 +- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index 247264f..3f2b519 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ repositories { dependencies { // GraphQL dependencies - compile("com.graphql-java:graphql-java:6.0") + compile("com.graphql-java:graphql-java:7.0") // Commons dependencies compile("org.apache.commons:commons-lang3:3.4") diff --git a/gradle.properties b/gradle.properties index 07c3196..2a6c9b4 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.3.0 +PROJECT_VERSION=1.4.0 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/Glitr.java b/src/main/java/com/nfl/glitr/Glitr.java index 97db230..b6f674d 100644 --- a/src/main/java/com/nfl/glitr/Glitr.java +++ b/src/main/java/com/nfl/glitr/Glitr.java @@ -1,12 +1,12 @@ package com.nfl.glitr; -import com.nfl.glitr.exception.GlitrException; import com.nfl.glitr.registry.TypeRegistry; import com.nfl.glitr.relay.RelayHelper; import com.nfl.glitr.util.ObjectMapper; import com.nfl.glitr.util.QueryComplexityCalculator; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; +import graphql.schema.visibility.GraphqlFieldVisibility; import javax.annotation.Nullable; @@ -22,11 +22,11 @@ public class Glitr { private static ObjectMapper objectMapper; - public Glitr(TypeRegistry typeRegistry, Class queryRoot, @Nullable ObjectMapper objectMapper, @Nullable Class mutationRoot, @Nullable QueryComplexityCalculator queryComplexityCalculator) { - this(typeRegistry, queryRoot, objectMapper, null, mutationRoot, queryComplexityCalculator); + public Glitr(TypeRegistry typeRegistry, Class queryRoot, @Nullable GraphqlFieldVisibility fieldVisibility, @Nullable ObjectMapper objectMapper, @Nullable Class mutationRoot, @Nullable QueryComplexityCalculator queryComplexityCalculator) { + this(typeRegistry, queryRoot, fieldVisibility, objectMapper, null, mutationRoot, queryComplexityCalculator); } - public Glitr(TypeRegistry typeRegistry, Class queryRoot, @Nullable ObjectMapper objectMapper, @Nullable RelayHelper relayHelper, @Nullable Class mutationRoot, @Nullable QueryComplexityCalculator queryComplexityCalculator) { + public Glitr(TypeRegistry typeRegistry, Class queryRoot, @Nullable GraphqlFieldVisibility fieldVisibility, @Nullable ObjectMapper objectMapper, @Nullable RelayHelper relayHelper, @Nullable Class mutationRoot, @Nullable QueryComplexityCalculator queryComplexityCalculator) { assertNotNull(typeRegistry, "TypeRegistry can't be null"); assertNotNull(queryRoot, "queryRoot class can't be null"); this.typeRegistry = typeRegistry; @@ -37,7 +37,7 @@ public Glitr(TypeRegistry typeRegistry, Class queryRoot, @Nullable ObjectMapper .withQueryComplexityExcludeNodes(typeRegistry.getQueryComplexityExcludeNodes()); } Glitr.objectMapper = objectMapper; - this.schema = buildSchema(queryRoot, mutationRoot); + this.schema = buildSchema(queryRoot, mutationRoot, fieldVisibility); } public TypeRegistry getTypeRegistry() { @@ -65,7 +65,7 @@ public static ObjectMapper getObjectMapper() { return objectMapper; } - private GraphQLSchema buildSchema(Class queryRoot, Class mutationRoot) { + private GraphQLSchema buildSchema(Class queryRoot, Class mutationRoot, GraphqlFieldVisibility fieldVisibility) { // create GraphQL Schema GraphQLObjectType mutationType = null; if (mutationRoot != null) { @@ -73,14 +73,23 @@ private GraphQLSchema buildSchema(Class queryRoot, Class mutationRoot) { } GraphQLObjectType queryType = (GraphQLObjectType) typeRegistry.lookup(queryRoot); + + if (fieldVisibility != null) { + return GraphQLSchema.newSchema() + .query(queryType) + .mutation(mutationType) + .fieldVisibility(fieldVisibility) + .build(typeRegistry.getTypeDictionary()); + } + return GraphQLSchema.newSchema() .query(queryType) .mutation(mutationType) .build(typeRegistry.getTypeDictionary()); } - public GraphQLSchema reloadSchema(Class queryRoot, Class mutationRoot) { - this.schema = buildSchema(queryRoot, mutationRoot); + public GraphQLSchema reloadSchema(Class queryRoot, Class mutationRoot, GraphqlFieldVisibility fieldVisibility) { + this.schema = buildSchema(queryRoot, mutationRoot, fieldVisibility); return this.schema; } } diff --git a/src/main/java/com/nfl/glitr/GlitrBuilder.java b/src/main/java/com/nfl/glitr/GlitrBuilder.java index 1987c31..6953c38 100644 --- a/src/main/java/com/nfl/glitr/GlitrBuilder.java +++ b/src/main/java/com/nfl/glitr/GlitrBuilder.java @@ -11,6 +11,7 @@ import com.nfl.glitr.util.ObjectMapper; import com.nfl.glitr.util.QueryComplexityCalculator; import graphql.schema.*; +import graphql.schema.visibility.GraphqlFieldVisibility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import rx.functions.Func4; @@ -36,6 +37,7 @@ public class GlitrBuilder { private RelayConfig relayConfig = null; private Object queryRoot = null; private Object mutationRoot = null; + private GraphqlFieldVisibility fieldVisibility = null; private ObjectMapper objectMapper = null; private QueryComplexityCalculator queryComplexityCalculator; @@ -63,6 +65,11 @@ public GlitrBuilder withMutationRoot(Object mutationRoot) { return this; } + public GlitrBuilder withFieldVisibility(GraphqlFieldVisibility fieldVisibility) { + this.fieldVisibility = fieldVisibility; + return this; + } + public GlitrBuilder withOverrides(Map> overrides) { this.overrides = overrides; return this; @@ -146,7 +153,6 @@ public Glitr build() { } private Glitr buildGlitr() { - if (objectMapper == null) { logger.warn("No ObjectMapper instance has been registered."); } @@ -163,11 +169,10 @@ private Glitr buildGlitr() { Class mutationRootClass = mutationRoot != null ? mutationRoot.getClass() : null; - return new Glitr(typeRegistry, queryRoot.getClass(), objectMapper, null, mutationRootClass, queryComplexityCalculator); + return new Glitr(typeRegistry, queryRoot.getClass(), fieldVisibility, objectMapper, null, mutationRootClass, queryComplexityCalculator); } private Glitr buildGlitrWithRelaySupport() { - if (objectMapper == null) { throw new IllegalArgumentException("No ObjectMapper instance has been registered. It's required to register one when using GLiTR with Relay"); } @@ -201,6 +206,6 @@ private Glitr buildGlitrWithRelaySupport() { Class mutationRootClass = mutationRoot != null ? mutationRoot.getClass() : null; - return new Glitr(typeRegistry, queryRoot.getClass(), objectMapper, relayHelper, mutationRootClass, queryComplexityCalculator); + return new Glitr(typeRegistry, queryRoot.getClass(), fieldVisibility, objectMapper, relayHelper, mutationRootClass, queryComplexityCalculator); } } diff --git a/src/main/java/com/nfl/glitr/util/QueryComplexityCalculator.java b/src/main/java/com/nfl/glitr/util/QueryComplexityCalculator.java index ab12f01..aadbda4 100644 --- a/src/main/java/com/nfl/glitr/util/QueryComplexityCalculator.java +++ b/src/main/java/com/nfl/glitr/util/QueryComplexityCalculator.java @@ -204,7 +204,7 @@ private int calculateMaxDepth(Node queryNode) { } int maxChildDepth = 0; - for (Node childNode : queryNode.getChildren()) { + for (Node childNode : (List) queryNode.getChildren()) { final int currentChildDepth = calculateMaxDepth(childNode); maxChildDepth = Math.max(maxChildDepth, currentChildDepth); } @@ -309,7 +309,7 @@ private int queryScore(String path, Node queryNode, int depth) { } } - for (Node currentChild : queryNode.getChildren()) { + for (Node currentChild : (List) queryNode.getChildren()) { if (currentChild.getClass() == Argument.class) { continue; } diff --git a/src/test/groovy/com/nfl/glitr/registry/GlitrAdditionalTypesTest.groovy b/src/test/groovy/com/nfl/glitr/registry/GlitrAdditionalTypesTest.groovy index c30bfb6..10daf60 100644 --- a/src/test/groovy/com/nfl/glitr/registry/GlitrAdditionalTypesTest.groovy +++ b/src/test/groovy/com/nfl/glitr/registry/GlitrAdditionalTypesTest.groovy @@ -28,7 +28,7 @@ class GlitrAdditionalTypesTest extends Specification { when: "add additional types to type registry and reload the schema" glitr.typeRegistry.lookup(Man.class) glitr.typeRegistry.lookup(Cyborg.class) - glitr.reloadSchema(QueryRoot.class, null) + glitr.reloadSchema(QueryRoot.class, null, null) then: "Man and Cyborg are now part of the schema" glitr.typeRegistry.getType(typeResolutionEnvMan) != null glitr.typeRegistry.getType(typeResolutionEnvCyborg) != null