@@ -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.
@@ -32,6 +47,8 @@ class QuerySetList(graphene.List):
3247 * ``limit``
3348 * ``offset``
3449 * ``search_query``
50+ * ``search_operator``
51+ * ``search_fields``
3552 * ``order``
3653
3754 :param enable_in_menu: Enable in_menu filter.
@@ -42,6 +59,10 @@ class QuerySetList(graphene.List):
4259 :type enable_offset: bool
4360 :param enable_search: Enable search query argument.
4461 :type enable_search: bool
62+ :param enable_search_fields: Enable search fields argument, enable_search must also be True
63+ :type enable_search_fields: bool
64+ :param enable_search_operator: Enable search operator argument, enable_search must also be True
65+ :type enable_search_operator: bool
4566 :param enable_order: Enable ordering via query argument.
4667 :type enable_order: bool
4768 """
@@ -50,8 +71,10 @@ def __init__(self, of_type, *args, **kwargs):
5071 enable_in_menu = kwargs .pop ("enable_in_menu" , False )
5172 enable_limit = kwargs .pop ("enable_limit" , True )
5273 enable_offset = kwargs .pop ("enable_offset" , True )
53- enable_search = kwargs .pop ("enable_search" , True )
5474 enable_order = kwargs .pop ("enable_order" , True )
75+ enable_search = kwargs .pop ("enable_search" , True )
76+ enable_search_fields = kwargs .pop ("enable_search_fields" , True )
77+ enable_search_operator = kwargs .pop ("enable_search_operator" , True )
5578
5679 # Check if the type is a Django model type. Do not perform the
5780 # check if value is lazy.
@@ -106,6 +129,22 @@ def __init__(self, of_type, *args, **kwargs):
106129 graphene .String ,
107130 description = _ ("Filter the results using Wagtail's search." ),
108131 )
132+ if enable_search_operator :
133+ kwargs ["search_operator" ] = graphene .Argument (
134+ SearchOperatorEnum ,
135+ description = _ (
136+ "Specify search operator (and/or), see: https://docs.wagtail.org/en/stable/topics/search/searching.html#search-operator"
137+ ),
138+ default_value = "and" ,
139+ )
140+
141+ if enable_search_fields :
142+ kwargs ["search_fields" ] = graphene .Argument (
143+ graphene .List (graphene .String ),
144+ description = _ (
145+ "A list of fields to search in. see: https://docs.wagtail.org/en/stable/topics/search/searching.html#specifying-the-fields-to-search"
146+ ),
147+ )
109148
110149 if "id" not in kwargs :
111150 kwargs ["id" ] = graphene .Argument (graphene .ID , description = _ ("Filter by ID" ))
@@ -152,23 +191,31 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
152191 """
153192 Paginated QuerySet type with arguments used by Django's query sets.
154193
155- This type setts the following arguments on itself:
194+ This type sets the following arguments on itself:
156195
157196 * ``id``
158197 * ``in_menu``
159198 * ``page``
160199 * ``per_page``
161200 * ``search_query``
201+ * ``search_operator``
202+ * ``search_fields``
162203 * ``order``
163204
164205 :param enable_search: Enable search query argument.
165206 :type enable_search: bool
207+ :param enable_search_fields: Enable search fields argument, enable_search must also be True
208+ :type enable_search_fields: bool
209+ :param enable_search_operator: Enable search operator argument, enable_search must also be True
210+ :type enable_search_operator: bool
166211 :param enable_order: Enable ordering via query argument.
167212 :type enable_order: bool
168213 """
169214
170215 enable_in_menu = kwargs .pop ("enable_in_menu" , False )
171216 enable_search = kwargs .pop ("enable_search" , True )
217+ enable_search_fields = kwargs .pop ("enable_search_fields" , True )
218+ enable_search_operator = kwargs .pop ("enable_search_operator" , True )
172219 enable_order = kwargs .pop ("enable_order" , True )
173220 required = kwargs .get ("required" , False )
174221 type_name = type_class if isinstance (type_class , str ) else type_class .__name__
@@ -225,6 +272,22 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
225272 kwargs ["search_query" ] = graphene .Argument (
226273 graphene .String , description = _ ("Filter the results using Wagtail's search." )
227274 )
275+ if enable_search_operator :
276+ kwargs ["search_operator" ] = graphene .Argument (
277+ SearchOperatorEnum ,
278+ description = _ (
279+ "Specify search operator (and/or), see: https://docs.wagtail.org/en/stable/topics/search/searching.html#search-operator"
280+ ),
281+ default_value = "and" ,
282+ )
283+
284+ if enable_search_fields :
285+ kwargs ["search_fields" ] = graphene .Argument (
286+ graphene .List (graphene .String ),
287+ description = _ (
288+ "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"
289+ ),
290+ )
228291
229292 if "id" not in kwargs :
230293 kwargs ["id" ] = graphene .Argument (graphene .ID , description = _ ("Filter by ID" ))
0 commit comments