New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arbitrary string ids #8645
Arbitrary string ids #8645
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit a30bbb1:
|
I can not wait to get my hands on this, I was going to write custom prisma extension/patch to do this for me |
Thanks @molomby! |
👏 I will open new issue or create PR when i have time |
Take care, will wait. |
In alignment with general best practice and the GraphQL spec, Keystone likes IDs to be opaque and arbitrary – without any meaning or reference to the data they represent. But Keystone goes beyond these recommendations, limiting devs to 3 specific types of ID: auto incs, uuids, and cuids.
This was a design decision intended to lead devs away from "meaningful" ids that often cause problem as a project grows. I've argued against custom IDs myself in the past, but the fact is, it's perfectly reasonable for devs to want more control over their IDs and it's not Keystone's place to stand in the way.
For example, the current implementation prevents devs from using the newer version of cuids (cuid2), ulids, Nano ID or any number of other suitable ID formats. For a system that prides itself on the "escape hatches" it offers devs, intentionally blocking these options is counterproductive.
Fortunately, it's easy to fix. I've done so here by introducing a
kind: 'string'
option for theidField
config. The new kind falls back tocuid
values by default but allows the user to provide any string value they want in aresolveInput
hook. Theid
field filter code has been updated to allow any string to be queried. I've added a simple example project to demo the functionality.As above, this change is intended only to allow custom ID formats, not wholly custom ID values. As such it doesn't add the
id
field to the create or update GraphQL types for a list, so it doesn't address #4824 / #6034.It's also worth noting that the exact functionality of these ids will differ by DB provider due to case-sensitivity. This is a complexity developers will be opting into by overriding the default id behaviour.