Skip to content

Commit

Permalink
allow using AWSTimestamp as type too
Browse files Browse the repository at this point in the history
  • Loading branch information
flogy committed Oct 28, 2020
1 parent 4551e1d commit c907569
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ Append `@ttl` to target fields.
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
expirationUnixTime: AWSTimestamp! @ttl
}
```

It is important that the field you use the directive is of type `Int`, as the expiration timestamp must be in [Unix time](https://en.wikipedia.org/wiki/Unix_time) format.
It is important that the field you use the directive is of type `AWSTimestamp` (recommended) or `Int`, as the expiration timestamp must be in [Unix time](https://en.wikipedia.org/wiki/Unix_time) format.

## Contribute 🦸

Expand Down
38 changes: 30 additions & 8 deletions src/__tests__/ttl-transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test("@ttl directive can be used on fields", () => {
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
expirationUnixTime: AWSTimestamp! @ttl
}
`;
expect(() => transformer.transform(schema)).not.toThrow();
Expand All @@ -30,15 +30,15 @@ test("@ttl directive can not be used on types", () => {
type ExpiringChatMessage @model @ttl {
id: ID!
message: String
expirationUnixTime: Int!
expirationUnixTime: AWSTimestamp!
}
`;
expect(() => transformer.transform(schema)).toThrowError(
'Directive "ttl" may not be used on OBJECT.'
);
});

test("@ttl directive can only be used on fields of type Int", () => {
test("@ttl directive can not be used on fields other than Int and AWSTimestamp", () => {
const schema = `
type ExpiringChatMessage @model {
id: ID!
Expand All @@ -47,17 +47,39 @@ test("@ttl directive can only be used on fields of type Int", () => {
}
`;
expect(() => transformer.transform(schema)).toThrowError(
'Directive "ttl" must be used only on Int type fields.'
'Directive "ttl" must be used only on AWSTimestamp or Int type fields.'
);
});

test("@ttl directive can be used on fields with AWSTimestamp type", () => {
const schema = `
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: AWSTimestamp! @ttl
}
`;
expect(() => transformer.transform(schema)).not.toThrow();
});

test("@ttl directive can be used on fields with Int type", () => {
const schema = `
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
}
`;
expect(() => transformer.transform(schema)).not.toThrow();
});

test("Only one @ttl directive per type is allowed", () => {
const schema = `
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
anotherExpirationUnixTime: Int! @ttl
expirationUnixTime: AWSTimestamp! @ttl
anotherExpirationUnixTime: AWSTimestamp! @ttl
}
`;
expect(() => transformer.transform(schema)).toThrowError(
Expand Down Expand Up @@ -90,7 +112,7 @@ test("Generated CloudFormation document contains the TimeToLiveSpecification pro
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
expirationUnixTime: AWSTimestamp! @ttl
}
`;
const properties = getPropertiesOfSchemaTable(schema, "ExpiringChatMessage");
Expand All @@ -103,7 +125,7 @@ test("TimeToLiveSpecification property is pointing to the field where the @ttl d
type ExpiringChatMessage @model {
id: ID!
message: String
expirationUnixTime: Int! @ttl
expirationUnixTime: AWSTimestamp! @ttl
}
`;
const properties = getPropertiesOfSchemaTable(schema, "ExpiringChatMessage");
Expand Down
4 changes: 2 additions & 2 deletions src/ttl-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class TtlTransformer extends Transformer {
directive: DirectiveNode,
acc: TransformerContext
) => {
if (getBaseType(definition.type) !== "Int") {
if (!["AWSTimestamp", "Int"].includes(getBaseType(definition.type))) {
throw new InvalidDirectiveError(
'Directive "ttl" must be used only on Int type fields.'
'Directive "ttl" must be used only on AWSTimestamp or Int type fields.'
);
}

Expand Down

0 comments on commit c907569

Please sign in to comment.