|
12 | 12 | */ |
13 | 13 | package graphql.annotations.dataFetchers; |
14 | 14 |
|
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; |
22 | 20 |
|
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; |
24 | 29 | import java.util.ArrayList; |
25 | 30 | import java.util.Arrays; |
| 31 | +import java.util.Collections; |
26 | 32 | import java.util.List; |
27 | 33 | import java.util.Map; |
| 34 | +import java.util.Optional; |
28 | 35 |
|
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; |
| 36 | +import graphql.annotations.annotationTypes.GraphQLBatched; |
| 37 | +import graphql.annotations.annotationTypes.GraphQLConstructor; |
| 38 | +import graphql.annotations.annotationTypes.GraphQLInvokeDetached; |
| 39 | +import graphql.annotations.annotationTypes.GraphQLName; |
| 40 | +import graphql.annotations.processor.ProcessingElementsContainer; |
| 41 | +import graphql.annotations.processor.typeFunctions.TypeFunction; |
| 42 | +import graphql.schema.DataFetcher; |
| 43 | +import graphql.schema.DataFetchingEnvironment; |
| 44 | +import graphql.schema.GraphQLInputObjectType; |
| 45 | +import graphql.schema.GraphQLList; |
| 46 | +import graphql.schema.GraphQLType; |
34 | 47 |
|
35 | 48 |
|
36 | 49 | /** |
@@ -111,48 +124,74 @@ private Object[] invocationArgs(DataFetchingEnvironment environment, ProcessingE |
111 | 124 |
|
112 | 125 | graphql.schema.GraphQLType graphQLType = typeFunction.buildType(true, paramType, p.getAnnotatedType(), container); |
113 | 126 | if (envArgs.containsKey(parameterName)) { |
114 | | - result.add(buildArg(p.getParameterizedType(), graphQLType, envArgs.get(parameterName))); |
| 127 | + result.add(buildArg(p.getParameterizedType(), graphQLType, envArgs.containsKey(parameterName) ? Optional.ofNullable(envArgs.get(parameterName)) : null)); |
115 | 128 | } else { |
116 | 129 | result.add(null); |
117 | 130 | } |
118 | 131 | } |
119 | 132 | return result.toArray(); |
120 | 133 | } |
121 | 134 |
|
122 | | - private Object buildArg(Type p, GraphQLType graphQLType, Object arg) { |
| 135 | + @SuppressWarnings("ConstantConditions") |
| 136 | + private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) { |
123 | 137 | if (arg == null) { |
124 | 138 | return null; |
125 | 139 | } |
126 | 140 | if (graphQLType instanceof graphql.schema.GraphQLNonNull) { |
127 | 141 | graphQLType = ((graphql.schema.GraphQLNonNull) graphQLType).getWrappedType(); |
128 | 142 | } |
| 143 | + |
129 | 144 | if (p instanceof Class<?> && graphQLType instanceof GraphQLInputObjectType) { |
130 | | - Constructor<?> constructors[] = ((Class) p).getConstructors(); |
| 145 | + Constructor<?>[] constructors = ((Class) p).getConstructors(); |
131 | 146 | Constructor<?> constructor = getBuildArgConstructor(constructors); |
132 | 147 | Parameter[] parameters = constructor.getParameters(); |
133 | | - if (parameters.length == 1 && parameters[0].getType().isAssignableFrom(arg.getClass())) { |
134 | | - return constructNewInstance(constructor, arg); |
| 148 | + |
| 149 | + if (parameters.length == 1 && arg.isPresent() && parameters[0].getType().isAssignableFrom(arg.get().getClass())) { |
| 150 | + if (parameters[0].getType().isAssignableFrom(Optional.class)) { |
| 151 | + return constructNewInstance(constructor, arg); |
| 152 | + } else { |
| 153 | + return constructNewInstance(constructor, arg.orElse(null)); |
| 154 | + } |
135 | 155 | } else { |
136 | 156 | List<Object> objects = new ArrayList<>(); |
137 | | - Map map = (Map) arg; |
| 157 | + Map map = (Map) arg.orElseGet(Collections::emptyMap); |
138 | 158 | for (Parameter parameter : parameters) { |
139 | 159 | 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))); |
| 160 | + if (!map.containsKey(name)) { |
| 161 | + objects.add(null); |
| 162 | + } else { |
| 163 | + objects.add(buildArg(parameter.getParameterizedType(), ((GraphQLInputObjectType) graphQLType).getField(name).getType(), Optional.ofNullable(map.get(name)))); |
| 164 | + } |
141 | 165 | } |
142 | 166 | return constructNewInstance(constructor, objects.toArray(new Object[objects.size()])); |
143 | 167 | } |
144 | 168 | } else if (p instanceof ParameterizedType && graphQLType instanceof GraphQLList) { |
145 | | - List<Object> list = new ArrayList<>(); |
146 | | - Type subType = ((ParameterizedType) p).getActualTypeArguments()[0]; |
147 | | - GraphQLType wrappedType = ((GraphQLList) graphQLType).getWrappedType(); |
| 169 | + if (((ParameterizedType) p).getRawType() == Optional.class) { |
| 170 | + if (arg == null) { |
| 171 | + return null; |
| 172 | + } else { |
| 173 | + Type subType = ((ParameterizedType) p).getActualTypeArguments()[0]; |
| 174 | + return Optional.ofNullable(buildArg(subType, graphQLType, arg)); |
| 175 | + } |
| 176 | + } else { |
| 177 | + List<Object> list = new ArrayList<>(); |
| 178 | + Type subType = ((ParameterizedType) p).getActualTypeArguments()[0]; |
| 179 | + GraphQLType wrappedType = ((GraphQLList) graphQLType).getWrappedType(); |
148 | 180 |
|
149 | | - for (Object item : ((List) arg)) { |
150 | | - list.add(buildArg(subType, wrappedType, item)); |
| 181 | + for (Object item : ((List) arg.orElseGet(Collections::emptyList))) { |
| 182 | + list.add(buildArg(subType, wrappedType, Optional.ofNullable(item))); |
| 183 | + } |
| 184 | + return list; |
| 185 | + } |
| 186 | + } else if (p instanceof ParameterizedType && ((ParameterizedType) p).getRawType() == Optional.class) { |
| 187 | + Type subType = ((ParameterizedType) p).getActualTypeArguments()[0]; |
| 188 | + if (arg == null) { |
| 189 | + return null; |
| 190 | + } else { |
| 191 | + return Optional.ofNullable(buildArg(subType, new GraphQLUndefined(), arg)); |
151 | 192 | } |
152 | | - |
153 | | - return list; |
154 | 193 | } else { |
155 | | - return arg; |
| 194 | + return arg.orElse(null); |
156 | 195 | } |
157 | 196 | } |
158 | 197 |
|
|
0 commit comments