1212 */
1313package graphql .annotations .dataFetchers ;
1414
15- import graphql .annotations .annotationTypes .GraphQLBatched ;
16- import graphql .annotations .annotationTypes .GraphQLConstructor ;
17- import graphql .annotations .annotationTypes .GraphQLInvokeDetached ;
18- import graphql .annotations .annotationTypes .GraphQLName ;
19- import graphql .annotations .processor .ProcessingElementsContainer ;
20- import graphql .annotations .processor .typeFunctions .TypeFunction ;
21- import graphql .schema .*;
15+ import static graphql .annotations .processor .util .NamingKit .toGraphqlName ;
16+ import static graphql .annotations .processor .util .PrefixesUtil .addPrefixToPropertyName ;
17+ import static graphql .annotations .processor .util .PrefixesUtil .extractPrefixedName ;
18+ import static graphql .annotations .processor .util .ReflectionKit .constructNewInstance ;
19+ import static graphql .annotations .processor .util .ReflectionKit .newInstance ;
2220
23- import java .lang .reflect .*;
21+ import java .lang .reflect .Constructor ;
22+ import java .lang .reflect .Field ;
23+ import java .lang .reflect .InvocationTargetException ;
24+ import java .lang .reflect .Method ;
25+ import java .lang .reflect .Modifier ;
26+ import java .lang .reflect .Parameter ;
27+ import java .lang .reflect .ParameterizedType ;
28+ import java .lang .reflect .Type ;
2429import java .util .ArrayList ;
2530import java .util .Arrays ;
2631import java .util .List ;
2732import java .util .Map ;
33+ import java .util .Optional ;
2834
29- import static graphql .annotations .processor .util .NamingKit .toGraphqlName ;
30- import static graphql .annotations .processor .util .PrefixesUtil .addPrefixToPropertyName ;
31- import static graphql .annotations .processor .util .PrefixesUtil .extractPrefixedName ;
32- import static graphql .annotations .processor .util .ReflectionKit .constructNewInstance ;
33- import static graphql .annotations .processor .util .ReflectionKit .newInstance ;
35+ import graphql .annotations .annotationTypes .GraphQLBatched ;
36+ import graphql .annotations .annotationTypes .GraphQLConstructor ;
37+ import graphql .annotations .annotationTypes .GraphQLInvokeDetached ;
38+ import graphql .annotations .annotationTypes .GraphQLName ;
39+ import graphql .annotations .processor .ProcessingElementsContainer ;
40+ import graphql .annotations .processor .typeFunctions .TypeFunction ;
41+ import graphql .schema .DataFetcher ;
42+ import graphql .schema .DataFetchingEnvironment ;
43+ import graphql .schema .GraphQLInputObjectType ;
44+ import graphql .schema .GraphQLList ;
45+ import graphql .schema .GraphQLType ;
3446
3547
3648/**
@@ -111,33 +123,35 @@ private Object[] invocationArgs(DataFetchingEnvironment environment, ProcessingE
111123
112124 graphql .schema .GraphQLType graphQLType = typeFunction .buildType (true , paramType , p .getAnnotatedType (), container );
113125 if (envArgs .containsKey (parameterName )) {
114- result .add (buildArg (p .getParameterizedType (), graphQLType , envArgs .get (parameterName )));
126+ result .add (buildArg (p .getParameterizedType (), graphQLType , envArgs .containsKey ( parameterName ) ? Optional . ofNullable ( envArgs . get (parameterName )) : null ));
115127 } else {
116128 result .add (null );
117129 }
118130 }
119131 return result .toArray ();
120132 }
121133
122- private Object buildArg (Type p , GraphQLType graphQLType , Object arg ) {
134+ private Object buildArg (Type p , GraphQLType graphQLType , Optional < Object > arg ) {
123135 if (arg == null ) {
124136 return null ;
125137 }
126138 if (graphQLType instanceof graphql .schema .GraphQLNonNull ) {
127139 graphQLType = ((graphql .schema .GraphQLNonNull ) graphQLType ).getWrappedType ();
128140 }
141+
129142 if (p instanceof Class <?> && graphQLType instanceof GraphQLInputObjectType ) {
130143 Constructor <?> constructors [] = ((Class ) p ).getConstructors ();
131144 Constructor <?> constructor = getBuildArgConstructor (constructors );
132145 Parameter [] parameters = constructor .getParameters ();
133- if (parameters .length == 1 && parameters [0 ].getType ().isAssignableFrom (arg .getClass ())) {
134- return constructNewInstance (constructor , arg );
146+
147+ if (parameters .length == 1 && parameters [0 ].getType ().isAssignableFrom (arg .get ().getClass ())) {
148+ return constructNewInstance (constructor , arg .get ());
135149 } else {
136150 List <Object > objects = new ArrayList <>();
137- Map map = (Map ) arg ;
151+ Map map = (Map ) arg . get () ;
138152 for (Parameter parameter : parameters ) {
139153 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 )));
154+ objects .add (buildArg (parameter .getParameterizedType (), ((GraphQLInputObjectType ) graphQLType ).getField (name ).getType (), map .containsKey ( name ) ? Optional . ofNullable ( map . get (name )) : null ));
141155 }
142156 return constructNewInstance (constructor , objects .toArray (new Object [objects .size ()]));
143157 }
@@ -146,13 +160,20 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
146160 Type subType = ((ParameterizedType ) p ).getActualTypeArguments ()[0 ];
147161 GraphQLType wrappedType = ((GraphQLList ) graphQLType ).getWrappedType ();
148162
149- for (Object item : ((List ) arg )) {
150- list .add (buildArg (subType , wrappedType , item ));
163+ for (Object item : ((List ) arg . get () )) {
164+ list .add (buildArg (subType , wrappedType , Optional . ofNullable ( item ) ));
151165 }
152166
153167 return list ;
168+ } else if (p instanceof ParameterizedType && ((ParameterizedType ) p ).getRawType () == Optional .class ) {
169+ Type subType = ((ParameterizedType ) p ).getActualTypeArguments ()[0 ];
170+ if (arg == null ) {
171+ return null ;
172+ } else {
173+ return Optional .ofNullable (buildArg (subType , new GraphQLUndefined (), arg ));
174+ }
154175 } else {
155- return arg ;
176+ return arg . get () ;
156177 }
157178 }
158179
0 commit comments