|
| 1 | +# E. Appendix: Examples |
| 2 | + |
| 3 | +## Incremental Delivery Examples |
| 4 | + |
| 5 | +### Example 1 - A query containing both defer and stream |
| 6 | + |
| 7 | +```graphql example |
| 8 | +query { |
| 9 | + person(id: "cGVvcGxlOjE=") { |
| 10 | + ...HomeWorldFragment @defer(label: "homeWorldDefer") |
| 11 | + name |
| 12 | + films @stream(initialCount: 1, label: "filmsStream") { |
| 13 | + title |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | +fragment HomeWorldFragment on Person { |
| 18 | + homeWorld { |
| 19 | + name |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +The response to this request will be an _incremental stream_ consisting of an |
| 25 | +_initial incremental stream result_ followed by one or more _incremental stream |
| 26 | +update result_. |
| 27 | + |
| 28 | +The _initial incremental stream result_ has: |
| 29 | + |
| 30 | +- a {"data"} entry containing the results of the GraphQL operation except for |
| 31 | + the `@defer` and `@stream` selections; |
| 32 | +- a {"pending"} entry containing two _incremental pending notices_, one for the |
| 33 | + `@defer` selection and for the the `@stream` selection, indicating that these |
| 34 | + results will be delivered in a later _incremental stream update result_; |
| 35 | +- a {"hasNext"} entry with the value {true}, indicating that the response is not |
| 36 | + yet complete. |
| 37 | + |
| 38 | +If an error were to occur, it would also have an {"errors"} entry; but not in |
| 39 | +this example. |
| 40 | + |
| 41 | +```json example |
| 42 | +{ |
| 43 | + "data": { |
| 44 | + "person": { |
| 45 | + "name": "Luke Skywalker", |
| 46 | + "films": [{ "title": "A New Hope" }] |
| 47 | + } |
| 48 | + }, |
| 49 | + "pending": [ |
| 50 | + { "id": "0", "path": ["person"], "label": "homeWorldDefer" }, |
| 51 | + { "id": "1", "path": ["person", "films"], "label": "filmsStream" } |
| 52 | + ], |
| 53 | + "hasNext": true |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Depending on the behavior of the backend and the time at which the deferred and |
| 58 | +streamed resources resolve, the stream may produce results in different orders. |
| 59 | +In this example, our first _incremental stream update result_ contains the |
| 60 | +deferred data and the first streamed list item. There is one _incremental |
| 61 | +completion notice_, indicating that the deferred data has been completely |
| 62 | +delivered. |
| 63 | + |
| 64 | +```json example |
| 65 | +{ |
| 66 | + "incremental": [ |
| 67 | + { |
| 68 | + "id": "0", |
| 69 | + "data": { "homeWorld": { "name": "Tatooine" } } |
| 70 | + }, |
| 71 | + { |
| 72 | + "id": "1", |
| 73 | + "items": [{ "title": "The Empire Strikes Back" }] |
| 74 | + } |
| 75 | + ], |
| 76 | + "completed": [ |
| 77 | + {"id": "0"} |
| 78 | + ] |
| 79 | + "hasNext": true |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The second _incremental stream update result_ contains the final stream results. |
| 84 | +In this example, the underlying iterator does not close synchronously so |
| 85 | +{"hasNext"} is set to {true}. If this iterator did close synchronously, |
| 86 | +{"hasNext"} could be set to {false} and make this the final incremental stream |
| 87 | +update result. |
| 88 | + |
| 89 | +```json example |
| 90 | +{ |
| 91 | + "incremental": [ |
| 92 | + { |
| 93 | + "id": "1", |
| 94 | + "items": [{ "title": "Return of the Jedi" }] |
| 95 | + } |
| 96 | + ], |
| 97 | + "hasNext": true |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +When the underlying iterator of the `films` field closes there is no more data |
| 102 | +to deliver, so the third and final _incremental stream update result_ sets |
| 103 | +{"hasNext"} to {false} to indicate the end of the _incremental stream_. |
| 104 | + |
| 105 | +```json example |
| 106 | +{ |
| 107 | + "hasNext": false |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### Example 2 - A query containing overlapping defers |
| 112 | + |
| 113 | +```graphql example |
| 114 | +query { |
| 115 | + person(id: "cGVvcGxlOjE=") { |
| 116 | + ...HomeWorldFragment @defer(label: "homeWorldDefer") |
| 117 | + ...NameAndHomeWorldFragment @defer(label: "nameAndWorld") |
| 118 | + firstName |
| 119 | + } |
| 120 | +} |
| 121 | +fragment HomeWorldFragment on Person { |
| 122 | + homeWorld { |
| 123 | + name |
| 124 | + terrain |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fragment NameAndHomeWorldFragment on Person { |
| 129 | + firstName |
| 130 | + lastName |
| 131 | + homeWorld { |
| 132 | + name |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +In this example the response is an _incremental stream_ of the following |
| 138 | +results. |
| 139 | + |
| 140 | +The _initial incremental stream result_ contains the results of the `firstName` |
| 141 | +field. Even though it is also present in the `HomeWorldFragment`, it must be |
| 142 | +returned in the initial incremental stream result because it is also defined |
| 143 | +outside of any fragments with the `@defer` directive. Additionally, there are |
| 144 | +two _incremental pending notices_ indicating that results for both `@defer`s in |
| 145 | +the query will be delivered in later _incremental stream update result_. |
| 146 | + |
| 147 | +```json example |
| 148 | +{ |
| 149 | + "data": { |
| 150 | + "person": { |
| 151 | + "firstName": "Luke" |
| 152 | + } |
| 153 | + }, |
| 154 | + "pending": [ |
| 155 | + { "id": "0", "path": ["person"], "label": "homeWorldDefer" }, |
| 156 | + { "id": "1", "path": ["person"], "label": "nameAndWorld" } |
| 157 | + ], |
| 158 | + "hasNext": true |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +In this example, the first _incremental stream update result_ contains the |
| 163 | +deferred data from `HomeWorldFragment`. There is one _incremental completion |
| 164 | +notice_, indicating that `HomeWorldFragment` has been completely delivered. |
| 165 | +Because the `homeWorld` field is present in two separate `@defer`s, it is |
| 166 | +separated into its own _incremental result_. In this example, this incremental |
| 167 | +result contains the id `"0"`, but since the `name` field was included in both |
| 168 | +`HomeWorldFragment` and `NameAndHomeWorldFragment`, an id of `"1"` would also be |
| 169 | +a valid response. |
| 170 | + |
| 171 | +The second _incremental result_ in this _incremental stream update result_ |
| 172 | +contains the data for the `terrain` field. This _incremental result_ contains a |
| 173 | +{"subPath"} entry to indicate to clients that the _response position_ of this |
| 174 | +result can be determined by concatenating: the path from the _incremental |
| 175 | +pending notice_ for id `"0"`, and the value of this {"subPath"} entry. |
| 176 | + |
| 177 | +```json example |
| 178 | +{ |
| 179 | + "incremental": [ |
| 180 | + { |
| 181 | + "id": "0", |
| 182 | + "data": { "homeWorld": { "name": "Tatooine" } } |
| 183 | + }, |
| 184 | + { |
| 185 | + "id": "0", |
| 186 | + "subPath": ["homeWorld"], |
| 187 | + "data": { "terrain": "desert" } |
| 188 | + } |
| 189 | + ], |
| 190 | + "completed": [{ "id": "0" }], |
| 191 | + "hasNext": true |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +The second _incremental stream update result_ contains the remaining data from |
| 196 | +the `NameAndHomeWorldFragment`. `lastName` is the only remaining field from this |
| 197 | +selection that has not been delivered in a previous result. With this field now |
| 198 | +delivered, clients are informed that the `NameAndHomeWorldFragment` has been |
| 199 | +completed by the presence of the associated _incremental completion notice_. |
| 200 | +Additionally, {"hasNext"} is set to {false} indicating the end of the |
| 201 | +_incremental stream_. |
| 202 | + |
| 203 | +This example demonstrates that it is necessary for clients to process the entire |
| 204 | +incremental stream, as both the initial data and previous incremental results |
| 205 | +(with a potentially different value for {"id"}) may be required to complete a |
| 206 | +deferred fragment. |
| 207 | + |
| 208 | +```json example |
| 209 | +{ |
| 210 | + "incremental": [ |
| 211 | + { |
| 212 | + "id": "1", |
| 213 | + "data": { "lastName": "Skywalker" } |
| 214 | + } |
| 215 | + ], |
| 216 | + "completed": [{ "id": "1" }], |
| 217 | + "hasNext": false |
| 218 | +} |
| 219 | +``` |
0 commit comments