Skip to content

Commit

Permalink
feat: add optional custom label directive (#104)
Browse files Browse the repository at this point in the history
* feat: add optional custom label directive

Useful for labelling "Hero Block (depcreated)" or altering user-friendly labels without breaking the schema.

* refactor: simplify label directive

* chore: add tests and update changeset

* chore: update custom label in tests to be different to the normal fallback
  • Loading branch information
mattisherwood authored Aug 12, 2024
1 parent dd92fc4 commit 6131e70
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/lazy-clocks-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"amplience-graphql-codegen-terraform": minor
"amplience-graphql-codegen-common": minor
---

Add optional directive for custom label override
1 change: 1 addition & 0 deletions packages/common/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export const schemaPrepend = gql`
visualizations: Boolean
icon: String
autoSync: Boolean
label: String
) on OBJECT
`;
18 changes: 18 additions & 0 deletions packages/plugin-terraform/examples/output/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,21 @@ resource "amplience_content_type_assignment" "test_visualizations" {
repository_id = data.amplience_content_repository.website1.id
}


resource "amplience_content_type_schema" "test_custom_label" {
body = file("${path.module}/schemas/test-custom-label.json")
schema_id = "https://schema-examples.com/test-custom-label"
validation_level = "CONTENT_TYPE"
auto_sync = true
}

resource "amplience_content_type" "test_custom_label" {
content_type_uri = amplience_content_type_schema.test_custom_label.schema_id
label = "Test Altered Custom Label"
status = "ACTIVE"
}

resource "amplience_content_type_assignment" "test_custom_label" {
content_type_id = amplience_content_type.test_custom_label.id
repository_id = data.amplience_content_repository.website1.id
}
6 changes: 5 additions & 1 deletion packages/plugin-terraform/src/lib/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ export const createObjectTypeVisitor =
? maybeDirectiveValue<StringValueNode>(directive, "icon")?.value
: undefined;

const label = directive
? maybeDirectiveValue<StringValueNode>(directive, "label")?.value
: undefined;

const dynamicVisualization = visualization?.find(hasProperty("for_each"));

const contentType = tfg.resource("amplience_content_type", name, {
content_type_uri: schema.attr("schema_id"),
label: capitalCase(node.name.value),
label: label ?? capitalCase(node.name.value),
icon: iconUrl ? { size: 256, url: iconUrl } : undefined,
status: "ACTIVE",
'dynamic"visualization"':
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-terraform/test/testdata/base.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type TestNoAutoSync @amplienceContentType {
name: String
}

type TestCustomLabel @amplienceContentType(label: "Test Altered Custom Label") {
name: String
}

type NotATest {
name: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type TestNoAutoSync @amplienceContentType {
name: String
}

type TestCustomLabel @amplienceContentType(label: "Test Altered Custom Label") {
name: String
}

type NotATest {
name: String!
}
18 changes: 18 additions & 0 deletions packages/plugin-terraform/test/testdata/expected/base.tf
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,21 @@ resource "amplience_content_type_assignment" "test_no_auto_sync"{
content_type_id = amplience_content_type.test_no_auto_sync.id
repository_id = data.amplience_content_repository.website1.id
}

resource "amplience_content_type_schema" "test_custom_label"{
body = file("${path.module}/schemas/test-custom-label.json")
schema_id = "https://schema-examples.com/test-custom-label"
validation_level = "CONTENT_TYPE"
auto_sync = true
}

resource "amplience_content_type" "test_custom_label"{
content_type_uri = amplience_content_type_schema.test_custom_label.schema_id
label = "Test Altered Custom Label"
status = "ACTIVE"
}

resource "amplience_content_type_assignment" "test_custom_label"{
content_type_id = amplience_content_type.test_custom_label.id
repository_id = data.amplience_content_repository.website1.id
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,22 @@ for_each = var.variables["CONTENT_REPO_MAP"]
content_type_id = amplience_content_type.test_no_auto_sync.id
repository_id = each.value
}

resource "amplience_content_type_schema" "test_custom_label"{
body = file("${path.module}/schemas/test-custom-label.json")
schema_id = "https://schema-examples.com/test-custom-label"
validation_level = "CONTENT_TYPE"
auto_sync = true
}

resource "amplience_content_type" "test_custom_label"{
content_type_uri = amplience_content_type_schema.test_custom_label.schema_id
label = "Test Altered Custom Label"
status = "ACTIVE"
}

resource "amplience_content_type_assignment" "test_custom_label"{
for_each = var.variables["CONTENT_REPO_MAP"]
content_type_id = amplience_content_type.test_custom_label.id
repository_id = each.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,21 @@ resource "amplience_content_type_assignment" "test_no_auto_sync"{
content_type_id = amplience_content_type.test_no_auto_sync.id
repository_id = data.amplience_content_repository.website1.id
}

resource "amplience_content_type_schema" "test_custom_label"{
body = file("${path.module}/schemas/test-custom-label.json")
schema_id = "https://schema-examples.com/test-custom-label"
validation_level = "CONTENT_TYPE"
auto_sync = true
}

resource "amplience_content_type" "test_custom_label"{
content_type_uri = amplience_content_type_schema.test_custom_label.schema_id
label = "Test Altered Custom Label"
status = "ACTIVE"
}

resource "amplience_content_type_assignment" "test_custom_label"{
content_type_id = amplience_content_type.test_custom_label.id
repository_id = data.amplience_content_repository.website1.id
}

0 comments on commit 6131e70

Please sign in to comment.