1
1
/**
2
- * Copyright 2016 Yurii Rashkovskii
3
- *
4
2
* Licensed under the Apache License, Version 2.0 (the "License");
5
3
* you may not use this file except in compliance with the License.
6
4
* You may obtain a copy of the License at
15
13
package graphql .annotations .dataFetchers ;
16
14
17
15
import graphql .annotations .annotationTypes .GraphQLBatched ;
16
+ import graphql .annotations .annotationTypes .GraphQLConstructor ;
18
17
import graphql .annotations .annotationTypes .GraphQLInvokeDetached ;
19
18
import graphql .annotations .annotationTypes .GraphQLName ;
20
19
import graphql .annotations .processor .ProcessingElementsContainer ;
@@ -68,10 +67,9 @@ public MethodDataFetcher(Method method, TypeFunction typeFunction, ProcessingEle
68
67
public T get (DataFetchingEnvironment environment ) {
69
68
try {
70
69
T obj ;
71
- if (Modifier .isStatic (method .getModifiers ())){
70
+ if (Modifier .isStatic (method .getModifiers ())) {
72
71
return (T ) method .invoke (null , invocationArgs (environment , container ));
73
- }
74
- else if (method .isAnnotationPresent (GraphQLBatched .class ) || method .isAnnotationPresent (GraphQLInvokeDetached .class )) {
72
+ } else if (method .isAnnotationPresent (GraphQLBatched .class ) || method .isAnnotationPresent (GraphQLInvokeDetached .class )) {
75
73
obj = newInstance ((Class <T >) method .getDeclaringClass ());
76
74
} else if (!method .getDeclaringClass ().isInstance (environment .getSource ())) {
77
75
obj = newInstance ((Class <T >) method .getDeclaringClass (), environment .getSource ());
@@ -130,21 +128,19 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
130
128
}
131
129
if (p instanceof Class <?> && graphQLType instanceof GraphQLInputObjectType ) {
132
130
Constructor <?> constructors [] = ((Class ) p ).getConstructors ();
133
- for (Constructor <?> constructor : constructors ) {
134
- Parameter [] parameters = constructor .getParameters ();
135
- if (parameters .length == 1 && parameters [0 ].getType ().isAssignableFrom (arg .getClass ())) {
136
- return constructNewInstance (constructor , arg );
137
- } else {
138
- List <Object > objects = new ArrayList <>();
139
- Map map = (Map ) arg ;
140
- for (Parameter parameter : parameters ) {
141
- String name = toGraphqlName (parameter .getAnnotation (GraphQLName .class ) != null ? parameter .getAnnotation (GraphQLName .class ).value () : parameter .getName ());
142
- objects .add (buildArg (parameter .getParameterizedType (), ((GraphQLInputObjectType ) graphQLType ).getField (name ).getType (), map .get (name )));
143
- }
144
- return constructNewInstance (constructor , objects .toArray (new Object [objects .size ()]));
131
+ Constructor <?> constructor = getBuildArgConstructor (constructors );
132
+ Parameter [] parameters = constructor .getParameters ();
133
+ if (parameters .length == 1 && parameters [0 ].getType ().isAssignableFrom (arg .getClass ())) {
134
+ return constructNewInstance (constructor , arg );
135
+ } else {
136
+ List <Object > objects = new ArrayList <>();
137
+ Map map = (Map ) arg ;
138
+ for (Parameter parameter : parameters ) {
139
+ String name = toGraphqlName (parameter .getAnnotation (GraphQLName .class ) != null ? parameter .getAnnotation (GraphQLName .class ).value () : parameter .getName ());
140
+ objects .add (buildArg (parameter .getParameterizedType (), ((GraphQLInputObjectType ) graphQLType ).getField (name ).getType (), map .get (name )));
145
141
}
142
+ return constructNewInstance (constructor , objects .toArray (new Object [objects .size ()]));
146
143
}
147
- return null ;
148
144
} else if (p instanceof ParameterizedType && graphQLType instanceof GraphQLList ) {
149
145
List <Object > list = new ArrayList <>();
150
146
Type subType = ((ParameterizedType ) p ).getActualTypeArguments ()[0 ];
@@ -160,6 +156,24 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
160
156
}
161
157
}
162
158
159
+
160
+ /***
161
+ * return the constructor to call in order to build the object
162
+ * @param constructors Object constructors
163
+ * @return the annotated constructor if present else return the first constructor
164
+ */
165
+ private Constructor getBuildArgConstructor (Constructor <?> constructors []) {
166
+ if (constructors != null ) {
167
+ for (Constructor <?> constructor : constructors ) {
168
+ if (constructor .isAnnotationPresent (GraphQLConstructor .class )) {
169
+ return constructor ;
170
+ }
171
+ }
172
+ return constructors [0 ];
173
+ }
174
+ return null ;
175
+ }
176
+
163
177
private Object getGraphQLFieldValue (Object source , String fieldName ) throws IllegalAccessException , NoSuchFieldException , InvocationTargetException {
164
178
Object methodValue = getValueFromMethod (source , fieldName );
165
179
if (methodValue != null ) return methodValue ;
0 commit comments