ra-postgraphile icon indicating copy to clipboard operation
ra-postgraphile copied to clipboard

Expose Enums

Open BowlingX opened this issue 6 years ago • 1 comments

To make usage of enums easier in react-admin. We should provide a hook to query those.

BowlingX avatar Sep 30 '19 10:09 BowlingX

Hi, I recently have been struggling to make this work for value filtering and I found a workaround for simple string based enums. The thing is that we define all our enum types as ('aa','bb'....) all named as XXXchoices, this way we can identify them easily. With a quick check in the filters.js file we check if the normalizedName ends with choices and we treat them as they where strings but only for equalTo values as like does not make much sense here. I leave the code sample below hoping someone finds it interesting: (in build/module/filters.js

// ...
// code omitted as nothing changed
export var mapFilterType = function (type, value, key) {
    var _a, _b, _c, _d, _e;
    var normalizedName = type.name.toLowerCase();
    // precheck for choices enums
    if (normalizedName.endsWith('choices')) {
        // returns the same as 'string' but without the '%like%' part
        return {
                or: [
                    (_b = {},
                        _b[key] = {
                            equalTo: value,
                        },
                        _b),
                ],
            }; 
    } else {
        // normal type matching
        switch (normalizedName) {
            // ...
            // code omitted as nothing changed
        }
    }
};
// ...
// code omitted as nothing changed

Sorry but I don't use too much TS, thanks for the great package BTW!

EDIT (26/11/2020): as for the 4.2.0 update of ra-postgraphile, this workaround is no longer required so I strongly advise anyone facing my same issue to update to that version. Thanks a lot!

jonArzelus avatar Sep 23 '20 12:09 jonArzelus