|
1 |
| -# Alternate Key Pattern |
| 1 | +# Alternate key |
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 through an alternative set of properties that is not its primary key.* |
6 | 6 |
|
7 | 7 | ## Problem
|
8 | 8 |
|
9 |
| ---- |
| 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. |
10 | 10 |
|
11 |
| -The resources exposed in 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. |
| 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. |
12 | 12 |
|
13 |
| -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 an unique property that could be used to identify it. |
14 |
| - |
15 |
| -While it is still possible to use the `$filter` query parameter, such as |
16 |
| - |
17 |
| -`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 | +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. |
18 | 14 |
|
19 | 15 | ## Solution
|
20 | 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 | 18 |
|
23 |
| -Resource addressing via an alternative key can be achieved using the same parentheses-style convention as for the canonical key, with one difference: single-part alternate keys MUST specify the key property name to unambiguously determine the alternate key. (Note: this is a hypothetical sample) |
| 19 | +The following is a hypothetical sample: |
24 | 20 |
|
25 |
| -https://graph.microsoft.com/v1.0/users(0) - Retrieves the employee with ID = 0 |
26 |
| -https://graph.microsoft.com/v1.0/users(email=' [email protected]') Retrieves the employee with the email matching `[email protected]` |
| 21 | +https://graph.microsoft.com/v1.0/users(0) - Retrieves the employee with ID = 0. |
27 | 22 |
|
28 |
| -## When to Use this Pattern |
| 23 | +https://graph.microsoft.com/v1.0/users(email='[email protected]') Retrieves the employee with the email matching `[email protected]`. |
29 | 24 |
|
30 |
| ---- |
| 25 | +## When to use this pattern |
31 | 26 |
|
32 |
| -This pattern works and makes sense when the alternate key is good enough to identify a single resource and provides an useful alternative to the client. |
| 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. |
33 | 28 |
|
34 | 29 | ## Example
|
35 | 30 |
|
36 |
| ---- |
37 |
| - |
38 |
| -The same user identified via the alternate key SSN, the canonical (primary) key ID using the non-canonical long form with specified key property name, and the canonical short form without key property name |
| 31 | +The same user is identified via the alternate key SSN, the canonical (primary) key ID using the non-canonical long form with a specified key property name, and the canonical short form without a key property name. |
39 | 32 |
|
40 | 33 | Declare `mail` and `ssn` as alternate keys on an entity:
|
41 | 34 |
|
@@ -75,59 +68,59 @@ Declare `mail` and `ssn` as alternate keys on an entity:
|
75 | 68 |
|
76 | 69 | 1. Get a specific resource through `$filter`:
|
77 | 70 |
|
78 |
| -```http |
79 |
| -GET https://graph.microsoft.com/v1.0/users/?$filter=ssn eq '123-45-6789' |
80 |
| -``` |
| 71 | + ```http |
| 72 | + GET https://graph.microsoft.com/v1.0/users/?$filter=ssn eq '123-45-6789' |
| 73 | + ``` |
| 74 | + |
| 75 | + ```json |
| 76 | + { |
| 77 | + "value": [ |
| 78 | + { |
| 79 | + "givenName": "Bob", |
| 80 | + "jobTitle": "Retail Manager", |
| 81 | + |
| 82 | + "mobilePhone": "+1 425 555 0109", |
| 83 | + "officeLocation": "18/2111", |
| 84 | + "preferredLanguage": "en-US", |
| 85 | + "surname": "Vance", |
| 86 | + "userPrincipalName": "[email protected]", |
| 87 | + "id": "1a89ade6-9f59-4fea-a139-23f84e3aef66" |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + ``` |
| 92 | +
|
| 93 | +2. Get a specific resource either through its primary key or through the two alternate keys: |
| 94 | +
|
| 95 | + ```http |
| 96 | + GET https://graph.microsoft.com/v1.0/users/1a89ade6-9f59-4fea-a139-23f84e3aef66 |
| 97 | + GET https://graph.microsoft.com/v1.0/users(ssn='123-45-6789') |
| 98 | + GET https://graph.microsoft.com/v1.0/users(mail='[email protected]') |
| 99 | + ``` |
81 | 100 |
|
82 |
| -```json |
83 |
| -{ |
84 |
| - "value": [ |
| 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: |
| 104 | + |
| 105 | + ```json |
85 | 106 | {
|
86 | 107 | "givenName": "Bob",
|
87 | 108 | "jobTitle": "Retail Manager",
|
88 | 109 |
|
89 | 110 | "mobilePhone": "+1 425 555 0109",
|
90 | 111 | "officeLocation": "18/2111",
|
91 | 112 | "preferredLanguage": "en-US",
|
| 113 | + "ssn": "123-45-6789", |
92 | 114 | "surname": "Vance",
|
93 | 115 | "userPrincipalName": "[email protected]",
|
94 | 116 | "id": "1a89ade6-9f59-4fea-a139-23f84e3aef66"
|
95 | 117 | }
|
96 |
| - ] |
97 |
| -} |
98 |
| -``` |
99 |
| - |
100 |
| -2. Get a specific resource either through its primary key, or through the two alternate keys: |
101 |
| - |
102 |
| -```http |
103 |
| -GET https://graph.microsoft.com/v1.0/users/1a89ade6-9f59-4fea-a139-23f84e3aef66 |
104 |
| -GET https://graph.microsoft.com/v1.0/users(ssn='123-45-6789') |
105 |
| -GET https://graph.microsoft.com/v1.0/users(mail='[email protected]') |
106 |
| -``` |
| 118 | + ``` |
107 | 119 |
|
108 |
| -**NOTE:** When requesting a resource through its primary key you might want to prefer to use key-as-segment (as shown above). Also, the key-as-segment does not work for alternate keys. |
109 |
| - |
110 |
| -All of the 3 will yield the sare response: |
111 |
| - |
112 |
| -```json |
113 |
| -{ |
114 |
| - "givenName": "Bob", |
115 |
| - "jobTitle": "Retail Manager", |
116 |
| - |
117 |
| - "mobilePhone": "+1 425 555 0109", |
118 |
| - "officeLocation": "18/2111", |
119 |
| - "preferredLanguage": "en-US", |
120 |
| - "ssn": "123-45-6789", |
121 |
| - "surname": "Vance", |
122 |
| - "userPrincipalName": "[email protected]", |
123 |
| - "id": "1a89ade6-9f59-4fea-a139-23f84e3aef66" |
124 |
| -} |
125 |
| -``` |
126 |
| - |
127 |
| -3. Requesting a resource for an unsupported alternate key property |
| 120 | +3. Request a resource for an unsupported alternate key property: |
128 | 121 |
|
129 |
| -```http |
130 |
| -GET https://graph.microsoft.com/v1.0/users(name='Bob') |
131 |
| -
|
132 |
| -400 Bad Request |
133 |
| -``` |
| 122 | + ```http |
| 123 | + GET https://graph.microsoft.com/v1.0/users(name='Bob') |
| 124 | + |
| 125 | + 400 Bad Request |
| 126 | + ``` |
0 commit comments