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

fix: un-require required input fields with @default #2867

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -189,7 +189,7 @@ input ModelTestConditionInput {

input CreateTestInput {
id: ID
stringValue: String!
stringValue: String
}

input UpdateTestInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,54 @@ describe('DefaultValueModelTransformer:', () => {
).toThrow('Default value "text" is not a valid AWSIPAddress.');
});

it('throws if @default is used on an implicit primaryKey', () => {
const schema = `
type Test @model {
id: ID! @default(value: "ID: 80361")
stringValue: String!
}
`;

expect(() =>
testTransform({
schema,
transformers: [new ModelTransformer(), new DefaultValueTransformer()],
}),
).toThrow('The @default directive may not be applied to primaryKey fields.');
});

it('throws if @default is used on an explicit primaryKey', () => {
const schema = `
type Test @model {
id: ID! @default(value: "ID: 80361") @primaryKey
stringValue: String!
}
`;

expect(() =>
testTransform({
schema,
transformers: [new ModelTransformer(), new DefaultValueTransformer(), new PrimaryKeyTransformer()],
}),
).toThrow('The @default directive may not be applied to primaryKey fields.');
});

it('throws if @default is used on a composite key member', () => {
const schema = `
type Project @model {
projectId: ID! @primaryKey(sortKeyFields: ["name"])
name: String! @default(value: "Mustapha Mond")
}
`;

expect(() =>
testTransform({
schema,
transformers: [new ModelTransformer(), new DefaultValueTransformer(), new PrimaryKeyTransformer()],
}),
).toThrow('The @default directive may not be applied to composite key member fields.');
});

it('should validate enum values', async () => {
const inputSchema = `
type Post @model {
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 @@ -72,10 +73,34 @@ 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');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one more case to consider here which is when the @default is applied to fields part of a composite primary key. Gen2 doc or this Gen1 doc is more explicit about the schema with composite primary key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a separate check scanning over sortKeyFields.

if (isPrimaryKeyField) {
throw new InvalidDirectiveError('The @default directive may not be applied to primaryKey fields.');
}
};

const validateNotCompositeKeyMember = (config: DefaultValueDirectiveConfiguration): void => {
const objectDirectives = config.object.fields?.flatMap((f) => f.directives);
const primaryKeyDirective = objectDirectives?.find((dir) => dir?.name.value === 'primaryKey');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we refactor this check to see if the field is a primary key into it's own method that can be re-used between this method and the one above?

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 Expand Up @@ -123,6 +148,7 @@ export class DefaultValueTransformer extends TransformerPluginBase {
const input = InputObjectDefinitionWrapper.fromObject(name, config.object, ctx.inputDocument);
const fieldWrapper = input.fields.find((f) => f.name === config.field.name.value);
fieldWrapper?.makeNullable();
ctx.output.updateInput(input.serialize());
}
}
};
Expand Down
Loading