@@ -21,6 +21,21 @@ def parse_literal(ast, _variables=None):
2121 return return_value
2222
2323
24+ class SearchOperatorEnum (graphene .Enum ):
25+ """
26+ Enum for search operator.
27+ """
28+
29+ AND = "and"
30+ OR = "or"
31+
32+ def __str__ (self ):
33+ # the core search parser expects the operator to be a string.
34+ # the default __str__ returns SearchOperatorEnum.AND/OR,
35+ # this __str__ returns the value and/or for compatibility.
36+ return self .value
37+
38+
2439class QuerySetList (graphene .List ):
2540 """
2641 List type with arguments used by Django's query sets.
@@ -31,6 +46,8 @@ class QuerySetList(graphene.List):
3146 * ``limit``
3247 * ``offset``
3348 * ``search_query``
49+ * ``search_operator``
50+ * ``search_fields``
3451 * ``order``
3552
3653 :param enable_limit: Enable limit argument.
@@ -39,15 +56,21 @@ class QuerySetList(graphene.List):
3956 :type enable_offset: bool
4057 :param enable_search: Enable search query argument.
4158 :type enable_search: bool
59+ :param enable_search_fields: Enable search fields argument, enable_search must also be True
60+ :type enable_search_fields: bool
61+ :param enable_search_operator: Enable search operator argument, enable_search must also be True
62+ :type enable_search_operator: bool
4263 :param enable_order: Enable ordering via query argument.
4364 :type enable_order: bool
4465 """
4566
4667 def __init__ (self , of_type , * args , ** kwargs ):
4768 enable_limit = kwargs .pop ("enable_limit" , True )
4869 enable_offset = kwargs .pop ("enable_offset" , True )
49- enable_search = kwargs .pop ("enable_search" , True )
5070 enable_order = kwargs .pop ("enable_order" , True )
71+ enable_search = kwargs .pop ("enable_search" , True )
72+ enable_search_fields = kwargs .pop ("enable_search_fields" , True )
73+ enable_search_operator = kwargs .pop ("enable_search_operator" , True )
5174
5275 # Check if the type is a Django model type. Do not perform the
5376 # check if value is lazy.
@@ -92,6 +115,22 @@ def __init__(self, of_type, *args, **kwargs):
92115 graphene .String ,
93116 description = _ ("Filter the results using Wagtail's search." ),
94117 )
118+ if enable_search_operator :
119+ kwargs ["search_operator" ] = graphene .Argument (
120+ SearchOperatorEnum ,
121+ description = _ (
122+ "Specify search operator (and/or), see: https://docs.wagtail.org/en/stable/topics/search/searching.html#search-operator"
123+ ),
124+ default_value = "and" ,
125+ )
126+
127+ if enable_search_fields :
128+ kwargs ["search_fields" ] = graphene .Argument (
129+ graphene .List (graphene .String ),
130+ description = _ (
131+ "A list of fields to search in. see: https://docs.wagtail.org/en/stable/topics/search/searching.html#specifying-the-fields-to-search"
132+ ),
133+ )
95134
96135 if "id" not in kwargs :
97136 kwargs ["id" ] = graphene .Argument (graphene .ID , description = _ ("Filter by ID" ))
@@ -138,21 +177,29 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
138177 """
139178 Paginated QuerySet type with arguments used by Django's query sets.
140179
141- This type setts the following arguments on itself:
180+ This type sets the following arguments on itself:
142181
143182 * ``id``
144183 * ``page``
145184 * ``per_page``
146185 * ``search_query``
186+ * ``search_operator``
187+ * ``search_fields``
147188 * ``order``
148189
149190 :param enable_search: Enable search query argument.
150191 :type enable_search: bool
192+ :param enable_search_fields: Enable search fields argument, enable_search must also be True
193+ :type enable_search_fields: bool
194+ :param enable_search_operator: Enable search operator argument, enable_search must also be True
195+ :type enable_search_operator: bool
151196 :param enable_order: Enable ordering via query argument.
152197 :type enable_order: bool
153198 """
154199
155200 enable_search = kwargs .pop ("enable_search" , True )
201+ enable_search_fields = kwargs .pop ("enable_search_fields" , True )
202+ enable_search_operator = kwargs .pop ("enable_search_operator" , True )
156203 enable_order = kwargs .pop ("enable_order" , True )
157204 required = kwargs .get ("required" , False )
158205 type_name = type_class if isinstance (type_class , str ) else type_class .__name__
@@ -199,6 +246,22 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
199246 kwargs ["search_query" ] = graphene .Argument (
200247 graphene .String , description = _ ("Filter the results using Wagtail's search." )
201248 )
249+ if enable_search_operator :
250+ kwargs ["search_operator" ] = graphene .Argument (
251+ SearchOperatorEnum ,
252+ description = _ (
253+ "Specify search operator (and/or), see: https://docs.wagtail.org/en/stable/topics/search/searching.html#search-operator"
254+ ),
255+ default_value = "and" ,
256+ )
257+
258+ if enable_search_fields :
259+ kwargs ["search_fields" ] = graphene .Argument (
260+ graphene .List (graphene .String ),
261+ description = _ (
262+ "A comma-separated list of fields to search in. see: https://docs.wagtail.org/en/stable/topics/search/searching.html#specifying-the-fields-to-search"
263+ ),
264+ )
202265
203266 if "id" not in kwargs :
204267 kwargs ["id" ] = graphene .Argument (graphene .ID , description = _ ("Filter by ID" ))
0 commit comments