Skip to content

Commit

Permalink
fix(graphql-default-value-transformer): @default cannot be used on co…
Browse files Browse the repository at this point in the history
…mposite key members
  • Loading branch information
p5quared committed Sep 16, 2024
1 parent de7cc9e commit e00975f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"src/**/*.ts"
],
"coveragePathIgnorePatterns": [
"/__tests__/"
"/__tests__/",
"types.ts"
],
"snapshotFormat": {
"escapeString": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FieldDefinitionNode,
InterfaceTypeDefinitionNode,
Kind,
ListValueNode,
ObjectTypeDefinitionNode,
StringValueNode,
TypeNode,
Expand Down Expand Up @@ -82,11 +83,24 @@ const validateNotPrimaryKey = (field: FieldDefinitionNode): void => {
}
};

const validateNotCompositeKeyMember = (config: DefaultValueDirectiveConfiguration): void => {
const objectDirectives = config.object.fields?.flatMap((f) => f.directives);
const primaryKeyDirective = objectDirectives?.find((dir) => dir?.name.value === 'primaryKey');
if (primaryKeyDirective) {
const sortKeyFields = primaryKeyDirective.arguments?.find((arg) => arg.name.value === 'sortKeyFields')?.value as ListValueNode;
const sortKeys = sortKeyFields?.values as StringValueNode[];
if (sortKeys?.some((sortKey) => sortKey.value === config.field.name.value)) {
throw new InvalidDirectiveError('The @default directive may not be applied to composite key member fields.');
}
}
};

const validate = (ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void => {
validateModelDirective(config);
validateFieldType(ctx, config.field.type);
validateDirectiveArguments(config.directive);
validateNotPrimaryKey(config.field);
validateNotCompositeKeyMember(config);

// 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 e00975f

Please sign in to comment.