devind-team / graphene-django-filter

Advanced filters for Graphene
MIT License
19 stars 7 forks source link

Full text search #4

Closed SquakR closed 2 years ago

SquakR commented 2 years ago

Django provides the API for PostgreSQL full text search. It would be convenient to expose this API to the graphql filter level. The additional lookup with the name fullsearch can be added for implementation. For instance

import graphene
from graphene_django_filter import AdvancedFilterSet
from graphene_django import DjangoObjectType

class TaskFilter(AdvancedFilterSet)
    class Meta:
        model = Task
        fields = {
            'name': ('exact', 'contains', 'fullsearch')
        }

class UserType(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (graphene.relay.Node,)
        fields = '__all__'
        filter_fields = {
           'name': ('exact', 'contains', 'fullsearch')
        }

One of the full search queries could look like this.

{
  tasks(
    filter: {
      name: {
        trigramSimilarity: {
          gt: 0.8
          value: "Important"
        }
      }
    }
  ){
    edges {
      node {
        id
        name
      }
    }
  }
}