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: Support one-many self joins without primary directive #2799

Merged
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
14 changes: 6 additions & 8 deletions internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,15 +667,13 @@ func finalizeRelations(
continue
}

var otherColFieldDescription immutable.Option[client.CollectionFieldDescription]
for _, otherField := range otherColDefinition.Value().Description.Fields {
if otherField.RelationName.Value() == field.RelationName.Value() {
otherColFieldDescription = immutable.Some(otherField)
break
}
}
otherColFieldDescription, hasOtherColFieldDescription := otherColDefinition.Value().Description.GetFieldByRelation(
field.RelationName.Value(),
definition.GetName(),
field.Name,
)

if !otherColFieldDescription.HasValue() || otherColFieldDescription.Value().Kind.Value().IsArray() {
if !hasOtherColFieldDescription || otherColFieldDescription.Kind.Value().IsArray() {
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)
Expand Down
90 changes: 90 additions & 0 deletions tests/integration/schema/one_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,93 @@ func TestSchemaOneMany_Primary(t *testing.T) {

testUtils.ExecuteTestCase(t, test)
}

func TestSchemaOneMany_SelfReferenceOneFieldLexographicallyFirst(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
a: User
b: [User]
}
`,
ExpectedResults: []client.CollectionDescription{
{
Name: immutable.Some("User"),
Fields: []client.CollectionFieldDescription{
{
Name: "_docID",
},
{
Name: "a",
ID: 1,
Kind: immutable.Some[client.FieldKind](client.ObjectKind("User")),
RelationName: immutable.Some("user_user"),
},
{
Name: "a_id",
ID: 2,
Kind: immutable.Some[client.FieldKind](client.ScalarKind(client.FieldKind_DocID)),
RelationName: immutable.Some("user_user"),
},
{
Name: "b",
ID: 3,
Kind: immutable.Some[client.FieldKind](client.ObjectArrayKind("User")),
RelationName: immutable.Some("user_user"),
},
},
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestSchemaOneMany_SelfReferenceManyFieldLexographicallyFirst(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
b: User
a: [User]
}
`,
ExpectedResults: []client.CollectionDescription{
{
Name: immutable.Some("User"),
Fields: []client.CollectionFieldDescription{
{
Name: "_docID",
},
{
Name: "a",
ID: 1,
Kind: immutable.Some[client.FieldKind](client.ObjectArrayKind("User")),
RelationName: immutable.Some("user_user"),
},
{
Name: "b",
ID: 2,
Kind: immutable.Some[client.FieldKind](client.ObjectKind("User")),
RelationName: immutable.Some("user_user"),
},
{
Name: "b_id",
ID: 3,
Kind: immutable.Some[client.FieldKind](client.ScalarKind(client.FieldKind_DocID)),
RelationName: immutable.Some("user_user"),
},
},
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Loading