graphile-contrib / postgraphile-plugin-connection-filter

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

feat: add unaccent filter operators #170 #173

Closed madtibo closed 2 years ago

madtibo commented 2 years ago

This PR adds 4 new operators:

@mattbretl do you think it is possible to add these operators?

mattbretl commented 2 years ago

Thanks for the PR @madtibo !

In the interest of minimizing the scope of this plugin, would you be willing to use a small standalone plugin like this instead?

import type { Plugin } from "graphile-build";
import { Build } from "postgraphile-core";
import { AddConnectionFilterOperator } from "postgraphile-plugin-connection-filter/dist/PgConnectionArgFilterPlugin";

const UnaccentPlugin: Plugin = (builder) => {
  builder.hook("build", (_, build) => {
    const {
      pgSql: sql,
      graphql: { GraphQLString },
      addConnectionFilterOperator,
      escapeLikeWildcards,
    } = build as Build & {
      addConnectionFilterOperator: AddConnectionFilterOperator;
      escapeLikeWildcards: any;
    };

    addConnectionFilterOperator(
      "String",
      "includesUnaccentInsensitive",
      "Contains the specified string (unaccented case-insensitive).",
      () => GraphQLString,
      (i, v) => sql.query`UNACCENT(${i}) ILIKE UNACCENT(${v})`,
      {
        resolveInput: (input) => `%${escapeLikeWildcards(input)}%`,
        resolveSqlIdentifier: (i) => i,
      }
    );

    // continue adding other operators here

    return _;
  });
};

export default UnaccentPlugin;

I really need to properly document addConnectionFilterOperator in the README. :relaxed: The API isn't ideal, but it's been out there long enough now that I might as well promote it and encourage others to extend this plugin as needed.

madtibo commented 2 years ago

In the interest of minimizing the scope of this plugin, would you be willing to use a small standalone plugin like this instead?

It is not nice to force the use of the UNACCENT extension. So this is a very good idea. I will look into it.

madtibo commented 2 years ago

In the interest of minimizing the scope of this plugin, would you be willing to use a small standalone plugin like this instead?

@mattbretl I created a new project for this plugin: postgraphile-plugin-unaccented-search-filter.

I am not fluent in Javascript and I am not sure what should be done to make it a valid package. Could you give me a hand?

madtibo commented 2 years ago

Plugin available !!