1414 */
1515package graphql .annotations .connection ;
1616
17+ import graphql .annotations .ExtensionDataFetcherWrapper ;
1718import graphql .relay .Connection ;
18- import graphql .relay .DefaultConnection ;
19- import graphql .relay .DefaultPageInfo ;
2019import graphql .schema .DataFetcher ;
2120import graphql .schema .DataFetchingEnvironment ;
21+ import graphql .schema .FieldDataFetcher ;
22+ import graphql .schema .PropertyDataFetcher ;
2223
2324import java .lang .reflect .Constructor ;
2425import java .util .Arrays ;
25- import java .util .Collections ;
26+ import java .util .List ;
2627import java .util .Optional ;
2728
2829import static graphql .annotations .ReflectionKit .constructNewInstance ;
2930
3031public 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