14
14
*/
15
15
package graphql .annotations .processor .retrievers .fieldBuilders ;
16
16
17
+ import graphql .Scalars ;
17
18
import graphql .annotations .annotationTypes .directives .activation .GraphQLDirectives ;
18
19
import graphql .annotations .processor .ProcessingElementsContainer ;
19
20
import graphql .annotations .processor .exceptions .GraphQLAnnotationsException ;
26
27
import java .lang .annotation .Annotation ;
27
28
import java .lang .reflect .AnnotatedElement ;
28
29
import java .lang .reflect .Method ;
30
+ import java .math .BigDecimal ;
29
31
import java .util .ArrayList ;
30
32
import java .util .Arrays ;
31
33
import java .util .List ;
32
34
import java .util .stream .Collectors ;
33
35
36
+ import static graphql .Assert .assertShouldNeverHappen ;
37
+ import static graphql .scalar .CoercingUtil .isNumberIsh ;
34
38
import static graphql .schema .GraphQLDirective .newDirective ;
35
39
36
40
@@ -134,7 +138,22 @@ private void transformArgument(Annotation annotation, GraphQLDirective.Builder d
134
138
Object argumentValue = methods [finalI ].invoke (annotation );
135
139
Object value ;
136
140
if (graphQLArgument .getType () instanceof GraphQLScalarType ) {
137
- value = ((GraphQLScalarType ) graphQLArgument .getType ()).getCoercing ().parseValue (argumentValue );
141
+ // value = ((GraphQLScalarType) graphQLArgument.getType()).getCoercing().parseValue(argumentValue);
142
+
143
+ try {
144
+ GraphQLScalarType argumentType = (GraphQLScalarType ) graphQLArgument .getType ();
145
+ if ( argumentType .equals ( Scalars .GraphQLBoolean ) )
146
+ {
147
+ value = castToBoolean ( argumentValue );
148
+ }
149
+ else
150
+ {
151
+ value = argumentType .getCoercing ().parseValue ( argumentValue );
152
+ }
153
+ builder .value ( value );
154
+ } catch (Exception e ) {
155
+ throw new GraphQLAnnotationsException (COULD_NOT_PARSE_ARGUMENT_VALUE_TO_ARGUMENT_TYPE , e );
156
+ }
138
157
}
139
158
else {
140
159
value = argumentValue ;
@@ -159,8 +178,17 @@ private void transformArgument(String[] argumentValues, GraphQLDirective.Builder
159
178
if (graphQLArgument .getType () instanceof GraphQLScalarType ) {
160
179
161
180
try {
162
- Object value = ((GraphQLScalarType ) graphQLArgument .getType ()).getCoercing ().parseValue (argumentValue );
163
- builder .value (value );
181
+ Object value ;
182
+ GraphQLScalarType argumentType = (GraphQLScalarType ) graphQLArgument .getType ();
183
+ if ( argumentType .equals ( Scalars .GraphQLBoolean ) )
184
+ {
185
+ value = castToBoolean ( argumentValue );
186
+ }
187
+ else
188
+ {
189
+ value = argumentType .getCoercing ().parseValue ( argumentValue );
190
+ }
191
+ builder .value ( value );
164
192
} catch (Exception e ) {
165
193
throw new GraphQLAnnotationsException (COULD_NOT_PARSE_ARGUMENT_VALUE_TO_ARGUMENT_TYPE , e );
166
194
}
@@ -169,4 +197,43 @@ private void transformArgument(String[] argumentValues, GraphQLDirective.Builder
169
197
}
170
198
}));
171
199
}
200
+
201
+ private Boolean castToBoolean ( Object input )
202
+ {
203
+ if ( input instanceof Boolean )
204
+ {
205
+ return (Boolean ) input ;
206
+ }
207
+ else if ( input instanceof String )
208
+ {
209
+ String lStr = ( (String ) input ).toLowerCase ();
210
+ if ( lStr .equals ( "true" ) )
211
+ {
212
+ return true ;
213
+ }
214
+ if ( lStr .equals ( "false" ) )
215
+ {
216
+ return false ;
217
+ }
218
+ return null ;
219
+ }
220
+ else if ( isNumberIsh ( input ) )
221
+ {
222
+ BigDecimal value ;
223
+ try
224
+ {
225
+ value = new BigDecimal ( input .toString () );
226
+ }
227
+ catch ( NumberFormatException e )
228
+ {
229
+ // this should never happen because String is handled above
230
+ return assertShouldNeverHappen ();
231
+ }
232
+ return value .compareTo ( BigDecimal .ZERO ) != 0 ;
233
+ }
234
+ else
235
+ {
236
+ return null ;
237
+ }
238
+ }
172
239
}
0 commit comments