From 0d937653f25fc936f11234a1cef6eae195984351 Mon Sep 17 00:00:00 2001 From: Peter Vaiciulis Date: Thu, 12 Sep 2024 09:38:44 -0700 Subject: [PATCH] fix(graphql-default-value-transformer): @default may not be applied to primary key fields --- .../src/graphql-default-value-transformer.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/amplify-graphql-default-value-transformer/src/graphql-default-value-transformer.ts b/packages/amplify-graphql-default-value-transformer/src/graphql-default-value-transformer.ts index 5802deada6..6fa7e60a7a 100644 --- a/packages/amplify-graphql-default-value-transformer/src/graphql-default-value-transformer.ts +++ b/packages/amplify-graphql-default-value-transformer/src/graphql-default-value-transformer.ts @@ -72,10 +72,21 @@ const validateDefaultValueType = (ctx: TransformerSchemaVisitStepContextProvider } }; +const validateNotPrimaryKey = (field: FieldDefinitionNode): void => { + let isPrimaryKeyField = + field.directives!.find((dir) => dir.name.value === 'primaryKey') || + (getBaseType(field.type) === 'ID' && field.type.kind === Kind.NON_NULL_TYPE && field.name.value === 'id'); + + if (isPrimaryKeyField) { + throw new InvalidDirectiveError('The @default directive may not be applied to primaryKey fields.'); + } +}; + const validate = (ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void => { validateModelDirective(config); validateFieldType(ctx, config.field.type); validateDirectiveArguments(config.directive); + validateNotPrimaryKey(config.field); // Validate the default values only for the DynamoDB datasource. // For SQL, the database determines and sets the default value. We will not validate the value in transformers.