Skip to content

Commit

Permalink
WIP - Prevent mutations from secondary side of relation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 10, 2024
1 parent bc68f57 commit eda43d3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions client/collection_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (col CollectionDescription) GetFieldByRelation(
otherCollectionName string,
otherFieldName string,
) (CollectionFieldDescription, bool) {
if relationName == "" {
return CollectionFieldDescription{}, false
}

for _, field := range col.Fields {
if field.RelationName.Value() == relationName &&
!(col.Name.Value() == otherCollectionName && otherFieldName == field.Name) &&
Expand Down
2 changes: 1 addition & 1 deletion client/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func NewFieldDefinition(local CollectionFieldDescription, global SchemaFieldDesc
Kind: kind,
RelationName: local.RelationName.Value(),
Typ: global.Typ,
IsPrimaryRelation: kind.IsObject() && !kind.IsArray(),
IsPrimaryRelation: local.RelationName.HasValue() && !kind.IsArray(),
DefaultValue: local.DefaultValue,
}
}
Expand Down
5 changes: 5 additions & 0 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ func (doc *Document) Set(field string, value any) error {
if !exists {
return NewErrFieldNotExist(field)
}

if fd.RelationName != "" && !fd.IsPrimaryRelation {
return NewErrCannotSetRelationFromSecondarySide(field)
}

if fd.Kind.IsObject() && !fd.Kind.IsArray() {
if !strings.HasSuffix(field, request.RelatedObjectID) {
field = field + request.RelatedObjectID
Expand Down
6 changes: 6 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
errCanNotTurnNormalValueIntoArray string = "can not turn normal value into array"
errCanNotMakeNormalNilFromFieldKind string = "can not make normal nil from field kind"
errFailedToParseKind string = "failed to parse kind"
errCannotSetRelationFromSecondarySide string = "cannot set relation from secondary side"
)

// Errors returnable from this package.
Expand Down Expand Up @@ -61,6 +62,7 @@ var (
ErrCanNotMakeNormalNilFromFieldKind = errors.New(errCanNotMakeNormalNilFromFieldKind)
ErrCollectionNotFound = errors.New(errCollectionNotFound)
ErrFailedToParseKind = errors.New(errFailedToParseKind)
ErrCannotSetRelationFromSecondarySide = errors.New(errCannotSetRelationFromSecondarySide)
)

// NewErrFieldNotExist returns an error indicating that the given field does not exist.
Expand Down Expand Up @@ -190,3 +192,7 @@ func ReviveError(message string) error {
return fmt.Errorf("%s", message)
}
}

func NewErrCannotSetRelationFromSecondarySide(name string) error {
return errors.New(errCannotSetRelationFromSecondarySide, errors.NewKV("Name", name))
}
4 changes: 4 additions & 0 deletions internal/request/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ func (g *Generator) buildMutationInputTypes(collections []client.CollectionDefin
continue
}

if field.RelationName != "" && !field.IsPrimaryRelation {
continue
}

var ttype gql.Type
if field.Kind.IsObject() {
if field.Kind.IsArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromPrimarySide(t *testing.T) {
}

func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) {
author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c"
author2ID := "bae-c058cfd4-259f-5b08-975d-106f13a143d5"

test := testUtils.TestCase{
Expand All @@ -280,13 +279,9 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T)
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: fmt.Sprintf(
`{
"name": "Painted House",
"author_id": "%s"
}`,
author1ID,
),
Doc: `{
"name": "Painted House"
}`,
},
testUtils.UpdateDoc{
CollectionID: 0,
Expand All @@ -297,7 +292,7 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T)
}`,
author2ID,
),
ExpectedError: "target document is already linked to another document.",
ExpectedError: "cannot set relation from secondary side",
},
},
}
Expand Down

0 comments on commit eda43d3

Please sign in to comment.