From 1469029cf48d3629a8afb1beb2ec033ef9ccd9cd Mon Sep 17 00:00:00 2001 From: Dave New Date: Mon, 3 Feb 2025 14:49:31 +0200 Subject: [PATCH] fix: computed fields as inputs (#1717) --- node/codegen.go | 8 ++++++ node/codegen_test.go | 26 ++++++++++++++++++- .../testdata/errors/attribute_computed.keel | 10 +++++++ schema/validation/rules/actions/input_type.go | 11 ++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/node/codegen.go b/node/codegen.go index bfc2c1cb0..1abb5c43a 100644 --- a/node/codegen.go +++ b/node/codegen.go @@ -234,6 +234,10 @@ func writeUpdateValuesType(w *codegen.Writer, model *proto.Model) { continue } + if field.ComputedExpression != nil { + continue + } + w.Write(field.Name) w.Write(": ") t := toTypeScriptType(field.Type, true, false, false) @@ -341,6 +345,10 @@ func writeCreateValuesType(w *codegen.Writer, schema *proto.Schema, model *proto w.Writef("// if providing a value for this field do not also set %s\n", strings.TrimSuffix(field.Name, "Id")) } + if field.ComputedExpression != nil { + continue + } + w.Write(field.Name) if field.Optional || field.DefaultValue != nil || field.IsHasMany() || field.ComputedExpression != nil { w.Write("?") diff --git a/node/codegen_test.go b/node/codegen_test.go index 75257e23b..0057be154 100644 --- a/node/codegen_test.go +++ b/node/codegen_test.go @@ -138,7 +138,6 @@ export type PersonCreateValues = { bio: string file: runtime.InlineFile | runtime.File canHoldBreath: runtime.Duration - heightInMetres?: number id?: string createdAt?: Date updatedAt?: Date @@ -149,6 +148,31 @@ export type PersonCreateValues = { }) } +func TestWriteUpdateValuesInterface(t *testing.T) { + t.Parallel() + expected := ` +export type PersonUpdateValues = { + firstName: string + lastName: string | null + age: number + dateOfBirth: Date + gender: Gender + hasChildren: boolean + tags: string[] + height: number + bio: string + file: runtime.InlineFile | runtime.File + canHoldBreath: runtime.Duration + id: string + createdAt: Date + updatedAt: Date +}` + runWriterTest(t, testSchema, expected, func(s *proto.Schema, w *codegen.Writer) { + m := s.FindModel("Person") + writeUpdateValuesType(w, m) + }) +} + func TestWriteCreateValuesInterfaceWithRelationships(t *testing.T) { t.Parallel() schema := ` diff --git a/schema/testdata/errors/attribute_computed.keel b/schema/testdata/errors/attribute_computed.keel index 7ce84899a..611e3aecc 100644 --- a/schema/testdata/errors/attribute_computed.keel +++ b/schema/testdata/errors/attribute_computed.keel @@ -24,4 +24,14 @@ model Item { } //expect-error:5:14:E011:model 'Item' has an unrecognised attribute @computed @computed(price * quantity) +} + +model Thing { + fields { + total Decimal @computed(1 + 1) + } + actions { + //expect-error:35:40:ActionInputError:computed fields cannot be used as inputs as they are automatically generated + create createItem() with (total) + } } \ No newline at end of file diff --git a/schema/validation/rules/actions/input_type.go b/schema/validation/rules/actions/input_type.go index 5797ecf41..1c94ab9dc 100644 --- a/schema/validation/rules/actions/input_type.go +++ b/schema/validation/rules/actions/input_type.go @@ -86,6 +86,17 @@ func validateInputType( if query.ResolveInputType(asts, input, model, action) == "" { return unresolvedTypeError(asts, input, model) } + + field := query.ResolveInputField(asts, input, model) + if field != nil && query.FieldHasAttribute(field, parser.AttributeComputed) { + return errorhandling.NewValidationErrorWithDetails( + errorhandling.ActionInputError, + errorhandling.ErrorDetails{ + Message: "computed fields cannot be used as inputs as they are automatically generated", + }, + input, + ) + } } return nil }