aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Ability to use short ID instead of UUID v4 #2674

Closed ChristopheBougere closed 4 years ago

ChristopheBougere commented 4 years ago

Is your feature request related to a problem? Please describe. I am using GraphQL resource IDs in my app URL. However they aren't SEO friendly (UUID v4 is 36 chars). I would like to replace them by short IDs instead, but IDs are automatically generated on create mutations.

Describe the solution you'd like In my schema.graphql, I would like having the choice between let say ID and ShortID types.

Describe alternatives you've considered I thought about creating my own ShortID type, but in generated resolvers it seems to be using a $util.autoId() function (see docs). I don't know how I could generate an ID without a VTL function. And I don't want to have custom functions for each of my resolvers.

Additional context I am not sure this issue only depends on the Amplify team, maybe as well on AppSync team. But maybe this is already planned (because SEO is quite important when running in production), or someone has a great workaround ?

kaustavghosh06 commented 4 years ago

@ChristopheBougere You could use @key directive and specify your own primary key instead of using the default id primary key.

ChristopheBougere commented 4 years ago

Oh that's interesting I totally missed that directive. So instead of having this:

type Customer @model {
    id: ID!
    email: String!
    username: String
}

I would have this:

type Customer @model @key(fields: ["id"]) {
    id: String!
    email: String!
    username: String
}

And I would need to generate the id from the frontend and pass it during the create mutation? That looks great, I am just wondering if this could be risky to generate the ID from the frontend?

Thanks for your help @kaustavghosh06

attilah commented 4 years ago

@ChristopheBougere the resolver only assigns a value to id during create if it was not specified in the input. If you decide to control it, it works fine, that is something you can control, how you want to do it. I'd suggest that for database storage stick to uuidv4 and use shortid as an additional attribute and populate it from the "client" (API facade?). Since probably you'll need to query it I'd define a named @key to have a GSI generated for it.

The main reason I suggest this, that it is much easier to hit a collision with shortid because of its lower entropy.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.