@@ -14,6 +14,7 @@ local avro_helpers = require('graphql.avro_helpers')
14
14
local db_schema_helpers = require (' graphql.db_schema_helpers' )
15
15
local error_codes = require (' graphql.error_codes' )
16
16
local statistics = require (' graphql.statistics' )
17
+ local expressions = require (' graphql.expressions' )
17
18
18
19
local check = utils .check
19
20
local e = error_codes
@@ -762,16 +763,16 @@ local function validate_collections(collections, schemas)
762
763
end
763
764
end
764
765
765
- --- Whether an object match set of PCRE.
766
- ---
767
- --- @tparam table obj an object to check
766
+ --- Whether an object match set of PCREs.
768
767
---
769
768
--- @tparam table pcre map with PCRE as values; names are correspond to field
770
769
--- names of the `obj` to match
771
770
---
771
+ --- @tparam table obj an object to check
772
+ ---
772
773
--- @treturn boolean `res` whether the `obj` object match `pcre` set of
773
774
--- regexps.
774
- local function match_using_re (obj , pcre )
775
+ local function match_using_re (pcre , obj )
775
776
if pcre == nil then return true end
776
777
777
778
assert (rex ~= nil , ' we should not pass over :compile() ' ..
@@ -784,7 +785,7 @@ local function match_using_re(obj, pcre)
784
785
return false
785
786
end
786
787
if type (re ) == ' table' then
787
- local match = match_using_re (obj [field_name ], re )
788
+ local match = match_using_re (re , obj [field_name ])
788
789
if not match then return false end
789
790
elseif not utils .regexp (re , obj [field_name ]) then
790
791
return false
@@ -794,6 +795,24 @@ local function match_using_re(obj, pcre)
794
795
return true
795
796
end
796
797
798
+ --- Whether an object match an expression.
799
+ ---
800
+ --- @param expr (table or string ) compiled or raw expression
801
+ ---
802
+ --- @tparam table obj an object to check
803
+ ---
804
+ --- @tparam [opt] table variables variables values from the request
805
+ ---
806
+ --- @treturn boolean `res` whether the `obj` object match `expr` expression
807
+ local function match_using_expr (expr , obj , variables )
808
+ if expr == nil then return true end
809
+
810
+ check (expr , ' expression' , ' table' )
811
+ local res = expr :execute (obj , variables )
812
+ check (res , ' expression result' , ' boolean' )
813
+ return res
814
+ end
815
+
797
816
--- Check whether we meet deadline time.
798
817
---
799
818
--- The functions raises an exception in the case.
834
853
--- * `pivot_filter` (table, set of fields to match the objected pointed by
835
854
--- `offset` arqument of the GraphQL query),
836
855
--- * `resolveField` (function) for subrequests, see @{impl.new}.
856
+ --- * XXX: describe other fields.
837
857
---
838
858
--- @return nil
839
859
---
@@ -864,7 +884,9 @@ local function process_tuple(self, state, tuple, opts)
864
884
865
885
local collection_name = opts .collection_name
866
886
local pcre = opts .pcre
887
+ local expr = opts .expr
867
888
local resolveField = opts .resolveField
889
+ local variables = qcontext .variables
868
890
869
891
-- convert tuple -> object
870
892
local obj = opts .unflatten_tuple (self , collection_name , tuple ,
@@ -895,7 +917,7 @@ local function process_tuple(self, state, tuple, opts)
895
917
896
918
-- filter out non-matching objects
897
919
local match = utils .is_subtable (obj , truncated_filter ) and
898
- match_using_re (obj , pcre )
920
+ match_using_re (pcre , obj ) and match_using_expr ( expr , obj , variables )
899
921
if do_filter then
900
922
if not match then return true end
901
923
else
@@ -1035,6 +1057,13 @@ local function prepare_select_internal(self, collection_name, from, filter,
1035
1057
qcontext = qcontext
1036
1058
}
1037
1059
1060
+ -- compile an expression argument if provided and did not compiled yet
1061
+ local expr = args .filter
1062
+ check (expr , ' expression' , ' table' , ' string' , ' nil' )
1063
+ if type (expr ) == ' string' then
1064
+ expr = expressions .new (expr )
1065
+ end
1066
+
1038
1067
-- read only process_tuple options
1039
1068
local select_opts = {
1040
1069
limit = args .limit ,
@@ -1046,6 +1075,7 @@ local function prepare_select_internal(self, collection_name, from, filter,
1046
1075
use_tomap = self .collection_use_tomap [collection_name ] or false ,
1047
1076
default_unflatten_tuple = default_unflatten_tuple ,
1048
1077
pcre = args .pcre ,
1078
+ expr = expr ,
1049
1079
resolveField = extra .resolveField ,
1050
1080
is_hidden = extra .is_hidden ,
1051
1081
}
0 commit comments