diff --git a/internal/request/graphql/schema/collection.go b/internal/request/graphql/schema/collection.go index c4180be0f4..d6ae0854d0 100644 --- a/internal/request/graphql/schema/collection.go +++ b/internal/request/graphql/schema/collection.go @@ -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) diff --git a/tests/integration/schema/one_many_test.go b/tests/integration/schema/one_many_test.go index bab84b3d40..6783a17e7b 100644 --- a/tests/integration/schema/one_many_test.go +++ b/tests/integration/schema/one_many_test.go @@ -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) +}