Skip to main content

Bidirectional relationships

The data model below has a directional reference from Equipment.documents to the Document type.

type Equipment @view(space: "core") {
name: String
documents: [Document]
}

interface Document {
name: String
}

In this example, the relationhips are only queryable via Equipment.documents, and not from Document. This is called a 1 directional relationship.

Adding a new Document.equipments: [Equipment] doesn't indicate that they reference each other. For example:

type A {
prop1: [B]
prop2: [B]
}

interface B {
prop3: [A] # which one does this point to?
}

It is not possible to tell where B.prop3 links to. Therefore, we would require you to load relationships into B.prop3 in the opposite direction, even if both A.prop1 and A.prop2 already contain data. The relationships are all independent of each other, not the inverse of another relationship.

This can be useful for some cases, but in general you should avoid this behavior because of the high data maintenance cost.

Use @relation to specify inverse relations

To specify inverse relations, you can use @relation to define what type.property a relation is directed.

By default, all relationships have an OUTWARDS direction. To define that a field aims in an inwards direction, you need to specify that it's going INWARDS to another type.property, for example:

type Equipment @view(space: "core") {
name: String
documents: [Document]
}

interface Document {
name: String
equipments: [Equipment]
@relation(
type: { space: "core", externalId: "Equipment.documents" }
direction: INWARDS
)
}

Explanation

Whenever you introduce a relationship, a default @relation is added, The type is pointed at the same space, the externalId is always TypeName.field, and the direction is OUTWARDS.

In the example above, a default @relation(type: { space: "core", externalId: "Equipment.documents" }, direction: OUTWARDS) is created for Equipment.documents. This is the explicit definition happening in the background:

type Equipment @view(space: "core") {
name: String
documents: [Document]
@relation(
type: { space: "core", externalId: "Equipment.documents" }
direction: OUTWARDS
)
}