You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: graph/patterns/alternate-key.md
+54-12
Original file line number
Diff line number
Diff line change
@@ -2,29 +2,51 @@
2
2
3
3
Microsoft Graph API Design Pattern
4
4
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.*
6
6
7
7
## Problem
8
8
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.
10
10
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.
12
12
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.
14
19
15
20
## Solution
16
21
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:
18
29
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
+
```
20
33
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:
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.
24
46
25
47
## When to use this pattern
26
48
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.
28
50
29
51
## Example
30
52
@@ -82,6 +104,7 @@ Declare `mail` and `ssn` as alternate keys on an entity:
@@ -94,13 +117,12 @@ Declare `mail` and `ssn` as alternate keys on an entity:
94
117
95
118
```http
96
119
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)
97
121
GET https://graph.microsoft.com/v1.0/users(ssn='123-45-6789')
98
122
GET https://graph.microsoft.com/v1.0/users(mail='[email protected]')
99
123
```
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:
104
126
105
127
```json
106
128
{
@@ -123,4 +145,24 @@ Declare `mail` and `ssn` as alternate keys on an entity:
123
145
GET https://graph.microsoft.com/v1.0/users(name='Bob')
124
146
125
147
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."
0 commit comments