Skip to content

Commit

Permalink
Get correct field when finalizing relations
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 28, 2024
1 parent 348a2b9 commit 4447999
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 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
29 changes: 28 additions & 1 deletion tests/integration/schema/one_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,34 @@ func TestSchemaOneMany_SelfReferenceOneFieldLexographicallyFirst(t *testing.T) {
b: [User]
}
`,
ExpectedError: "relation missing field. Object: User, RelationName: user_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"),
},
},
},
},
},
},
}
Expand Down

0 comments on commit 4447999

Please sign in to comment.