Skip to content

Commit

Permalink
Merge pull request #20 from tinnou/master
Browse files Browse the repository at this point in the history
Update to graphql-java:4.2
  • Loading branch information
vaant authored Sep 24, 2017
2 parents 747b4b5 + 85f0854 commit e924d44
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repositories {

dependencies {
// GraphQL dependencies
compile("com.graphql-java:graphql-java:2.3.0")
compile("com.graphql-java:graphql-java:4.2")

// Commons dependencies
compile("org.apache.commons:commons-lang3:3.4")
Expand Down
51 changes: 44 additions & 7 deletions src/main/java/com/nfl/glitr/registry/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@
import com.nfl.glitr.registry.datafetcher.AnnotationBasedDataFetcherFactory;
import com.nfl.glitr.registry.datafetcher.query.OverrideDataFetcher;
import com.nfl.glitr.registry.datafetcher.query.batched.CompositeDataFetcherFactory;
import com.nfl.glitr.registry.type.*;
import com.nfl.glitr.registry.type.GraphQLEnumTypeFactory;
import com.nfl.glitr.registry.type.GraphQLInputObjectTypeFactory;
import com.nfl.glitr.registry.type.GraphQLInterfaceTypeFactory;
import com.nfl.glitr.registry.type.GraphQLObjectTypeFactory;
import com.nfl.glitr.registry.type.GraphQLTypeFactory;
import com.nfl.glitr.registry.type.JavaType;
import com.nfl.glitr.registry.type.Scalars;
import com.nfl.glitr.relay.Node;
import com.nfl.glitr.relay.Relay;
import com.nfl.glitr.util.ReflectionUtil;
import graphql.schema.*;
import graphql.TypeResolutionEnvironment;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeReference;
import graphql.schema.PropertyDataFetcher;
import graphql.schema.TypeResolver;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,12 +42,26 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.*;
import java.util.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static graphql.Scalars.*;
import static graphql.Scalars.GraphQLBoolean;
import static graphql.Scalars.GraphQLFloat;
import static graphql.Scalars.GraphQLID;
import static graphql.Scalars.GraphQLInt;
import static graphql.Scalars.GraphQLLong;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
Expand Down Expand Up @@ -71,6 +106,8 @@ public class TypeRegistry implements TypeResolver {
this.relay = relay;
if (relay != null) {
this.nodeInterface = relay.nodeInterface(this);
// register Node so we don't inadvertently recreate it later
this.registry.put(Node.class, this.nodeInterface);
}
this.explicitRelayNodeScanEnabled = explicitRelayNodeScanEnabled;
}
Expand All @@ -91,8 +128,8 @@ public GraphQLInterfaceType getNodeInterface() {
}

@Override
public GraphQLObjectType getType(Object object) {
return (GraphQLObjectType)registry.get(object.getClass());
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
return (GraphQLObjectType)registry.get(env.getObject().getClass());
}

public Map<Class, GraphQLType> getRegistry() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.nfl.glitr.relay;

import graphql.relay.PageInfo;
import graphql.relay.ConnectionCursor;
import graphql.relay.DefaultPageInfo;

public class PageInfoWithTotal extends PageInfo {
public class PageInfoWithTotal extends DefaultPageInfo {

private int total;

public PageInfoWithTotal(ConnectionCursor startCursor, ConnectionCursor endCursor,
boolean hasPreviousPage, boolean hasNextPage) {
super(startCursor, endCursor, hasPreviousPage, hasNextPage);
}

public int getTotal() {
return total;
Expand Down
56 changes: 42 additions & 14 deletions src/main/java/com/nfl/glitr/relay/RelayHelper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.nfl.glitr.relay;

import com.nfl.glitr.registry.TypeRegistry;
import graphql.relay.Base64;
import graphql.relay.ConnectionCursor;
import graphql.relay.DefaultConnection;
import graphql.relay.DefaultConnectionCursor;
import graphql.relay.DefaultEdge;
import graphql.relay.Edge;
import graphql.schema.*;

import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -44,30 +53,30 @@ public GraphQLObjectType connectionType(String simpleName, GraphQLObjectType edg
}

public static graphql.relay.Connection buildConnection(Iterable<?> col, int skipItems, int totalCount) {
List<Edge> edges = new ArrayList<>();
List<Edge<Object>> edges = new ArrayList<>();
int ix = skipItems;

for (Object object : col) {
edges.add(new Edge(object, new ConnectionCursor(createCursor(ix++))));
edges.add(new DefaultEdge<>(object, new DefaultConnectionCursor(createCursor(ix++))));
}

PageInfoWithTotal pageInfoWithTotal = new PageInfoWithTotal();
ConnectionCursor startCursor = null;
ConnectionCursor endCursor = null ;
boolean hasPreviousPage = skipItems > 0 && totalCount > 0;
boolean hasNextPage = skipItems + edges.size() + 1 < totalCount;

if (edges.size() > 0) {
Edge firstEdge = edges.get(0);
Edge lastEdge = edges.get(edges.size() - 1);
pageInfoWithTotal.setStartCursor(firstEdge.getCursor());
pageInfoWithTotal.setEndCursor(lastEdge.getCursor());
startCursor = firstEdge.getCursor();
endCursor = lastEdge.getCursor();
}

pageInfoWithTotal.setHasPreviousPage(skipItems > 0 && totalCount > 0);
pageInfoWithTotal.setHasNextPage(skipItems + edges.size() + 1 < totalCount);
PageInfoWithTotal pageInfoWithTotal = new PageInfoWithTotal(startCursor, endCursor,
hasPreviousPage, hasNextPage);
pageInfoWithTotal.setTotal(totalCount);

graphql.relay.Connection connection = new graphql.relay.Connection();
connection.setEdges(edges);
connection.setPageInfo(pageInfoWithTotal);
return connection;
return new DefaultConnection<>(edges, pageInfoWithTotal);
}

public static String createCursor(int offset) {
Expand All @@ -79,4 +88,23 @@ public static int getOffsetFromCursor(String cursor, int defaultValue) {
String string = Base64.fromBase64(cursor);
return Integer.parseInt(string.substring(DUMMY_CURSOR_PREFIX.length()));
}


static public class Base64 {

private Base64() {
}

public static String toBase64(String string) {
try {
return DatatypeConverter.printBase64Binary(string.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

public static String fromBase64(String string) {
return new String(DatatypeConverter.parseBase64Binary(string), Charset.forName("UTF-8"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nfl.glitr.data.circularReference
import com.nfl.glitr.Glitr
import com.nfl.glitr.GlitrBuilder
import com.nfl.glitr.data.query.QueryType
import graphql.TypeResolutionEnvironment
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLType
Expand All @@ -20,7 +21,8 @@ class CircularReferenceTest extends Specification {
type instanceof GraphQLInterfaceType
type.name == AbstractRead.simpleName
then: "And make sure the implementing type has the interface registered"
def objType = glitr.typeRegistry.getType(new Novel())
def objType = glitr.typeRegistry.getType(new TypeResolutionEnvironment(new Novel(),
null, null, null, null))
objType.class == GraphQLObjectType
objType.name == Novel.simpleName
objType.fieldDefinitions.name as Set == ["novel", "pageCount", "title", "reviewed"] as Set
Expand All @@ -36,7 +38,8 @@ class CircularReferenceTest extends Specification {
type instanceof GraphQLInterfaceType
type.name == Readable.simpleName
then: "And make sure the implementing type has the interface registered"
def objType = glitr.typeRegistry.getType(new Book())
def objType = glitr.typeRegistry.getType(new TypeResolutionEnvironment(new Book(),
null, null, null, null))
objType.class == GraphQLObjectType
objType.name == Book.simpleName
objType.fieldDefinitions.name as Set == ["title", "synopsis"] as Set
Expand Down
1 change: 0 additions & 1 deletion src/test/groovy/com/nfl/glitr/data/query/QueryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.nfl.glitr.annotation.GlitrArgument;
import com.nfl.glitr.annotation.GlitrForwardPagingArguments;
import com.nfl.glitr.relay.*;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.nfl.glitr.data.query.additionalTypes.Cyborg
import com.nfl.glitr.data.query.additionalTypes.Man
import com.nfl.glitr.data.query.additionalTypes.QueryRoot
import com.nfl.glitr.util.SerializationUtil
import graphql.TypeResolutionEnvironment
import spock.lang.Specification

class GlitrAdditionalTypesTest extends Specification {
Expand All @@ -16,17 +17,21 @@ class GlitrAdditionalTypesTest extends Specification {
.withObjectMapper(SerializationUtil.objectMapper)
.withQueryRoot(new QueryRoot())
.build()

def typeResolutionEnvMan = new TypeResolutionEnvironment(new Man(), null, null, null, null)
def typeResolutionEnvCyborg = new TypeResolutionEnvironment(new Cyborg(), null, null, null, null)

then: "incidentally Man and Cyborg by default have not been discovered"
glitr.typeRegistry.getType(new Man()) == null
glitr.typeRegistry.getType(new Cyborg()) == null
glitr.typeRegistry.getType(typeResolutionEnvMan) == null
glitr.typeRegistry.getType(typeResolutionEnvCyborg) == null

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)
then: "Man and Cyborg are now part of the schema"
glitr.typeRegistry.getType(new Man()) != null
glitr.typeRegistry.getType(new Cyborg()) != null
glitr.typeRegistry.getType(typeResolutionEnvMan) != null
glitr.typeRegistry.getType(typeResolutionEnvCyborg) != null
glitr.schema.getType(Man.class.simpleName) != null
glitr.schema.getType(Cyborg.class.simpleName) != null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.nfl.glitr.GlitrBuilder
import com.nfl.glitr.data.additionalScalars.CustomScalar
import com.nfl.glitr.data.additionalScalars.Root
import graphql.Scalars
import graphql.TypeResolutionEnvironment
import spock.lang.Specification

class GlitrRegisterAdditionalScalars extends Specification {
Expand All @@ -15,8 +16,9 @@ class GlitrRegisterAdditionalScalars extends Specification {
.withQueryRoot(new Root())
.addCustomScalar(CustomScalar.class, Scalars.GraphQLString)
.build()
def typeResolutionEnv = new TypeResolutionEnvironment(new Root(), null, null, null, null)
then: "make sure the scalar has been registered correctly as a GraphQLString"
glitr.typeRegistry.getType(new Root()).getFieldDefinition("scalar").type.name == Scalars.GraphQLString.name
glitr.typeRegistry.getType(typeResolutionEnv).getFieldDefinition("scalar").type.name == Scalars.GraphQLString.name
}

def "Register twice the same custom scalar should fail"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package com.nfl.glitr.registry.datafetcher.query

import graphql.Scalars
import graphql.schema.DataFetchingEnvironment
import graphql.schema.DataFetchingEnvironmentBuilder
import graphql.schema.PropertyDataFetcher
import spock.lang.Specification

class CompositeDataFetcherTest extends Specification {

def propertyDataFetcher = new PropertyDataFetcher("title")
def overrideDataFetcher = new OverrideDataFetcher("title", new Override())
def env = new DataFetchingEnvironment(new DummyClass(), null, null, null, Scalars.GraphQLString, null, null);
def env = DataFetchingEnvironmentBuilder.newDataFetchingEnvironment()
.source(new DummyClass())
.fieldType(Scalars.GraphQLString)
.build()

def "Should iterate over dataFetchers until override gets called & return null when not found"() {
expect:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.nfl.glitr.registry.datafetcher.query

import graphql.Scalars
import graphql.schema.DataFetchingEnvironment
import graphql.schema.DataFetchingEnvironmentBuilder
import spock.lang.Specification

class OverrideDataFetcherTest extends Specification {

def env = new DataFetchingEnvironment(new DummyClass(), null, null, null, Scalars.GraphQLString, null, null);
def env = DataFetchingEnvironmentBuilder.newDataFetchingEnvironment()
.source(new DummyClass())
.fieldType(Scalars.GraphQLString)
.build()

def "override method in Override class"() {
expect:
Expand Down
6 changes: 3 additions & 3 deletions src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ class RelayHelperTest extends Specification {
assert pageInfoWithTotal.total == totalCount

if (resultSize > 0) {
assert (connection.pageInfo.startCursor?.value == graphql.relay.Base64.toBase64("simple-cursor${toSkip}"))
assert (connection.pageInfo.endCursor?.value == graphql.relay.Base64.toBase64("simple-cursor${toSkip + resultSize - 1}"))
assert (connection.pageInfo.startCursor?.value == RelayHelper.Base64.toBase64("simple-cursor${toSkip}"))
assert (connection.pageInfo.endCursor?.value == RelayHelper.Base64.toBase64("simple-cursor${toSkip + resultSize - 1}"))
}

connection.edges.forEach({
assert (it.cursor.value == graphql.relay.Base64.toBase64("simple-cursor${it.node}"));
assert (it.cursor.value == RelayHelper.Base64.toBase64("simple-cursor${it.node}"));
});
}
}

0 comments on commit e924d44

Please sign in to comment.