Skip to content

Commit

Permalink
fix(graphql-default-value-transformer): @default may not be applied t…
Browse files Browse the repository at this point in the history
…o primary key fields
  • Loading branch information
p5quared committed Sep 12, 2024
1 parent 5db3514 commit cbc28fd
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,21 @@ const validateDefaultValueType = (ctx: TransformerSchemaVisitStepContextProvider
}
};

const validateNotPrimaryKey = (field: FieldDefinitionNode): void => {
const 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.
Expand Down

0 comments on commit cbc28fd

Please sign in to comment.