Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow primary field declarations on one-many #2796

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,19 @@ func finalizeRelations(
}

if !otherColFieldDescription.HasValue() || otherColFieldDescription.Value().Kind.Value().IsArray() {
// Relations only defined on one side of the object are possible, and so if this is one of them
// or if the other side is an array, we need to add the field to the schema (is primary side).
definition.Schema.Fields = append(
definition.Schema.Fields,
client.SchemaFieldDescription{
Name: field.Name,
Kind: field.Kind.Value(),
Typ: cTypeByFieldNameByObjName[definition.Schema.Name][field.Name],
},
)
if _, exists := definition.Schema.GetFieldByName(field.Name); !exists {
// Relations only defined on one side of the object are possible, and so if this is one of them
// or if the other side is an array, we need to add the field to the schema (is primary side)
// if the field has not been explicitly declared by the user.
definition.Schema.Fields = append(
definition.Schema.Fields,
client.SchemaFieldDescription{
Name: field.Name,
Kind: field.Kind.Value(),
Typ: cTypeByFieldNameByObjName[definition.Schema.Name][field.Name],
},
)
}
}

otherIsEmbedded := len(otherColDefinition.Value().Description.Fields) == 0
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ func assertCollectionDescriptions(

if expected.Indexes != nil || len(actual.Indexes) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no indexes)
// This is to save each test action from having to bother declaring an empty slice (if there are no indexes)
require.Equal(s.t, expected.Indexes, actual.Indexes)
}

if expected.Sources != nil || len(actual.Sources) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no sources)
// This is to save each test action from having to bother declaring an empty slice (if there are no sources)
require.Equal(s.t, expected.Sources, actual.Sources)
}

Expand Down
85 changes: 85 additions & 0 deletions tests/integration/schema/one_many_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package schema

import (
"testing"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestSchemaOneMany_Primary(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
name: String
dogs: [Dog]
}
type Dog {
name: String
owner: User @primary
}
`,
ExpectedResults: []client.CollectionDescription{
{
Name: immutable.Some("User"),
Fields: []client.CollectionFieldDescription{
{
Name: "_docID",
},
{
Name: "dogs",
ID: 1,
Kind: immutable.Some[client.FieldKind](client.ObjectArrayKind("Dog")),
RelationName: immutable.Some("dog_user"),
},
{
Name: "name",
ID: 2,
},
},
},
{
Name: immutable.Some("Dog"),
Fields: []client.CollectionFieldDescription{
{
Name: "_docID",
},
{
Name: "name",
ID: 1,
},
{
Name: "owner",
ID: 2,
Kind: immutable.Some[client.FieldKind](client.ObjectKind("User")),
RelationName: immutable.Some("dog_user"),
},
{
Name: "owner_id",
ID: 3,
Kind: immutable.Some[client.FieldKind](client.ScalarKind(client.FieldKind_DocID)),
RelationName: immutable.Some("dog_user"),
},
},
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Loading