Skip to content

Commit

Permalink
fix(graphql): disable default value validation for rds
Browse files Browse the repository at this point in the history
  • Loading branch information
sundersc committed Sep 18, 2023
1 parent 2631c02 commit 8cd3b26
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { validateModelSchema } from '@aws-amplify/graphql-transformer-core';
import { DatasourceType, validateModelSchema } from '@aws-amplify/graphql-transformer-core';
import { parse } from 'graphql';
import { testTransform } from '@aws-amplify/graphql-transformer-test-utils';
import { DefaultValueTransformer } from '..';
Expand Down Expand Up @@ -311,4 +311,33 @@ describe('DefaultValueModelTransformer:', () => {
const schema = parse(out.schema);
validateModelSchema(schema);
});

it('default value type should not be validated for rds datasource', async () => {
const validSchema = `
type Note @model {
id: ID!
content: String!
createdAt: AWSDateTime @default(value: "CURRENT_TIMESTAMP")
}
`;

const modelToDatasourceMap = new Map<string, DatasourceType>();
modelToDatasourceMap.set('Note', {
dbType: 'MySQL',
provisionDB: false,
});
const out = testTransform({
schema: validSchema,
transformers: [new ModelTransformer(), new DefaultValueTransformer()],
modelToDatasourceMap,
});
expect(out).toBeDefined();

validateModelSchema(parse(out.schema));
expect(out.stacks).toBeDefined();
expect(out.stacks.RdsApiStack).toBeDefined();
expect(out.stacks.RdsApiStack.Resources).toBeDefined();
expect(out.resolvers['Mutation.createNote.init.1.req.vtl']).toBeDefined();
expect(out.resolvers['Mutation.createNote.init.2.req.vtl']).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
DDB_DB_TYPE,
DirectiveWrapper,
generateGetArgumentsInput,
InputObjectDefinitionWrapper,
Expand Down Expand Up @@ -79,7 +80,18 @@ const validate = (ctx: TransformerSchemaVisitStepContextProvider, config: Defaul
validateModelDirective(config);
validateFieldType(ctx, config.field.type);
validateDirectiveArguments(config.directive);
validateDefaultValueType(ctx, config);

// Validate the default values only for the DynamoDB datasource.
// For RDS, the database determines and sets the default value. We will not validate the value in transformers.
const isDynamoDB = isDynamoDBDatasource(ctx, config.object.name.value);
if (isDynamoDB) {
validateDefaultValueType(ctx, config);
}
};

const isDynamoDBDatasource = (ctx: TransformerSchemaVisitStepContextProvider, modelName: string): boolean => {
const isDynamoDB = (ctx.modelToDatasourceMap.get(modelName)?.dbType ?? DDB_DB_TYPE) === DDB_DB_TYPE;
return isDynamoDB;
};

export class DefaultValueTransformer extends TransformerPluginBase {
Expand Down Expand Up @@ -128,6 +140,13 @@ export class DefaultValueTransformer extends TransformerPluginBase {
const context = ctx as TransformerContextProvider;

for (const typeName of this.directiveMap.keys()) {

// Set the default value only for DDB datasource. For RDS, the database will set the value.
const isDynamoDB = isDynamoDBDatasource(ctx, typeName);
if (!isDynamoDB) {
continue;
}

const snippets: string[] = [];
for (const config of this.directiveMap.get(typeName)!) {
const fieldName = config.field.name.value;
Expand Down

0 comments on commit 8cd3b26

Please sign in to comment.