18
18
import graphql .schema .CoercingParseValueException ;
19
19
import graphql .schema .CoercingSerializeException ;
20
20
import graphql .schema .GraphQLScalarType ;
21
+ import graphql .util .FpKit ;
21
22
23
+ import java .math .BigDecimal ;
24
+ import java .math .BigInteger ;
25
+ import java .util .ArrayList ;
22
26
import java .util .Collections ;
23
27
import java .util .LinkedHashMap ;
24
28
import java .util .List ;
25
29
import java .util .Map ;
26
30
import java .util .stream .Collectors ;
27
31
32
+ import static graphql .language .ObjectField .newObjectField ;
28
33
import static graphql .scalars .util .Kit .typeName ;
29
34
30
35
/**
@@ -46,14 +51,16 @@ public Object parseValue(Object input) throws CoercingParseValueException {
46
51
47
52
@ Override
48
53
public Object parseLiteral (Object input ) throws CoercingParseLiteralException {
54
+ // on purpose - object scalars can be null
55
+ //noinspection ConstantConditions
49
56
return parseLiteral (input , Collections .emptyMap ());
50
57
}
51
58
52
59
@ Override
53
60
public Object parseLiteral (Object input , Map <String , Object > variables ) throws CoercingParseLiteralException {
54
61
if (!(input instanceof Value )) {
55
62
throw new CoercingParseLiteralException (
56
- "Expected AST type 'StringValue ' but was '" + typeName (input ) + "'."
63
+ "Expected AST type 'Value ' but was '" + typeName (input ) + "'."
57
64
);
58
65
}
59
66
if (input instanceof NullValue ) {
@@ -95,6 +102,64 @@ public Object parseLiteral(Object input, Map<String, Object> variables) throws C
95
102
}
96
103
return Assert .assertShouldNeverHappen ("We have covered all Value types" );
97
104
}
105
+
106
+ @ Override
107
+ public Value <?> valueToLiteral (Object input ) {
108
+ if (input == null ) {
109
+ return NullValue .newNullValue ().build ();
110
+ }
111
+ if (input instanceof String ) {
112
+ return new StringValue ((String ) input );
113
+ }
114
+ if (input instanceof Float ) {
115
+ return new FloatValue (BigDecimal .valueOf ((Float ) input ));
116
+ }
117
+ if (input instanceof Double ) {
118
+ return new FloatValue (BigDecimal .valueOf ((Double ) input ));
119
+ }
120
+ if (input instanceof BigDecimal ) {
121
+ return new FloatValue ((BigDecimal ) input );
122
+ }
123
+ if (input instanceof BigInteger ) {
124
+ return new IntValue ((BigInteger ) input );
125
+ }
126
+ if (input instanceof Number ) {
127
+ long l = ((Number ) input ).longValue ();
128
+ return new IntValue (BigInteger .valueOf (l ));
129
+ }
130
+ if (input instanceof Boolean ) {
131
+ return new BooleanValue ((Boolean ) input );
132
+ }
133
+ if (FpKit .isIterable (input )) {
134
+ return handleIterable (FpKit .toIterable (input ));
135
+ }
136
+ if (input instanceof Map ) {
137
+ return handleMap ((Map <?, ?>) input );
138
+ }
139
+ throw new UnsupportedOperationException ("The ObjectScalar cant handle values of type : " + input .getClass ());
140
+ }
141
+
142
+ private Value <?> handleMap (Map <?, ?> map ) {
143
+ ObjectValue .Builder builder = ObjectValue .newObjectValue ();
144
+ for (Map .Entry <?, ?> entry : map .entrySet ()) {
145
+ String name = String .valueOf (entry .getKey ());
146
+ Value <?> value = valueToLiteral (entry .getValue ());
147
+
148
+ builder .objectField (
149
+ newObjectField ().name (name ).value (value ).build ()
150
+ );
151
+ }
152
+ return builder .build ();
153
+ }
154
+
155
+ @ SuppressWarnings ("rawtypes" )
156
+ private Value <?> handleIterable (Iterable <?> input ) {
157
+ List <Value > values = new ArrayList <>();
158
+ for (Object val : input ) {
159
+ values .add (valueToLiteral (val ));
160
+ }
161
+ return ArrayValue .newArrayValue ().values (values ).build ();
162
+ }
98
163
};
99
164
100
165
public static GraphQLScalarType INSTANCE = GraphQLScalarType .newScalar ()
0 commit comments