If I called works.category it will just show undefined but if I map through the works it will show each category per piece not as a collection along with all other fields within an object.
Is there anyway to map or iterate through all of the enums on strapi.js?
As the enumeration values are statically typed (stored in the model config) we don’t have a method for fetching those values as it’s generally expected the frontend also would have them statically typed.
To fetch those you will either need to make a custom GraphQL resolve or a custom REST route/controller to fetch the data from the model information.
Hi @DMehaffy, I also have the same need and I do not agree with your first solution that we should store enum values on both frontend & backend, no one wants to maintain two lists for several reasons.
The second solution solves this issue but it would be nice to consider to add a convenient way to query enum values.
Thanks
I do agree with you that it doesn’t fit every need. The specifics was more so done via the definition of “enumeration” values and how they are treated in programming.
I was trying to find a solution to the same issue and found this “hacky” way to get enum values from Strapi, by making a GET request on the /content-type-builder/content-types/:uid endpoint which you’ll find in the Content-Type-Builder section of the User & Permissions Plugin
So you wrote a custom controller that returns strapi.models[ctx.query.model].allAttributes after checking to ensure that the user has queried an existing model and whether the user has the requisite permissions.
More or less yes, I have mine hard coded to check a specific role’s permissions (public) but you could check the ctx.state.user.role.id and query based on that.
It creates a type definition file (on build or whenever you call it) directly from the schema which is pulled from strapis graphql endpoint, allowing you to iterate over the enum type in the frontend, without having you maintain multiple lists. I hope this helps.