graphile-contrib / postgraphile-plugin-connection-filter

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

feat: add type prefix option #204

Closed Brookke closed 8 months ago

Brookke commented 10 months ago

Solves #203

Brookke commented 10 months ago

Hey @mattbretl I've opened this PR, to address #203. Normally I would would wait for sign off on the proposal before hand. But, given I had already written the code when testing this locally, I thought I would open a PR with the change since it was fairly small. Would love to get a review when you get a moment :)

benjie commented 10 months ago

This can also be achieved via a plugin:

In V4:

import { makeAddInflectorsPlugin } from "graphile-utils";

export const PrefixFilterPlugin = makeAddInflectorsPlugin(
  (inflectors, build, options) => {
    const {
      filterType: oldFilterType,
      filterFieldType: oldFilterFieldType,
      filterFieldListType: oldFilterFieldListType,
    } = inflectors;
    const { connectionFilterTypePrefix = "" } = options;
    return {
      filterType(typeName) {
        return connectionFilterTypePrefix + oldFilterType.call(this, typeName);
      },
      filterFieldType(typeName) {
        return (
          connectionFilterTypePrefix + oldFilterFieldType.call(this, typeName)
        );
      },
      filterFieldListType(typeName) {
        return (
          connectionFilterTypePrefix +
          oldFilterFieldListType.call(this, typeName)
        );
      },
    };
  },
  true,
);

And of course, It's Better In V5™️:

function prefix(prev, preset, typeName) {
  const { schema: { connectionFilterTypePrefix = "" } } = preset;
  return connectionFilterTypePrefix + prev(typeName);
}

export const PrefixFilterPlugin = {
  name: "PrefixFilterPlugin",
  inflection: {
    replace: {
      filterType: prefix,
      filterFieldType: prefix,
      filterFieldListType: prefix,
    },
  },
};
Brookke commented 9 months ago

This can also be achieved via a plugin:

In V4:

import { makeAddInflectorsPlugin } from "graphile-utils";

export const PrefixFilterPlugin = makeAddInflectorsPlugin(
  (inflectors, build, options) => {
    const {
      filterType: oldFilterType,
      filterFieldType: oldFilterFieldType,
      filterFieldListType: oldFilterFieldListType,
    } = inflectors;
    const { connectionFilterTypePrefix = "" } = options;
    return {
      filterType(typeName) {
        return connectionFilterTypePrefix + oldFilterType.call(this, typeName);
      },
      filterFieldType(typeName) {
        return (
          connectionFilterTypePrefix + oldFilterFieldType.call(this, typeName)
        );
      },
      filterFieldListType(typeName) {
        return (
          connectionFilterTypePrefix +
          oldFilterFieldListType.call(this, typeName)
        );
      },
    };
  },
  true,
);

And of course, It's Better In V5™️:

function prefix(prev, preset, typeName) {
  const { schema: { connectionFilterTypePrefix = "" } } = preset;
  return connectionFilterTypePrefix + prev(typeName);
}

export const PrefixFilterPlugin = {
  name: "PrefixFilterPlugin",
  inflection: {
    replace: {
      filterType: prefix,
      filterFieldType: prefix,
      filterFieldListType: prefix,
    },
  },
};

Thanks for this @benjie this will unlock us. I will leave this PR open for now as I think that the ergonomics of this PR are nicer, but understand if it gets closed in favour of suggesting people write a custom plugin

benjie commented 8 months ago

Since there has been little interest in this and the workaround is straightforward I'm going to go ahead and close this. Thanks for submitting it though, @Brookke!