Skip to content

Commit 0d93765

Browse files
committed
fix(graphql-default-value-transformer): @default may not be applied to primary key fields
1 parent 5db3514 commit 0d93765

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/amplify-graphql-default-value-transformer/src/graphql-default-value-transformer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,21 @@ const validateDefaultValueType = (ctx: TransformerSchemaVisitStepContextProvider
7272
}
7373
};
7474

75+
const validateNotPrimaryKey = (field: FieldDefinitionNode): void => {
76+
let isPrimaryKeyField =
77+
field.directives!.find((dir) => dir.name.value === 'primaryKey') ||
78+
(getBaseType(field.type) === 'ID' && field.type.kind === Kind.NON_NULL_TYPE && field.name.value === 'id');
79+
80+
if (isPrimaryKeyField) {
81+
throw new InvalidDirectiveError('The @default directive may not be applied to primaryKey fields.');
82+
}
83+
};
84+
7585
const validate = (ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void => {
7686
validateModelDirective(config);
7787
validateFieldType(ctx, config.field.type);
7888
validateDirectiveArguments(config.directive);
89+
validateNotPrimaryKey(config.field);
7990

8091
// Validate the default values only for the DynamoDB datasource.
8192
// For SQL, the database determines and sets the default value. We will not validate the value in transformers.

0 commit comments

Comments
 (0)