Benefits of using the enum functionality? #6462
-
What's the benefit of setting a table as an enum? (Based on this page: https://hasura.io/docs/1.0/graphql/core/schema/enums.html) Can't I just create a table with just a primary key as a string, and not set it to enum? The only benefit I see is that within the hasura console UI, if you add new rows, and a field has a foreign key to an enum, you can select the value from a dropdown box, instead of having to type it out. But I rarely do this anyway, since my rows are generated on my client. So are there any benefits to setting a table as an enum? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If you set a table as ENUM those values will be in the GraphQL schema. If the client tries to send a different value it will error out without hitting the database. It also makes it easier to test. You can just introspect the schema to see the allowed values, instead of making a request to the server. But there are a few limitations with it and it is not always the best solution. For example, if the ENUM value change you need to reset the metadata to see them in the schema. If the values are dynamic or have the possibility to change I would avoid the ENUM. Also it is saved as a field in the table and not as a relationship. Sometimes it is better to have a relationship. I used it for things that have a predefined value and won't change. For example gender, role, etc |
Beta Was this translation helpful? Give feedback.
@joshuarobs
If you set a table as ENUM those values will be in the GraphQL schema. If the client tries to send a different value it will error out without hitting the database. It also makes it easier to test. You can just introspect the schema to see the allowed values, instead of making a request to the server.
But there are a few limitations with it and it is not always the best solution. For example, if the ENUM value change you need to reset the metadata to see them in the schema. If the values are dynamic or have the possibility to change I would avoid the ENUM. Also it is saved as a field in the table and not as a relationship. Sometimes it is better to have a relationship.
I used …