Skip to content

Commit

Permalink
fix: Support one-many self joins without primary directive (sourcenet…
Browse files Browse the repository at this point in the history
…work#2799)

## Relevant issue(s)

Resolves sourcenetwork#2620

## Description

Support one-many self joins without primary directive.
  • Loading branch information
AndrewSisley authored Jun 28, 2024
1 parent 1f9c3ee commit aaaa9d9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
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)
}

0 comments on commit aaaa9d9

Please sign in to comment.