Skip to content

Commit e65fcce

Browse files
committed
fixing code review notes
1 parent 5918fda commit e65fcce

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/main/java/graphql/annotations/dataFetchers/MethodDataFetcher.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,32 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
137137
}
138138

139139
private Object getGraphQLFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException, InvocationTargetException {
140+
Object methodValue = getValueFromMethod(source, fieldName);
141+
if (methodValue != null) return methodValue;
142+
143+
Field field = getField(source.getClass(), fieldName);
144+
if (getValueFromField(field)) return field.get(source);
145+
146+
throw new NoSuchFieldException("No GraphQL field found");
147+
}
148+
149+
private boolean getValueFromField(Field field) throws IllegalAccessException {
150+
if (field != null) {
151+
field.setAccessible(true);
152+
return true;
153+
}
154+
return false;
155+
}
156+
157+
private Object getValueFromMethod(Object source, String fieldName) throws IllegalAccessException, InvocationTargetException {
140158
String[] orderedPrefixes = new String[]{"", "get", "is"};
141159
for (String orderedPrefix : orderedPrefixes) {
142160
Method method = getMethod(source.getClass(), fieldName, orderedPrefix);
143161
if (method != null) {
144162
return method.invoke(source);
145163
}
146164
}
147-
148-
Field field = getField(source.getClass(), fieldName);
149-
if (field != null) {
150-
field.setAccessible(true);
151-
return field.get(source);
152-
} else {
153-
throw new NoSuchFieldException("No GraphQL field found");
154-
}
165+
return null;
155166
}
156167

157168
private Method getMethod(Class<?> clazz, String name, String prefix) {

0 commit comments

Comments
 (0)