Skip to content

Commit 988bee2

Browse files
author
gursajan
committed
fixed merge differences
1 parent b6dd40c commit 988bee2

2 files changed

Lines changed: 41 additions & 49 deletions

File tree

src/main/java/graphql/language/AstValueHelper.java

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414
import graphql.schema.GraphQLNonNull;
1515
import graphql.schema.GraphQLScalarType;
1616
import graphql.schema.GraphQLType;
17+
import graphql.schema.PropertyDataFetcherHelper;
1718
import 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;
2520
import java.math.BigDecimal;
2621
import java.math.BigInteger;
2722
import java.util.ArrayList;
28-
import java.util.LinkedHashMap;
2923
import java.util.List;
30-
import java.util.Map;
3124

3225
import static graphql.schema.GraphQLTypeUtil.isList;
3326
import 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 {

src/test/groovy/graphql/language/AstValueHelperTest.groovy

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ class AstValueHelperTest extends Specification {
108108
astFromValue('HELLO', myEnum).isEqualTo(new EnumValue('HELLO'))
109109

110110
astFromValue(complexValue, myEnum).isEqualTo(new EnumValue('COMPLEX'))
111-
112-
// // Note: case sensitive
113-
// astFromValue('hello', myEnum) == null
114-
//
115-
// // Note: Not a valid enum value
116-
// astFromValue('VALUE', myEnum) == null
117111
}
118112

119113
def 'converts array values to List ASTs'() {
@@ -135,19 +129,43 @@ class AstValueHelperTest extends Specification {
135129
)
136130
}
137131

132+
class SomePojo {
133+
def foo = 3
134+
def bar = "HELLO"
135+
}
136+
class SomePojoWithFields {
137+
public float foo = 3
138+
public String bar = "HELLO"
139+
}
140+
138141
def 'converts input objects'() {
139-
expect:
142+
given:
140143
def inputObj = GraphQLInputObjectType.newInputObject()
141144
.name('MyInputObj')
142145
.field({ f -> f.name("foo").type(GraphQLFloat) })
143146
.field({ f -> f.name("bar").type(myEnum) })
144147
.build()
148+
expect:
145149

146150
astFromValue([foo: 3, bar: 'HELLO'], inputObj).isEqualTo(
147151
new ObjectValue([new ObjectField("foo", new IntValue(bigInt(3))),
148152
new ObjectField("bar", new EnumValue('HELLO')),
149153
])
150154
)
155+
156+
astFromValue(new SomePojo(), inputObj).isEqualTo(
157+
new ObjectValue([new ObjectField("foo", new IntValue(bigInt(3))),
158+
new ObjectField("bar", new EnumValue('HELLO')),
159+
])
160+
)
161+
162+
astFromValue(new SomePojoWithFields(), inputObj).isEqualTo(
163+
new ObjectValue([new ObjectField("foo", new IntValue(bigInt(3))),
164+
new ObjectField("bar", new EnumValue('HELLO')),
165+
])
166+
)
167+
168+
151169
}
152170

153171
def 'converts input objects with explicit nulls'() {

0 commit comments

Comments
 (0)