Skip to content

Commit

Permalink
fix: computed fields as inputs (#1717)
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza authored Feb 3, 2025
1 parent 7afcd6d commit 1469029
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions node/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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("?")
Expand Down
26 changes: 25 additions & 1 deletion node/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 := `
Expand Down
10 changes: 10 additions & 0 deletions schema/testdata/errors/attribute_computed.keel
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions schema/validation/rules/actions/input_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 1469029

Please sign in to comment.