2020-05-17
|~2 min read
|390 words
Recently, I wanted to introduce permissions to an app I was building.
Keeping things very simple to start, I decided to have two permissions: Admin or User.
I want to keep flexibility for the future, and compose permissions bottom-up - rather than have singular groups that can balloon in number that I need to manage.
To implement this then, I decided to:
The result looked a little like:
type User {
id: ID! @id
name: String!
email: String! @unique
password: String!
salt: String!
+ permissions: [Permission]
}
+ enum Permission {
+ ADMIN
+ USER
+ }
While this data model reads nicely, Prisma will reject it as it’s missing an @scalarList
directive.
$ prisma deploy
Deploying service `xxx` to stage `dev` to server `prisma-us1` 171ms
Errors:
User
✖ Valid values for the strategy argument of `@scalarList` are: RELATION.
The error message is quite explicit, if not clear.
From the Prisma docs:
@scalarList
This
@scalarList(strategy: STRATEGY!)
directive is required on any scalar list field. The only valid argument for thestrategy
argument isRELATION
.type Post { tags: [String!]! @scalarList(strategy: RELATION) }
The fix is a one line change:
type User {
id: ID! @id
name: String!
email: String! @unique
password: String!
salt: String!
- permissions: [Permission]
+ permissions: [Permission] @scalarList(strategy: RELATION)
}
enum Permission {
ADMIN
USER
}
With that one change made, when we redeploy we see all green:
$ prisma deploy
Deploying service `xxx` to stage `dev` to server `prisma-us1` 194ms
Changes:
User (Type)
+ Created field `permissions` of type `[Enum!]!`
Permission (Enum)
+ Created enum Permission with values `ADMIN`, `USER`
Applying changes 1.4s
Embedding enums directly into your data model can be great for managing lists of options. There are, however, some caveats to their use. In this case, the issue had to do with the use of the array (i.e. a list), and not so much with the fact that the type was an Enum. The same solution would have been required even if the list of permissions had been a string (as indicated by the example from the docs).
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!