30
30
import java .util .Map ;
31
31
import java .util .NoSuchElementException ;
32
32
import java .util .Optional ;
33
+ import java .util .function .Function ;
33
34
import java .util .stream .Collectors ;
34
35
import java .util .stream .IntStream ;
35
36
import java .util .stream .Stream ;
57
58
58
59
import com .introproventures .graphql .jpa .query .annotation .GraphQLDefaultOrderBy ;
59
60
import com .introproventures .graphql .jpa .query .schema .impl .PredicateFilter .Criteria ;
61
+
60
62
import graphql .GraphQLException ;
61
63
import graphql .execution .ValuesResolver ;
62
64
import graphql .language .Argument ;
63
65
import graphql .language .ArrayValue ;
66
+ import graphql .language .AstValueHelper ;
64
67
import graphql .language .BooleanValue ;
65
68
import graphql .language .Comment ;
66
69
import graphql .language .EnumValue ;
79
82
import graphql .schema .DataFetcher ;
80
83
import graphql .schema .DataFetchingEnvironment ;
81
84
import graphql .schema .DataFetchingEnvironmentBuilder ;
85
+ import graphql .schema .GraphQLArgument ;
82
86
import graphql .schema .GraphQLFieldDefinition ;
83
87
import graphql .schema .GraphQLList ;
84
88
import graphql .schema .GraphQLObjectType ;
@@ -378,12 +382,29 @@ protected Predicate getPredicate(CriteriaBuilder cb, Root<?> from, From<?,?> pat
378
382
379
383
380
384
@ SuppressWarnings ( "unchecked" )
381
- private <R extends Value > R getValue (Argument argument ) {
382
- return (R ) argument .getValue ();
385
+ private <R extends Value <?>> R getValue (Argument argument , DataFetchingEnvironment environment ) {
386
+ Value <?> value = argument .getValue ();
387
+
388
+ if (VariableReference .class .isInstance (value )) {
389
+ String variableName = VariableReference .class .cast (value )
390
+ .getName ();
391
+
392
+ Object variableValue = environment .getExecutionContext ()
393
+ .getVariables ()
394
+ .get (variableName );
395
+
396
+ GraphQLArgument graphQLArgument = environment .getExecutionStepInfo ()
397
+ .getFieldDefinition ()
398
+ .getArgument (argument .getName ());
399
+
400
+ return (R ) AstValueHelper .astFromValue (variableValue , graphQLArgument .getType ());
401
+ }
402
+
403
+ return (R ) value ;
383
404
}
384
405
385
406
protected Predicate getWherePredicate (CriteriaBuilder cb , Root <?> root , From <?,?> path , DataFetchingEnvironment environment , Argument argument ) {
386
- ObjectValue whereValue = getValue (argument );
407
+ ObjectValue whereValue = getValue (argument , environment );
387
408
388
409
if (whereValue .getChildren ().isEmpty ())
389
410
return cb .conjunction ();
@@ -404,7 +425,7 @@ protected Predicate getWherePredicate(CriteriaBuilder cb, Root<?> root, From<?,
404
425
@ SuppressWarnings ({"unchecked" , "rawtypes" })
405
426
protected Predicate getArgumentPredicate (CriteriaBuilder cb , From <?,?> from ,
406
427
DataFetchingEnvironment environment , Argument argument ) {
407
- ObjectValue whereValue = getValue (argument );
428
+ ObjectValue whereValue = getValue (argument , environment );
408
429
409
430
if (whereValue .getChildren ().isEmpty ())
410
431
return cb .disjunction ();
@@ -494,7 +515,7 @@ protected Predicate getArgumentsPredicate(CriteriaBuilder cb,
494
515
From <?, ?> path ,
495
516
DataFetchingEnvironment environment ,
496
517
Argument argument ) {
497
- ArrayValue whereValue = getValue (argument );
518
+ ArrayValue whereValue = getValue (argument , environment );
498
519
499
520
if (whereValue .getValues ().isEmpty ())
500
521
return cb .disjunction ();
@@ -896,22 +917,41 @@ else if (value instanceof VariableReference) {
896
917
return argumentValue ;
897
918
}
898
919
} else if (value instanceof ArrayValue ) {
899
- Object convertedValue = environment .getArgument (argument .getName ());
900
- if (convertedValue != null && !getJavaType (environment , argument ).isEnum ()) {
901
- // unwrap [[EnumValue{name='value'}]]
902
- if (convertedValue instanceof Collection
903
- && ((Collection ) convertedValue ).stream ().allMatch (it ->it instanceof Collection )) {
904
- convertedValue = ((Collection ) convertedValue ).iterator ().next ();
920
+ Collection arrayValue = environment .getArgument (argument .getName ());
921
+
922
+ if (arrayValue != null ) {
923
+ // Let's unwrap array of array values
924
+ if (arrayValue .stream ()
925
+ .allMatch (it ->it instanceof Collection )) {
926
+ arrayValue = Collection .class .cast (arrayValue .iterator ()
927
+ .next ());
905
928
}
906
-
907
- if (convertedValue instanceof Collection
908
- && ((Collection ) convertedValue ).stream ().anyMatch (it ->it instanceof Value )) {
909
- return ((Collection ) convertedValue ).stream ()
910
- .map ((it ) -> convertValue (environment , argument , (Value ) it ))
911
- .collect (Collectors .toList ());
929
+
930
+ // Let's convert enum types, i.e. array of strings or EnumValue into Java type
931
+ if (getJavaType (environment , argument ).isEnum ()) {
932
+ Function <Object , Value > objectValue = (obj ) -> Value .class .isInstance (obj )
933
+ ? Value .class .cast (obj )
934
+ : new EnumValue (obj .toString ());
935
+ // Return real typed resolved array values converted into Java enums
936
+ return arrayValue .stream ()
937
+ .map ((it ) -> convertValue (environment ,
938
+ argument ,
939
+ objectValue .apply (it )))
940
+ .collect (Collectors .toList ());
941
+ }
942
+ // Let's try handle Ast Value types
943
+ else if (arrayValue .stream ()
944
+ .anyMatch (it ->it instanceof Value )) {
945
+ return arrayValue .stream ()
946
+ .map (it -> convertValue (environment ,
947
+ argument ,
948
+ Value .class .cast (it )))
949
+ .collect (Collectors .toList ());
950
+ }
951
+ // Return real typed resolved array value, i.e. Date, UUID, Long
952
+ else {
953
+ return arrayValue ;
912
954
}
913
- // Return real typed resolved array value
914
- return convertedValue ;
915
955
} else {
916
956
// Wrap converted values in ArrayList
917
957
return ((ArrayValue ) value ).getValues ().stream ()
0 commit comments