Skip to content
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

default ID prefix option as custom resolver #2455

Open
2 tasks
harshit9715 opened this issue Apr 15, 2024 · 1 comment
Open
2 tasks

default ID prefix option as custom resolver #2455

harshit9715 opened this issue Apr 15, 2024 · 1 comment

Comments

@harshit9715
Copy link

harshit9715 commented Apr 15, 2024

Describe the feature you'd like to request

Hi Team, was wondering if it was possible to create a custom directive eg: @Prefix('String').. that would add that prefix to the uuids generated in dynamodb.

Describe the solution you'd like

type User
  @searchable
  @prefix('usr_')
  @model(subscriptions: { level: off }) {
  id: ID!
  username: String!
  clerkId: ID!
  email: AWSEmail!
  name: String
  bio: String
  picture: String
  location: String
  portfolioWebsite: String
  reputation: Int
  # saved: [ID]
  role: Role! @default(value: "USER")
}

Using this directive will simply update the uuid and add a prefix on the autogenerated one.
9fbbfda1-bc51-4cf7-ba45-8a8042a40462 -> usr_9fbbfda1-bc51-4cf7-ba45-8a8042a40462

Expansion on idea: for field level we can do the same.. the value passed from mutations get a prefix automatically.

Describe alternatives you've considered

I can use something like uuid package and pass my own IDs. The only problem is that there is a management overhead when a new model is added. its needs to be added here. (and for manyToMany models too)

I tried overriding the values using slots

const getPrefix = (fieldName: string) => {
  // for createUser, we should get u_
  // for createQuestionTags we should get qt_
  // get only the first letter of each word and join them
  const allCapsArray = fieldName
    .replace("create", "")
    .split(/(?=[A-Z])/)
    .map((word) => word[0].toLowerCase());
  return allCapsArray.join("") + "_";
};


const graphApi = new AmplifyGraphqlApi(stack, "GraphqlApi", {
    definition: AmplifyGraphqlDefinition.fromFiles(
      "stacks/amplify-schema.graphql"
    ),
    apiName: "GraphqlApi",
    translationBehavior: {},
    transformerPlugins: [],
    functionSlots: [
      "createQuestionTags",
      "createQuestion",
      "createTag",
      "createUser",
      "createAnswer",
    ].map((fieldName) => ({
      fieldName: fieldName,
      function: {
        requestMappingTemplate:
          MappingTemplate.fromString(`## [Start] Request mapping template.
## get the "fieldName" from ctx.stash and add it as a prefix to id.. make sure to replace 'create' word with ''
$util.qr($ctx.stash.defaultValues.put("id", "${getPrefix(fieldName)}$util.autoId()"))
## [End] Initialization default values. **`),
      },
      slotIndex: 0,
      slotName: "init",
      typeName: "Mutation",
    })),
    authorizationModes: {
      apiKeyConfig: {
        expires: Duration.days(365),
        description: "API Key for GraphQL API - " + stack.stage,
      },
      defaultAuthorizationMode: "OPENID_CONNECT",
      oidcConfig: {
        oidcIssuerUrl: process.env.CLERK_ISSUER_BASE_URL!,
        oidcProviderName: "Clerk",
        tokenExpiryFromAuth: Duration.millis(0),
        tokenExpiryFromIssue: Duration.millis(0),
      },
    },
  });

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request

Would this feature include a breaking change?

  • ⚠️ This feature might incur a breaking change
@AnilMaktala
Copy link

Hey @harshit9715, Thank you for request this. We are marking this as a feature request for the team to evaluate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants