1414import graphql .schema .GraphQLNonNull ;
1515import graphql .schema .GraphQLScalarType ;
1616import graphql .schema .GraphQLType ;
17+ import graphql .schema .PropertyDataFetcherHelper ;
1718import graphql .util .FpKit ;
1819
19- import java .beans .BeanInfo ;
20- import java .beans .IntrospectionException ;
21- import java .beans .Introspector ;
22- import java .beans .PropertyDescriptor ;
23- import java .lang .reflect .InvocationTargetException ;
24- import java .lang .reflect .Method ;
2520import java .math .BigDecimal ;
2621import java .math .BigInteger ;
2722import java .util .ArrayList ;
28- import java .util .LinkedHashMap ;
2923import java .util .List ;
30- import java .util .Map ;
3124
3225import static graphql .schema .GraphQLTypeUtil .isList ;
3326import static graphql .schema .GraphQLTypeUtil .isNonNull ;
@@ -37,7 +30,7 @@ public class AstValueHelper {
3730
3831 /**
3932 * Produces a GraphQL Value AST given a Java value.
40- *
33+ * <p>
4134 * A GraphQL type must be provided, which will be used to interpret different
4235 * Java values.
4336 *
@@ -54,10 +47,9 @@ public class AstValueHelper {
5447 *
5548 * @param value - the java value to be converted into graphql ast
5649 * @param type the graphql type of the object
57- *
58- * @return a graphql language ast {@link Value}
50+ * @return a grapql language ast {@link Value}
5951 */
60- public static Value astFromValue (Object value , GraphQLType type ) {
52+ public static Value <?> astFromValue (Object value , GraphQLType type ) {
6153 if (value == null ) {
6254 return null ;
6355 }
@@ -118,37 +110,39 @@ public static Value astFromValue(Object value, GraphQLType type) {
118110 throw new AssertException ("'Cannot convert value to AST: " + serialized );
119111 }
120112
121- private static Value handleInputObject (Object _value , GraphQLInputObjectType type ) {
122- Map mapValue = objToMap (_value );
113+ private static Value <?> handleInputObject (Object javaValue , GraphQLInputObjectType type ) {
123114 List <GraphQLInputObjectField > fields = type .getFields ();
124115 List <ObjectField > fieldNodes = new ArrayList <>();
125116 fields .forEach (field -> {
117+ String fieldName = field .getName ();
126118 GraphQLInputType fieldType = field .getType ();
127- Value nodeValue = astFromValue (mapValue .get (field .getName ()), fieldType );
119+ Object fieldValueObj = PropertyDataFetcherHelper .getPropertyValue (fieldName , javaValue , fieldType );
120+ Value <?> nodeValue = astFromValue (fieldValueObj , fieldType );
128121 if (nodeValue != null ) {
129122
130- fieldNodes .add (ObjectField .newObjectField ().name (field . getName () ).value (nodeValue ).build ());
123+ fieldNodes .add (ObjectField .newObjectField ().name (fieldName ).value (nodeValue ).build ());
131124 }
132125 });
133126 return ObjectValue .newObjectValue ().objectFields (fieldNodes ).build ();
134127 }
135128
136- private static Value handleNumber (String stringValue ) {
129+ private static Value <?> handleNumber (String stringValue ) {
137130 if (stringValue .matches ("^[0-9]+$" )) {
138131 return IntValue .newIntValue ().value (new BigInteger (stringValue )).build ();
139132 } else {
140133 return FloatValue .newFloatValue ().value (new BigDecimal (stringValue )).build ();
141134 }
142135 }
143136
144- private static Value handleList (Object _value , GraphQLList type ) {
137+ @ SuppressWarnings ("rawtypes" )
138+ private static Value <?> handleList (Object _value , GraphQLList type ) {
145139 GraphQLType itemType = type .getWrappedType ();
146140 boolean isIterable = _value instanceof Iterable ;
147141 if (isIterable || (_value != null && _value .getClass ().isArray ())) {
148- Iterable iterable = isIterable ? (Iterable ) _value : FpKit .toCollection (_value );
142+ Iterable <?> iterable = isIterable ? (Iterable <?> ) _value : FpKit .toCollection (_value );
149143 List <Value > valuesNodes = new ArrayList <>();
150144 for (Object item : iterable ) {
151- Value itemNode = astFromValue (item , itemType );
145+ Value <?> itemNode = astFromValue (item , itemType );
152146 if (itemNode != null ) {
153147 valuesNodes .add (itemNode );
154148 }
@@ -158,7 +152,7 @@ private static Value handleList(Object _value, GraphQLList type) {
158152 return astFromValue (_value , itemType );
159153 }
160154
161- private static Value handleNonNull (Object _value , GraphQLNonNull type ) {
155+ private static Value <?> handleNonNull (Object _value , GraphQLNonNull type ) {
162156 GraphQLType wrappedType = type .getWrappedType ();
163157 return astFromValue (_value , wrappedType );
164158 }
@@ -178,37 +172,17 @@ private static boolean isNullish(Object serialized) {
178172 return serialized == null ;
179173 }
180174
181- private static Map objToMap (Object value ) {
182- if (value instanceof Map ) {
183- return (Map ) value ;
184- }
185- // java bean inspector
186- Map <String , Object > result = new LinkedHashMap <>();
187- try {
188- BeanInfo info = Introspector .getBeanInfo (value .getClass ());
189- for (PropertyDescriptor pd : info .getPropertyDescriptors ()) {
190- Method reader = pd .getReadMethod ();
191- if (reader != null )
192- result .put (pd .getName (), reader .invoke (value ));
193- }
194- } catch (IntrospectionException | InvocationTargetException | IllegalAccessException e ) {
195- throw new GraphQLException (e );
196- }
197- return result ;
198- }
199175
200176 /**
201177 * Parses an AST value literal into the correct {@link graphql.language.Value} which
202178 * MUST be of the correct shape eg '"string"' or 'true' or '1' or '{ "object", "form" }'
203179 * or '[ "array", "form" ]' otherwise an exception is thrown
204180 *
205181 * @param astLiteral the string to parse an AST literal
206- *
207182 * @return a valid Value
208- *
209183 * @throws graphql.AssertException if the input can be parsed
210184 */
211- public static Value valueFromAst (String astLiteral ) {
185+ public static Value <?> valueFromAst (String astLiteral ) {
212186 // we use the parser to give us the AST elements as if we defined an inputType
213187 String toParse = "input X { x : String = " + astLiteral + "}" ;
214188 try {
0 commit comments