forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyDataFetcher.java
63 lines (52 loc) · 2.08 KB
/
PropertyDataFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package graphql.schema;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.Map;
import static graphql.Scalars.GraphQLBoolean;
public class PropertyDataFetcher implements DataFetcher {
private final String propertyName;
public PropertyDataFetcher(String propertyName) {
this.propertyName = propertyName;
}
@Override
public Object get(DataFetchingEnvironment environment) {
Object source = environment.getSource();
if (source == null) return null;
if (source instanceof Map) {
return ((Map<?, ?>) source).get(propertyName);
}
return getPropertyViaGetter(source, environment.getFieldType());
}
private Object getPropertyViaGetter(Object object, GraphQLOutputType outputType) {
String prefix = isBooleanProperty(outputType) ? "is" : "get";
String getterName = prefix + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
try {
Method method = object.getClass().getMethod(getterName);
return method.invoke(object);
} catch (NoSuchMethodException e) {
return getFieldValue(object, outputType);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private boolean isBooleanProperty(GraphQLOutputType outputType) {
if (outputType == GraphQLBoolean) return true;
if (outputType instanceof GraphQLNonNull) {
return ((GraphQLNonNull) outputType).getWrappedType() == GraphQLBoolean;
}
return false;
}
private Object getFieldValue(Object object, GraphQLOutputType outputType) {
try {
Field field = object.getClass().getField(propertyName);
return field.get(object);
} catch (NoSuchFieldException e) {
return null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}