Skip to content

Commit c39ce1d

Browse files
committed
Moar updates
1 parent 598608c commit c39ce1d

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

graph/patterns/navigation-property.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Microsoft Graph API Design Pattern
77
## Problem
88
--------
99

10-
It is often valuable to represent a relationship between resources in an API. This may be a many-to-one or a one-to-many relationship.
10+
It is often valuable to represent a relationship between resources in an API.
1111

1212
Relationships between resources are often implicitly represented by a property contained in one of the resources that provides a key to a related resource. Usually that information is returned in a representation as an id value and the property is named using a convention that identifies the target type of related resource. e.g. userId
1313

@@ -23,35 +23,70 @@ e.g. /user/{userId}/manager # many-to-one relationship
2323

2424
Additionally, using the OData Expand query parameter, related entities can be transcluded into the primary entity so both can be retrieved in a single round trip.
2525

26+
These relationships can be described in CSDL as follows:
27+
28+
```xml
29+
<EntityType Name="user">
30+
<NavigationProperty Name="manager" Type="user" ContainsTarget="false" >
31+
<NavigationProperty Name="messages" Type="user" ContainsTarget="true" >
32+
</EntityType>
33+
```
34+
2635
## Issues and Considerations
2736
-------------------------
2837

2938
In the current Microsoft Graph implementation, there are some limitations on the use of navigation properties that cross between backend services. These limitations are being eliminated over time, but it will be necessary to ensure support for any particular scenario. [Limitations of the current implementation](https://dev.azure.com/msazure/One/_wiki/wikis/Microsoft%20Graph%20Partners/354352/Cross-workload-navigations?anchor=supported-scenarios) are documented internally.
3039

31-
Navigation properties defined within an entity are not returned by default when retreiving the representation of an entity unless explicity desired by a service.
40+
Navigation properties defined within an entity are not returned by default when retreiving the representation of an entity unless explicity desired by a service. The API can consumer can use the `expand` query parameterm, where supported, to retreive both the source and the target entity of the relationship in a single request.
3241

3342
Implementing support for accessing the "$ref" of a navigation property allows a caller to return just the URL of related resource. e.g. `/user/23/manager/$ref`. This is useful when a client wishes to identify the related resource but doesn't need all of its properties.
3443

3544
## When to Use this Pattern
3645
------------------------
3746

38-
### "has a" relationships
47+
### "Many-to-one" relationships
3948

4049
The use of navigation properties is preferred over including an Id field to reference the related entity in a many-to-one relationship. Id values require a client to make two round trips to retrieve the details of a related entity. With a navigation property a client can retrieve a related entity in a single round trip.
50+
51+
Many-to-one relationships are always non-contained relationships as the lifetime of the target cannot depend on the source.
52+
53+
```xml
54+
<EntityType Name="order">
55+
<NavigationProperty Name="customer" Type="customer" ContainsTarget="false" >
56+
</EntityType>
57+
```
58+
4159

42-
Navigation properties are also useful when clients sometimes want to retrieve both the primary entity and the related entity in a single round trip. The `expand` query parameter makes this possible.
60+
### "Zero-or-one-to-one" relationships
4361

44-
### "parent-child" relationships
62+
These navigation properties can be used as a structural organization mechanism to separate properties of an entity in a way that is similar to how complex types are often used. The primary difference being that the target of the navigation property are not returned by default when the source entity is retreived. The use of the navigation properties over complex properties is preferred when the source and target information comes from different backend APIs.
63+
64+
These relationships must be contained.
65+
66+
```xml
67+
<EntityType Name="invoice">
68+
<NavigationProperty Name="paymentDetails" Type="paymentInfo" ContainsTarget="true" >
69+
</EntityType>
70+
```
71+
72+
### "One-to-many" relationships
4573

4674
Resources that contain a parent Id property in a child resource can utilize a navigation property in the parent resource that is declared as a collection of child resources. If desirable, a parent navigation property can also be created in the child resource to the parent resource. This is usually not necessary as the parent URL is a subset of child resource URL. The main use of this would be when retrieving child resources and choosing to expand properties of the parent resource so that both can be retrieved in a single request.
4775

4876
`/invoice/{invoiceId}/items/{itemId}?expand=parentInvoice(select=invoiceDate,Customer)`
4977

50-
### "child-parent" relationships
78+
```xml
79+
<EntityType Name="invoice">
80+
<NavigationProperty Name="items" Type="invoiceItem" ContainsTarget="true" >
81+
</EntityType>
82+
83+
<EntityType Name="invoiceItem">
84+
<NavigationProperty Name="parentInvoice" Type="invoice" ContainsTarget="false" >
85+
</EntityType>
86+
```
5187

52-
One other use case is when child resources appear in a non-contained collection and there is a desire to access the canonical parent:
88+
One-to-many relationships may be contained or non-contained relations.
5389

54-
`/me/pinnedChannels/{channelId}/team`
5590

5691
## Example
5792
-------

0 commit comments

Comments
 (0)