Skip to content

Commit 7653b6b

Browse files
committed
validate that the dataFetcher is not a Property or Field data fetcher
1 parent 4755803 commit 7653b6b

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/main/java/graphql/annotations/connection/ConnectionDataFetcher.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@
1414
*/
1515
package graphql.annotations.connection;
1616

17+
import graphql.annotations.ExtensionDataFetcherWrapper;
1718
import graphql.relay.Connection;
18-
import graphql.relay.DefaultConnection;
19-
import graphql.relay.DefaultPageInfo;
2019
import graphql.schema.DataFetcher;
2120
import graphql.schema.DataFetchingEnvironment;
21+
import graphql.schema.FieldDataFetcher;
22+
import graphql.schema.PropertyDataFetcher;
2223

2324
import java.lang.reflect.Constructor;
2425
import java.util.Arrays;
25-
import java.util.Collections;
26+
import java.util.List;
2627
import java.util.Optional;
2728

2829
import static graphql.annotations.ReflectionKit.constructNewInstance;
2930

3031
public class ConnectionDataFetcher<T> implements DataFetcher<Connection<T>> {
3132
private final Class<? extends ConnectionFetcher<T>> connection;
32-
private final DataFetcher<T> actualDataFetcher;
33-
private final Constructor<ConnectionFetcher> constructor;
33+
private final DataFetcher<PaginatedData<T>> actualDataFetcher;
34+
private final Constructor<ConnectionFetcher<T>> constructor;
35+
private final List<Class<?>> blackListOfDataFetchers = Arrays.asList(PropertyDataFetcher.class, FieldDataFetcher.class);
3436

3537
public ConnectionDataFetcher(Class<? extends ConnectionFetcher<T>> connection, DataFetcher<T> actualDataFetcher) {
38+
validateDataFetcher(actualDataFetcher);
3639
this.connection = connection;
37-
this.actualDataFetcher = actualDataFetcher;
38-
ValidateDataFetcher();
39-
Optional<Constructor<ConnectionFetcher>> constructor =
40-
Arrays.asList(this.connection.getConstructors()).stream().
40+
this.actualDataFetcher = (DataFetcher<PaginatedData<T>>) actualDataFetcher;
41+
Optional<Constructor<ConnectionFetcher<T>>> constructor =
42+
Arrays.stream(this.connection.getConstructors()).
4143
filter(c -> c.getParameterCount() == 1).
42-
map(c -> (Constructor<ConnectionFetcher>) c).
44+
map(c -> (Constructor<ConnectionFetcher<T>>) c).
4345
findFirst();
4446
if (constructor.isPresent()) {
4547
this.constructor = constructor.get();
@@ -48,27 +50,20 @@ public ConnectionDataFetcher(Class<? extends ConnectionFetcher<T>> connection, D
4850
}
4951
}
5052

51-
private void ValidateDataFetcher() {
52-
if(connection.isAssignableFrom(EnhancedConnectionFetcher.class)) {
53-
if(!(actualDataFetcher instanceof PaginationDataFetcher)) {
54-
throw new GraphQLConnectionException(actualDataFetcher.getClass().getSimpleName() + " must extends PaginationDataFetcher");
55-
}
53+
private void validateDataFetcher(DataFetcher<?> dataFetcher) {
54+
if( dataFetcher instanceof ExtensionDataFetcherWrapper) {
55+
dataFetcher = ((ExtensionDataFetcherWrapper) dataFetcher).getUnwrappedDataFetcher();
56+
}
57+
final DataFetcher<?> finalDataFetcher = dataFetcher;
58+
if(blackListOfDataFetchers.stream().anyMatch(aClass -> aClass.isInstance(finalDataFetcher))) {
59+
throw new GraphQLConnectionException("Please don't use @GraphQLConnection on a field, because " +
60+
"neither PropertyDataFetcher nor FieldDataFetcher know how to handle connection");
5661
}
5762
}
5863

5964
@Override
6065
public Connection<T> get(DataFetchingEnvironment environment) {
61-
if(connection.isAssignableFrom(EnhancedConnectionFetcher.class)) {
62-
ConnectionFetcher<Connection<T>> conn = constructNewInstance(constructor, actualDataFetcher);
63-
return conn.get(environment);
64-
}
65-
else {
66-
T data = actualDataFetcher.get(environment);
67-
if (data != null) {
68-
ConnectionFetcher<Connection<T>> conn = constructNewInstance(constructor, data);
69-
return conn.get(environment);
70-
}
71-
return new DefaultConnection<>(Collections.emptyList(), new DefaultPageInfo(null, null, false, false));
72-
}
66+
ConnectionFetcher<T> conn = constructNewInstance(constructor, actualDataFetcher);
67+
return conn.get(environment);
7368
}
7469
}

0 commit comments

Comments
 (0)