Skip to content

Commit a76155f

Browse files
authored
Merge pull request #95 from Jahia/connections-fixes
Relay connections : support recursive and interfaces connection, pass arguments to DataFetcher
2 parents a2304fa + ba4fda2 commit a76155f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
8282

8383
private static final Relay RELAY_TYPES = new Relay();
8484

85+
private static final List<Class> TYPES_FOR_CONNECTION = Arrays.asList(GraphQLObjectType.class, GraphQLInterfaceType.class, GraphQLUnionType.class, GraphQLTypeReference.class);
86+
8587
private Map<String, graphql.schema.GraphQLType> typeRegistry = new HashMap<>();
8688
private final Stack<String> processing = new Stack<>();
8789

@@ -510,8 +512,7 @@ private GraphQLOutputType getGraphQLConnection(boolean isConnection, AccessibleO
510512

511513
private boolean isConnection(AccessibleObject obj, Class<?> klass, GraphQLOutputType type) {
512514
return obj.isAnnotationPresent(GraphQLConnection.class) &&
513-
type instanceof GraphQLList &&
514-
((GraphQLList) type).getWrappedType() instanceof GraphQLObjectType;
515+
type instanceof GraphQLList && TYPES_FOR_CONNECTION.stream().anyMatch(aClass -> aClass.isInstance(((GraphQLList) type).getWrappedType()));
515516
}
516517

517518
protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotationsException {
@@ -707,8 +708,10 @@ public ConnectionDataFetcher(Class<? extends Connection> connection, DataFetcher
707708

708709
@Override
709710
public Object get(DataFetchingEnvironment environment) {
710-
// Exclude arguments
711-
DataFetchingEnvironment env = new DataFetchingEnvironmentImpl(environment.getSource(), new HashMap<>(), environment.getContext(),
711+
// Create a list of arguments with connection specific arguments excluded
712+
HashMap<String, Object> arguments = new HashMap<>(environment.getArguments());
713+
arguments.keySet().removeAll(Arrays.asList("first", "last", "before", "after"));
714+
DataFetchingEnvironment env = new DataFetchingEnvironmentImpl(environment.getSource(), arguments, environment.getContext(),
712715
environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(),
713716
environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet());
714717
Connection conn = constructNewInstance(constructor, actualDataFetcher.get(env));

src/test/java/graphql/annotations/GraphQLConnectionTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public Stream<Obj> getObjStream() {
8888
Obj[] a = new Obj[objs.size()];
8989
return Stream.of(objs.toArray(a));
9090
}
91+
92+
@GraphQLField
93+
@GraphQLConnection(name = "objStreamWithParam")
94+
public Stream<Obj> getObjStreamWithParam(@GraphQLName("filter") String filter) {
95+
return this.objs.stream().filter( obj -> obj.val.startsWith(filter));
96+
}
97+
98+
9199
}
92100

93101
@Test
@@ -128,6 +136,27 @@ public void methodStream() {
128136
testResult("objStream", result);
129137
}
130138

139+
@Test
140+
public void methodListWithParam() {
141+
GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class);
142+
GraphQLSchema schema = newSchema().query(object).build();
143+
144+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
145+
ExecutionResult result = graphQL.execute("{ objStreamWithParam(first: 2, filter:\"hel\") { edges { cursor node { id, val } } } }",
146+
new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"), new Obj("4", "hello world"), new Obj("5", "hello again"))));
147+
148+
assertTrue(result.getErrors().isEmpty());
149+
150+
Map<String, Map<String, List<Map<String, Map<String, Object>>>>> data = result.getData();
151+
List<Map<String, Map<String, Object>>> edges = data.get("objStreamWithParam").get("edges");
152+
153+
assertEquals(edges.size(), 2);
154+
assertEquals(edges.get(0).get("node").get("id"), "2");
155+
assertEquals(edges.get(0).get("node").get("val"), "hello");
156+
assertEquals(edges.get(1).get("node").get("id"), "4");
157+
assertEquals(edges.get(1).get("node").get("val"), "hello world");
158+
}
159+
131160

132161
public static class CustomConnection implements Connection {
133162

0 commit comments

Comments
 (0)