graphile-contrib / postgraphile-plugin-connection-filter

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

Custom filter name via connectionFilterName #200

Open pilsy opened 1 year ago

pilsy commented 1 year ago

Where i work we wanted the ability to name the filter "where", so while we migrate from Hasura our queries will not change -- this PR adds this feature via connectionFilterName option

benjie commented 8 months ago

This need is better served by a small inflection plugin:

import { makeAddInflectorsPlugin } from "graphile-utils";

export const FilterToWherePlugin = makeAddInflectorsPlugin(() => ({
  filterType(typeName: string) {
    return `${typeName}Where`;
  },
  filterFieldType(typeName: string) {
    return `${typeName}Where`;
  },
  filterFieldListType(typeName: string) {
    return `${typeName}ListWhere`;
  },
  filterArgumentName() {
    return "where";
  }
});

The only change needed for this would be adding the new filterArgumentName inflector and then doing:

const filterArgumentName = inflection.filterArgumentName();

and using that variable in the place of filter here:

https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/blob/fcd5e920c50604063c5db9bc28c557bd69bcfdcc/src/PgConnectionArgFilterPlugin.ts#L127

and here:

https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/blob/fcd5e920c50604063c5db9bc28c557bd69bcfdcc/src/PgConnectionArgFilterPlugin.ts#L107-L109

Many of the changes made in this PR are undesirable; for example changing the smart tag from @omit filter to @omit <someVariable> is likely to cause a lot of pain for other plugins. If you want to simplify this PR down to only the few lines mentioned above then I'd definitely consider it.