graphile-contrib / postgraphile-plugin-connection-filter

Filtering on PostGraphile connections
MIT License
283 stars 32 forks source link

Trigram support? #175

Open theogravity opened 2 years ago

theogravity commented 2 years ago

Would it be difficult to support for the operators here?

https://www.postgresql.org/docs/14/pgtrgm.html

Would it be as simple as adding in something like:

      similarityGreaterThan: {
        description: "Value has a similarity that is greater than the current similarity threshold set by pg_trgm.similarity_threshold.",
        resolve: (i, v) => sql.query`${i} % ${v}`,
      },
theogravity commented 2 years ago

If anyone wants to know how to do this, you can actually create your own plugin easily using makeAddPgTableConditionPlugin from 'graphile-utils':

https://www.graphile.org/postgraphile/make-add-pg-table-condition-plugin/

import { makeAddPgTableConditionPlugin } from 'graphile-utils'

const cardNameSearch = makeAddPgTableConditionPlugin(
  'public',
  'cards',
  'cardNameSearch',
  (build) => {
    const { GraphQLList, GraphQLString } = build.graphql
    return {
      description:
        'Value has a similarity that is greater than the current similarity threshold set by pg_trgm.similarity_threshold.',
      type: new GraphQLList(GraphQLString),
    }
  },
  (value, helpers, build) => {
    const { sql, sqlTableAlias } = helpers
    return sql.fragment`${sqlTableAlias}.name %> (${sql.value(value)}::text)`
  },
)

export default cardNameSearch
mattbretl commented 1 year ago

(Sorry for the delayed response!)

Glad you were able to find a solution! :relaxed:

In case anyone else comes across this, you can probably achieve something similar on filter with the addConnectionFilterOperator method exported from this plugin. See https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/pull/173#issuecomment-1000945085 and https://github.com/graphile-contrib/postgraphile-plugin-connection-filter-postgis for examples.