2

Given the general structure:

class Article(Page):
  body = RichTextField(...)
  search_fields = Page.search_fields + [index.SearchField('body')]

class ArticleFilter(FilterSet):
  search = SearchFilter()

  class Meta:
    model = Article
    fields = ['slug']

class Query(ObjectType):
  articles = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleFilter)

I thought to create a "SearchFilter" to expose the wagtail search functionality, since I ultimately want to perform full text search via graphql like so:

query {
  articles (search: "some text in a page") {
     edges {
        nodes {
           slug
        }
     }
  }
}

"search" is not a field on the Django model which is why I created a custom field in the Django FilterSet. My thought was to do something like:

class SearchFilter(CharFilter):
  def filter(self, qs, value):
     search_results = [r.pk for r in qs.search(value)]
     return self.get_method(qs)(pk__in=search_results)

however, I'm curious if there's a better pattern that's more efficient. At the bare minimum I'd want to ensure the SearchFilter is added last (so the query searched is filtered first).

Should the "search" be moved outside of the FilterSet and into the Query/Node/custom connection, and if so, how can I add an additional field to "articles" to see it as the final step in resolving articles (i.e. tack it on to the end of the filter queryset)? If this does belong in a separate Connection, is it possible to combine that connection with the django filter connection?

I would think this pattern of accessing Wagtail search via graphene already exists, however I've had no luck on finding this in the documentation.

noahnu
  • 3,479
  • 2
  • 18
  • 40

0 Answers0