Skip to content

Commit 442f344

Browse files
committed
refactor annotation
1 parent 19552ea commit 442f344

18 files changed

+1014
-1273
lines changed

src/main/java/graphql/annotations/BatchedMethodDataFetcher.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@
2121
import java.lang.reflect.Modifier;
2222

2323
public class BatchedMethodDataFetcher extends MethodDataFetcher {
24-
public BatchedMethodDataFetcher(Method method) {
25-
super(method);
26-
if (!Modifier.isStatic(method.getModifiers())) {
27-
throw new IllegalArgumentException("Batched method should be static");
28-
}
29-
}
3024

31-
public BatchedMethodDataFetcher(Method method, TypeFunction typeFunction) {
32-
super(method, typeFunction);
25+
26+
public BatchedMethodDataFetcher(Method method,TypeFunction typeFunction, ProcessingElementsContainer container) {
27+
super(method,typeFunction, container);
3328
if (!Modifier.isStatic(method.getModifiers())) {
3429
throw new IllegalArgumentException("Batched method should be static");
3530
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations;
16+
17+
import graphql.schema.DataFetcher;
18+
import graphql.schema.DataFetchingEnvironment;
19+
import graphql.schema.DataFetchingEnvironmentImpl;
20+
21+
import java.lang.reflect.Constructor;
22+
import java.util.Arrays;
23+
import java.util.HashMap;
24+
import java.util.Optional;
25+
26+
import static graphql.annotations.ReflectionKit.constructNewInstance;
27+
28+
class ConnectionDataFetcher implements DataFetcher {
29+
private final Class<? extends Connection> connection;
30+
private final DataFetcher actualDataFetcher;
31+
private final Constructor<Connection> constructor;
32+
33+
public ConnectionDataFetcher(Class<? extends Connection> connection, DataFetcher actualDataFetcher) {
34+
this.connection = connection;
35+
Optional<Constructor<Connection>> constructor =
36+
Arrays.asList(connection.getConstructors()).stream().
37+
filter(c -> c.getParameterCount() == 1).
38+
map(c -> (Constructor<Connection>) c).
39+
findFirst();
40+
if (constructor.isPresent()) {
41+
this.constructor = constructor.get();
42+
} else {
43+
throw new IllegalArgumentException(connection + " doesn't have a single argument constructor");
44+
}
45+
this.actualDataFetcher = actualDataFetcher;
46+
}
47+
48+
@Override
49+
public Object get(DataFetchingEnvironment environment) {
50+
// Create a list of arguments with connection specific arguments excluded
51+
HashMap<String, Object> arguments = new HashMap<>(environment.getArguments());
52+
arguments.keySet().removeAll(Arrays.asList("first", "last", "before", "after"));
53+
DataFetchingEnvironment env = new DataFetchingEnvironmentImpl(environment.getSource(), arguments, environment.getContext(),
54+
environment.getRoot(), environment.getFieldDefinition(), environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(),
55+
environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet(), environment.getFieldTypeInfo());
56+
Object data = actualDataFetcher.get(env);
57+
if (data != null) {
58+
Connection conn = constructNewInstance(constructor, data);
59+
return conn.get(environment);
60+
}
61+
return null;
62+
}
63+
}

0 commit comments

Comments
 (0)