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 d107809
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 58 deletions.
10 changes: 10 additions & 0 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ func (doc *Document) Set(field string, value any) error {
if !exists {
return NewErrFieldNotExist(field)
}

if fd.Kind == FieldKind_DocID {
objFieldName := strings.TrimSuffix(field, request.RelatedObjectID)
ofd, exists := doc.collectionDefinition.GetFieldByName(objFieldName)
if exists && !ofd.IsPrimaryRelation {
return NewErrCannotSetRelationFromSecondarySide(field)
}

}

Check failure on line 703 in client/document.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)

if fd.Kind.IsObject() && !fd.Kind.IsArray() {
if !strings.HasSuffix(field, request.RelatedObjectID) {
field = field + request.RelatedObjectID
Expand Down
5 changes: 5 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 @@ -190,3 +191,7 @@ func ReviveError(message string) error {
return fmt.Errorf("%s", message)
}
}

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

if field.Kind == client.FieldKind_DocID {
objFieldName := strings.TrimSuffix(field.Name, request.RelatedObjectID)
ofd, exists := collection.GetFieldByName(objFieldName)
if exists && !ofd.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
49 changes: 0 additions & 49 deletions tests/predefined/gen_predefined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,55 +233,6 @@ func TestGeneratePredefinedFromSchema_OneToOneToOnePrimary(t *testing.T) {
}
}

func TestGeneratePredefinedFromSchema_TwoPrimaryToOneMiddle(t *testing.T) {
schema := `
type User {
name: String
device: Device
}
type Device {
model: String
owner: User @primary
specs: Specs @primary
}
type Specs {
OS: String
device: Device
}`

docs, err := CreateFromSDL(schema, DocsList{
ColName: "User",
Docs: []map[string]any{
{
"name": "John",
"device": map[string]any{
"model": "iPhone",
"specs": map[string]any{
"OS": "iOS",
},
},
},
},
})
assert.NoError(t, err)

colDefMap, err := gen.ParseSDL(schema)
require.NoError(t, err)

specsDoc := mustAddDocIDToDoc(map[string]any{"OS": "iOS"}, colDefMap["Specs"])
userDoc := mustAddDocIDToDoc(map[string]any{"name": "John"}, colDefMap["User"])
deviceDoc := mustAddDocIDToDoc(map[string]any{
"model": "iPhone",
"specs_id": specsDoc[request.DocIDFieldName],
"owner_id": userDoc[request.DocIDFieldName],
}, colDefMap["Device"])

errorMsg := assertDocs([]map[string]any{userDoc, deviceDoc, specsDoc}, docs)
if errorMsg != "" {
t.Error(errorMsg)
}
}

func TestGeneratePredefinedFromSchema_OneToTwoPrimary(t *testing.T) {
schema := `
type User {
Expand Down

0 comments on commit d107809

Please sign in to comment.