3030import java .util .Map ;
3131import java .util .NoSuchElementException ;
3232import java .util .Optional ;
33+ import java .util .function .Function ;
3334import java .util .stream .Collectors ;
3435import java .util .stream .IntStream ;
3536import java .util .stream .Stream ;
5758
5859import com .introproventures .graphql .jpa .query .annotation .GraphQLDefaultOrderBy ;
5960import com .introproventures .graphql .jpa .query .schema .impl .PredicateFilter .Criteria ;
61+
6062import graphql .GraphQLException ;
6163import graphql .execution .ValuesResolver ;
6264import graphql .language .Argument ;
6365import graphql .language .ArrayValue ;
66+ import graphql .language .AstValueHelper ;
6467import graphql .language .BooleanValue ;
6568import graphql .language .Comment ;
6669import graphql .language .EnumValue ;
7982import graphql .schema .DataFetcher ;
8083import graphql .schema .DataFetchingEnvironment ;
8184import graphql .schema .DataFetchingEnvironmentBuilder ;
85+ import graphql .schema .GraphQLArgument ;
8286import graphql .schema .GraphQLFieldDefinition ;
8387import graphql .schema .GraphQLList ;
8488import graphql .schema .GraphQLObjectType ;
@@ -378,12 +382,29 @@ protected Predicate getPredicate(CriteriaBuilder cb, Root<?> from, From<?,?> pat
378382
379383
380384 @ 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 (variableName );
399+
400+ return (R ) AstValueHelper .astFromValue (variableValue , graphQLArgument .getType ());
401+ }
402+
403+ return (R ) value ;
383404 }
384405
385406 protected Predicate getWherePredicate (CriteriaBuilder cb , Root <?> root , From <?,?> path , DataFetchingEnvironment environment , Argument argument ) {
386- ObjectValue whereValue = getValue (argument );
407+ ObjectValue whereValue = getValue (argument , environment );
387408
388409 if (whereValue .getChildren ().isEmpty ())
389410 return cb .conjunction ();
@@ -404,7 +425,7 @@ protected Predicate getWherePredicate(CriteriaBuilder cb, Root<?> root, From<?,
404425 @ SuppressWarnings ({"unchecked" , "rawtypes" })
405426 protected Predicate getArgumentPredicate (CriteriaBuilder cb , From <?,?> from ,
406427 DataFetchingEnvironment environment , Argument argument ) {
407- ObjectValue whereValue = getValue (argument );
428+ ObjectValue whereValue = getValue (argument , environment );
408429
409430 if (whereValue .getChildren ().isEmpty ())
410431 return cb .disjunction ();
@@ -494,7 +515,7 @@ protected Predicate getArgumentsPredicate(CriteriaBuilder cb,
494515 From <?, ?> path ,
495516 DataFetchingEnvironment environment ,
496517 Argument argument ) {
497- ArrayValue whereValue = getValue (argument );
518+ ArrayValue whereValue = getValue (argument , environment );
498519
499520 if (whereValue .getValues ().isEmpty ())
500521 return cb .disjunction ();
@@ -897,7 +918,29 @@ else if (value instanceof VariableReference) {
897918 }
898919 } else if (value instanceof ArrayValue ) {
899920 Object convertedValue = environment .getArgument (argument .getName ());
900- if (convertedValue != null && !getJavaType (environment , argument ).isEnum ()) {
921+
922+ if (convertedValue != null && getJavaType (environment , argument ).isEnum ()) {
923+
924+ Function <Object , Value > f = (obj ) -> Value .class .isInstance (obj )
925+ ? Value .class .cast (obj )
926+ : new EnumValue (obj .toString ());
927+
928+ // unwrap [[EnumValue{name='value'}]]
929+ if (convertedValue instanceof Collection
930+ && ((Collection ) convertedValue ).stream ().allMatch (it ->it instanceof Collection )) {
931+ convertedValue = ((Collection ) convertedValue ).iterator ().next ();
932+ }
933+
934+ if (convertedValue instanceof Collection ) {
935+ return ((Collection ) convertedValue ).stream ()
936+ .map ((it ) -> convertValue (environment , argument , f .apply (it )))
937+ .collect (Collectors .toList ());
938+ }
939+ // Return real typed resolved array value
940+ return convertValue (environment , argument , f .apply (convertedValue ));
941+ }
942+ else
943+ if (convertedValue != null && !getJavaType (environment , argument ).isEnum ()) {
901944 // unwrap [[EnumValue{name='value'}]]
902945 if (convertedValue instanceof Collection
903946 && ((Collection ) convertedValue ).stream ().allMatch (it ->it instanceof Collection )) {
0 commit comments