@@ -15,6 +15,7 @@ local avro_helpers = require('graphql.avro_helpers')
15
15
local db_schema_helpers = require (' graphql.db_schema_helpers' )
16
16
local error_codes = require (' graphql.error_codes' )
17
17
local statistics = require (' graphql.statistics' )
18
+ local expressions = require (' graphql.expressions' )
18
19
19
20
local check = utils .check
20
21
local e = error_codes
@@ -763,7 +764,7 @@ local function validate_collections(collections, schemas)
763
764
end
764
765
end
765
766
766
- --- Whether an object match set of PCRE .
767
+ --- Whether an object match set of PCREs .
767
768
---
768
769
--- @tparam table obj an object to check
769
770
---
@@ -817,6 +818,24 @@ local function match_using_re(obj, pcre)
817
818
return true
818
819
end
819
820
821
+ --- Whether an object match an expression.
822
+ ---
823
+ --- @tparam table obj an object to check
824
+ ---
825
+ --- @param expr (table or string ) compiled or raw expression
826
+ ---
827
+ --- @tparam [opt] table variables variables values from the request
828
+ ---
829
+ --- @treturn boolean `res` whether the `obj` object match `expr` expression
830
+ local function match_using_expr (obj , expr , variables )
831
+ if expr == nil then return true end
832
+
833
+ check (expr , ' expression' , ' table' )
834
+ local res = expr :execute (obj , variables )
835
+ check (res , ' expression result' , ' boolean' )
836
+ return res
837
+ end
838
+
820
839
--- Check whether we meet deadline time.
821
840
---
822
841
--- The functions raises an exception in the case.
857
876
--- * `pivot_filter` (table, set of fields to match the objected pointed by
858
877
--- `offset` arqument of the GraphQL query),
859
878
--- * `resolveField` (function) for subrequests, see @{impl.new}.
879
+ --- * XXX: describe other fields.
860
880
---
861
881
--- @return nil
862
882
---
@@ -887,7 +907,9 @@ local function process_tuple(self, state, tuple, opts)
887
907
888
908
local collection_name = opts .collection_name
889
909
local pcre = opts .pcre
910
+ local expr = opts .expr
890
911
local resolveField = opts .resolveField
912
+ local variables = qcontext .variables
891
913
892
914
-- convert tuple -> object
893
915
local obj = opts .unflatten_tuple (self , collection_name , tuple ,
@@ -918,7 +940,7 @@ local function process_tuple(self, state, tuple, opts)
918
940
919
941
-- filter out non-matching objects
920
942
local match = utils .is_subtable (obj , truncated_filter ) and
921
- match_using_re (obj , pcre )
943
+ match_using_re (obj , pcre ) and match_using_expr ( obj , expr , variables )
922
944
if do_filter then
923
945
if not match then return true end
924
946
else
@@ -1058,6 +1080,13 @@ local function prepare_select_internal(self, collection_name, from, filter,
1058
1080
qcontext = qcontext
1059
1081
}
1060
1082
1083
+ -- compile an expression argument if provided and did not compiled yet
1084
+ local expr = args .filter
1085
+ check (expr , ' expression' , ' table' , ' string' , ' nil' )
1086
+ if type (expr ) == ' string' then
1087
+ expr = expressions .new (expr )
1088
+ end
1089
+
1061
1090
-- read only process_tuple options
1062
1091
local select_opts = {
1063
1092
limit = args .limit ,
@@ -1069,6 +1098,7 @@ local function prepare_select_internal(self, collection_name, from, filter,
1069
1098
use_tomap = self .collection_use_tomap [collection_name ] or false ,
1070
1099
default_unflatten_tuple = default_unflatten_tuple ,
1071
1100
pcre = args .pcre ,
1101
+ expr = expr ,
1072
1102
resolveField = extra .resolveField ,
1073
1103
is_hidden = extra .is_hidden ,
1074
1104
}
0 commit comments