Skip to content

Commit a09c6d9

Browse files
authored
Merge pull request #363 from microsoft/garethj-msft-improvealtkeypattern
Improved alternate key pattern content.
2 parents bbec4b3 + 97a8eea commit a09c6d9

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

graph/patterns/alternate-key.md

+54-12
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,51 @@
22

33
Microsoft Graph API Design Pattern
44

5-
*The alternate key pattern provides the ability to query for a single, specific resource identifiable through an alternative set of properties that is not its primary key.*
5+
*The alternate key pattern provides the ability to query for a single, specific resource identifiable via one of an alternative set of properties that is not its primary key.*
66

77
## Problem
88

9-
The resources exposed in Microsoft Graph are identified through a primary key, which guarantees uniqueness inside the same resource type. Often though, that same resource can also be uniquely identified by an alternative, more convenient property (or set of properties) that provides a better developer experience.
9+
The resources exposed in Microsoft Graph are identified through a primary key, which guarantees uniqueness inside the same resource collection. Often though, that same resource can also be uniquely identified by an alternative, more convenient property that provides a better developer experience.
1010

11-
Take a look at the `user` resource: while the `id` remains a perfectly valid way to get the resource details, the `mail` address is also a unique property that could be used to identify it.
11+
Take a look at the `user` resource: while the `id` is the typical way to get the resource details, the `mail` address is also a unique property that can be used to identify it.
1212

13-
While it is still possible to use the `$filter` query parameter, such as `GET https://graph.microsoft.com/v1.0/users?$filter=mail eq '[email protected]'`, the returned result is wrapped in an array that needs to be unpacked.
13+
The resource can be accessed using the `$filter` query parameter, such as
14+
15+
```http
16+
GET https://graph.microsoft.com/v1.0/users?$filter=mail eq '[email protected]'
17+
```
18+
However, in this case, the returned result is wrapped in an array that needs to be unpacked. When the uniqueness of the property within the collection implies that only zero or one results can be returned from the call this array provides a suboptimal experience for callers.
1419

1520
## Solution
1621

17-
Resource addressing by using an alternative key can be achieved by using the same parentheses-style convention as the canonical key with one difference: single-part alternate keys MUST specify the key property name to unambiguously determine the alternate key.
22+
Typically resources in Graph are accessed using a simple forward-slash delimited URL pattern (this pattern is sometimes referred to as key-as-segment).
23+
24+
```http
25+
https://graph.microsoft.com/v1.0/users/0 - Retrieves the employee with ID = 0.
26+
```
27+
28+
However, resources can also be accessed using parentheses to delimit the key, like this:
1829

19-
The following is a hypothetical sample:
30+
```http
31+
https://graph.microsoft.com/v1.0/users(0) - Also retrieves the employee with ID = 0.
32+
```
2033

21-
https://graph.microsoft.com/v1.0/users(0) - Retrieves the employee with ID = 0.
34+
Resource addressing by using an alternative key can be achieved by using this same parentheses-style convention with one difference: alternate keys MUST specify the key property name to unambiguously determine the alternate key, like this:
2235

36+
```http
2337
https://graph.microsoft.com/v1.0/users(email='[email protected]') Retrieves the employee with the email matching `[email protected]`.
38+
```
39+
40+
In the same way as requesting a resource via the canonical key, if a resource cannot be located that matches the alternate key, then a 404 must be returned.
41+
42+
> **Note:** When requesting a resource via alternate keys, the simple slash-delimited URL style does not work.
43+
44+
> **Note:** Do not use multi-part alternate keys. Feedback has been that customers find multi-part keys confusing.
45+
> Either create a composite single-part surrogate key property or fall back to logical operations in a $filter clause.
2446
2547
## When to use this pattern
2648

27-
This pattern works and makes sense when the alternate key is good enough to identify a single resource and provides a useful alternative to the client.
49+
Use this pattern when your resource type has other keys than its canonical key which uniquely identify a single resource.
2850

2951
## Example
3052

@@ -82,6 +104,7 @@ Declare `mail` and `ssn` as alternate keys on an entity:
82104
"mobilePhone": "+1 425 555 0109",
83105
"officeLocation": "18/2111",
84106
"preferredLanguage": "en-US",
107+
"ssn": "123-45-6789",
85108
"surname": "Vance",
86109
"userPrincipalName": "[email protected]",
87110
"id": "1a89ade6-9f59-4fea-a139-23f84e3aef66"
@@ -94,13 +117,12 @@ Declare `mail` and `ssn` as alternate keys on an entity:
94117
95118
```http
96119
GET https://graph.microsoft.com/v1.0/users/1a89ade6-9f59-4fea-a139-23f84e3aef66
120+
GET https://graph.microsoft.com/v1.0/users(1a89ade6-9f59-4fea-a139-23f84e3aef66)
97121
GET https://graph.microsoft.com/v1.0/users(ssn='123-45-6789')
98122
GET https://graph.microsoft.com/v1.0/users(mail='[email protected]')
99123
```
100-
101-
> **Note:** When requesting a resource through its primary key, you might prefer to use `key-as-segment` (as shown earlier). Also, `key-as-segment` does not work for alternate keys.
102-
103-
All three yield the same response:
124+
125+
All four yield the same response:
104126
105127
```json
106128
{
@@ -123,4 +145,24 @@ Declare `mail` and `ssn` as alternate keys on an entity:
123145
GET https://graph.microsoft.com/v1.0/users(name='Bob')
124146
125147
400 Bad Request
148+
{
149+
"error" : {
150+
"code" : "400",
151+
"message": "'name' is not a valid alternate key for the resource type 'user'."
152+
}
153+
}
154+
```
155+
156+
4. Request a resource where the alternate key property does not exist on any resource in the colleciton:
157+
158+
```http
159+
GET https://graph.microsoft.com/v1.0/users(email='[email protected]')
160+
161+
404 Not Found
162+
{
163+
"error" : {
164+
"code" : "404",
165+
"message": "No user with the the specified 'email' could be found."
166+
}
167+
}
126168
```

0 commit comments

Comments
 (0)