Skip to content

Commit 56f4965

Browse files
committed
Moved all validation into the validator.Now it also checks that the
@GraphQLConnection is used correctly on fields
1 parent 7ebd554 commit 56f4965

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
*/
1515
package graphql.annotations.connection;
1616

17-
import graphql.annotations.ExtensionDataFetcherWrapper;
1817
import graphql.relay.Connection;
1918
import graphql.schema.DataFetcher;
2019
import graphql.schema.DataFetchingEnvironment;
21-
import graphql.schema.FieldDataFetcher;
22-
import graphql.schema.PropertyDataFetcher;
2320

2421
import java.lang.reflect.Constructor;
2522
import java.util.Arrays;
26-
import java.util.List;
2723
import java.util.Optional;
2824

2925
import static graphql.annotations.ReflectionKit.constructNewInstance;
@@ -32,10 +28,8 @@ public class ConnectionDataFetcher<T> implements DataFetcher<Connection<T>> {
3228
private final Class<? extends ConnectionFetcher<T>> connection;
3329
private final DataFetcher<PaginatedData<T>> actualDataFetcher;
3430
private final Constructor<ConnectionFetcher<T>> constructor;
35-
private final List<Class<?>> blackListOfDataFetchers = Arrays.asList(PropertyDataFetcher.class, FieldDataFetcher.class);
3631

3732
public ConnectionDataFetcher(Class<? extends ConnectionFetcher<T>> connection, DataFetcher<T> actualDataFetcher) {
38-
validateDataFetcher(actualDataFetcher);
3933
this.connection = connection;
4034
this.actualDataFetcher = (DataFetcher<PaginatedData<T>>) actualDataFetcher;
4135
Optional<Constructor<ConnectionFetcher<T>>> constructor =
@@ -50,17 +44,6 @@ public ConnectionDataFetcher(Class<? extends ConnectionFetcher<T>> connection, D
5044
}
5145
}
5246

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 without @GraphQLDataFetcher, because " +
60-
"neither PropertyDataFetcher nor FieldDataFetcher know how to handle connection");
61-
}
62-
}
63-
6447
@Override
6548
public Connection<T> get(DataFetchingEnvironment environment) {
6649
ConnectionFetcher<T> conn = constructNewInstance(constructor, actualDataFetcher);

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
*/
1515
package graphql.annotations.connection;
1616

17+
import graphql.annotations.GraphQLDataFetcher;
18+
1719
import java.lang.reflect.AccessibleObject;
1820
import java.lang.reflect.Field;
1921
import java.lang.reflect.Method;
2022

2123
public class ConnectionTypeValidator {
2224

2325
public void validate(AccessibleObject field) {
24-
if(field instanceof Field) {
25-
if(!PaginatedData.class.isAssignableFrom(((Field)field).getType())) {
26+
if (field instanceof Field) {
27+
if (field.isAnnotationPresent(GraphQLConnection.class) && !field.isAnnotationPresent(GraphQLDataFetcher.class)) {
28+
throw new GraphQLConnectionException("Please don't use @GraphQLConnection on" + ((Field) field).getName() +
29+
" without @GraphQLDataFetcher, because " +
30+
"neither PropertyDataFetcher nor FieldDataFetcher know how to handle connection");
31+
}
32+
33+
if (!PaginatedData.class.isAssignableFrom(((Field) field).getType())) {
2634
throw new GraphQLConnectionException(((Field) field).getName() + " type must be PaginatedData");
2735
}
28-
}
29-
else {
30-
if(!PaginatedData.class.isAssignableFrom(((Method)field).getReturnType())){
36+
} else {
37+
if (!PaginatedData.class.isAssignableFrom(((Method) field).getReturnType())) {
3138
throw new GraphQLConnectionException(((Method) field).getName() + " return type must be PaginatedData");
3239
}
3340
}

0 commit comments

Comments
 (0)