|
| 1 | +# `@cacheControl` Examples |
| 2 | + |
| 3 | +## Scalar Samples |
| 4 | + |
| 5 | +For scalar types, fields should always inherit from their parent type. However, you can override the parent value at the field level for individual fields. |
| 6 | + |
| 7 | +### Example 1 |
| 8 | + |
| 9 | +In this example, `@cacheControl` is applied to the `Author` type. All fields within the `Author` type are scalars, so they will automatically inherit the max age of 3600 from the `Author` type. |
| 10 | + |
| 11 | +```graphql |
| 12 | +type Author `@cacheControl`(maxAge: 3600) { |
| 13 | + id: ID! |
| 14 | + firstName: String! |
| 15 | + lastName: String! |
| 16 | + bookCount: Int! |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +### Example 2 |
| 21 | + |
| 22 | +In this example, `@cacheControl` is applied to the `Author` type. All fields within the `Author` type are scalars, so they will automatically inherit the max age of 3600 from the `Author` type. However, the `bookCount` field overrides this and gets a `maxAge` of 300. |
| 23 | + |
| 24 | +```graphql |
| 25 | +type Author `@cacheControl`(maxAge: 3600) { |
| 26 | + id: ID! |
| 27 | + firstName: String! |
| 28 | + lastName: String! |
| 29 | + bookCount: Int! `@cacheControl`(maxAge: 300) |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Example 3 |
| 34 | + |
| 35 | +In this example only the `bookCount` field has a `maxAge` applied, the `Author` type and all other fields within the type will receive the default max age value. |
| 36 | + |
| 37 | +```graphql |
| 38 | +type Author { |
| 39 | + id: ID! |
| 40 | + firstName: String! |
| 41 | + lastName: String! |
| 42 | + bookCount: Int! `@cacheControl`(maxAge: 300) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Composite Type Examples |
| 47 | + |
| 48 | +### Example 1 |
| 49 | + |
| 50 | +In this example the `author` field on `Book` has a `maxAge` of 3600 which is set on the `Author` type. |
| 51 | + |
| 52 | +```graphql |
| 53 | +type Author `@cacheControl`(maxAge: 3600) { |
| 54 | + id: ID! |
| 55 | + firstName: String! |
| 56 | + lastName: String! |
| 57 | + bookCount: Int! |
| 58 | +} |
| 59 | + |
| 60 | +type Book { |
| 61 | + id: ID! |
| 62 | + title: String! |
| 63 | + author: Author! # This field uses the max age from the Author type above |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Example 2 |
| 68 | + |
| 69 | +In this example the `Author` type has a `maxAge` of 3600, however the `author` field on `Book` overrides this with a `maxAge` of 600. |
| 70 | + |
| 71 | +```graphql |
| 72 | +type Author `@cacheControl`(maxAge: 3600) { |
| 73 | + id: ID! |
| 74 | + firstName: String! |
| 75 | + lastName: String! |
| 76 | + bookCount: Int! |
| 77 | +} |
| 78 | + |
| 79 | +type Book { |
| 80 | + id: ID! |
| 81 | + title: String! |
| 82 | + author: Author! `@cacheControl`(maxAge: 600) |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Example 3 |
| 87 | + |
| 88 | +In this example no `maxAge` is set for the `Author` type, the `author` field on `Book` is setting its `maxAge` to 600. |
| 89 | + |
| 90 | +```graphql |
| 91 | +type Author { |
| 92 | + id: ID! |
| 93 | + firstName: String! |
| 94 | + lastName: String! |
| 95 | + bookCount: Int! |
| 96 | +} |
| 97 | + |
| 98 | +type Book { |
| 99 | + id: ID! |
| 100 | + title: String! |
| 101 | + author: Author! `@cacheControl`(maxAge: 600) |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Example 4 |
| 106 | + |
| 107 | +In this example there is no `maxAge` set on the `Author` type so it would have a default `maxAge` of 0. It does not automatically inherit from the `Book` type. |
| 108 | + |
| 109 | +```graphql |
| 110 | +type Author { |
| 111 | + id: ID! |
| 112 | + firstName: String! |
| 113 | + lastName: String! |
| 114 | + bookCount: Int! |
| 115 | +} |
| 116 | + |
| 117 | +type Book `@cacheControl`(maxAge: 3600) { |
| 118 | + id: ID! |
| 119 | + title: String! |
| 120 | + author: Author! |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Example 5 |
| 125 | + |
| 126 | +In this example the `inheritMaxAge` is used on the `author` field of the `Book` type so that it inherits the `maxAge` of `Book` instead of using the value set on the `Author` type. |
| 127 | + |
| 128 | +```graphql |
| 129 | +type Author `@cacheControl`(maxAge: 3600) { |
| 130 | + id: ID! |
| 131 | + firstName: String! |
| 132 | + lastName: String! |
| 133 | + bookCount: Int! |
| 134 | +} |
| 135 | + |
| 136 | +type Book `@cacheControl`(maxAge: 1800) { |
| 137 | + id: ID! |
| 138 | + title: String! |
| 139 | + author: Author! `@cacheControl`(inheritMaxAge: true) #uses 1800 instead of 3600 |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### Example 6 |
| 144 | + |
| 145 | +In this example `inheritMaxAge` is applied to the `Author` type, so by default it has a `maxAge` of 0, but since `Book` has a `maxAge` of 1800 that is inherited by the `Author` type. |
| 146 | + |
| 147 | +```graphql |
| 148 | +type Author `@cacheControl`(inheritMaxAge: true) { |
| 149 | + id: ID! |
| 150 | + firstName: String! |
| 151 | + lastName: String! |
| 152 | + bookCount: Int! |
| 153 | +} |
| 154 | + |
| 155 | +type Book `@cacheControl`(maxAge: 1800) { |
| 156 | + id: ID! |
| 157 | + title: String! |
| 158 | + author: Author! # uses 1800 because of inheritMaxAge on Author |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Operation Override Examples |
| 163 | + |
| 164 | +Example 1 |
| 165 | + |
| 166 | +This example shows how you can override `maxAge` values at the operation level. |
| 167 | + |
| 168 | +``` |
| 169 | +# Schema |
| 170 | +
|
| 171 | +type Query { |
| 172 | + authors: [Author!]! |
| 173 | +} |
| 174 | +
|
| 175 | +type Author `@cacheControl`(maxAge: 3600) { |
| 176 | + id: ID! |
| 177 | + firstName: String! |
| 178 | + lastName: String! |
| 179 | + bookCount: Int! |
| 180 | + books: [Book!]! |
| 181 | +} |
| 182 | +
|
| 183 | +type Book { |
| 184 | + id: ID! |
| 185 | +} |
| 186 | +
|
| 187 | +# Operations |
| 188 | +
|
| 189 | +query GetAuthorsWithBooks { |
| 190 | + authors { # 3600 - Because it's of type Author which has maxAge 3600 |
| 191 | + id # 3600 - Because it's a scalar so inherits its parent field's maxAge |
| 192 | + bookCount `@cacheControl`(maxAge: 60) # 60 - Because it's overridden here in the operationSample |
| 193 | + books { # 0 - Default value, because no maxAge specified on Book |
| 194 | + # and it's not scalar so doesn't inherit its parent's maxAge |
| 195 | + id # 0 - Because it's a scalar so inherits its parent field's maxAge |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +## Schema and Operation Examples |
| 202 | + |
| 203 | +### Example 1 |
| 204 | + |
| 205 | +```graphql |
| 206 | +# Schema |
| 207 | + |
| 208 | +type Query { |
| 209 | + authors: [Author!]! |
| 210 | +} |
| 211 | + |
| 212 | +type Author `@cacheControl`(maxAge: 3600) { |
| 213 | + id: ID! |
| 214 | + firstName: String! |
| 215 | + lastName: String! |
| 216 | + bookCount: Int! `@cacheControl`(maxAge: 60) |
| 217 | + books: [Book!]! |
| 218 | +} |
| 219 | + |
| 220 | +type Book { |
| 221 | + id: ID! |
| 222 | +} |
| 223 | + |
| 224 | +# Operations |
| 225 | + |
| 226 | +query GetAuthorsWithBooks { |
| 227 | + authors { # 3600 - Because it's of type Author which has maxAge 3600 |
| 228 | + id # 3600 - Because it's a scalar so inherits its parent field's maxAge |
| 229 | + bookCount # 60 - Because it's specified on Author.bookCount |
| 230 | + books { # 0 - Default value, because no maxAge specified on Book |
| 231 | + # and it's not scalar so doesn't inherit its parent's maxAge |
| 232 | + id # 0 - Because it's a scalar so inherits its parent field's maxAge |
| 233 | + } |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +### Example 2 |
| 239 | + |
| 240 | +```graphql |
| 241 | +# Schema |
| 242 | + |
| 243 | +type Query { |
| 244 | + authors: [Author!]! |
| 245 | +} |
| 246 | + |
| 247 | +type Author `@cacheControl`(maxAge: 3600) { |
| 248 | + id: ID! |
| 249 | + firstName: String! |
| 250 | + lastName: String! |
| 251 | + bookCount: Int! `@cacheControl`(maxAge: 60) |
| 252 | + books: [Book!]! |
| 253 | +} |
| 254 | + |
| 255 | +type Book `@cacheControl`(maxAge: 1800) { |
| 256 | + id: ID! |
| 257 | +} |
| 258 | + |
| 259 | +# Operations |
| 260 | + |
| 261 | +query GetAuthorsWithBooks { |
| 262 | + authors { # 3600 - Because it's of type Author which has maxAge 3600 |
| 263 | + id # 3600 - Because it's a scalar so inherits its parent field's maxAge |
| 264 | + bookCount # 60 - Because it's specified on Author.bookCount |
| 265 | + books { # 1800 - Because it is specified on the Book type |
| 266 | + id # 1800 - Because it's a scalar so inherits its parent field's maxAge |
| 267 | + } |
| 268 | + } |
| 269 | +} |
| 270 | +``` |
| 271 | + |
| 272 | +### Example 3 |
| 273 | + |
| 274 | +```graphql |
| 275 | +# Schema |
| 276 | + |
| 277 | +type Query { |
| 278 | + authors: [Author!]! |
| 279 | +} |
| 280 | + |
| 281 | +type Author `@cacheControl`(maxAge: 3600) { |
| 282 | + id: ID! |
| 283 | + firstName: String! |
| 284 | + lastName: String! |
| 285 | + bookCount: Int! `@cacheControl`(maxAge: 60) |
| 286 | + books: [Book!]! `@cacheControl`(maxAge: 900) |
| 287 | +} |
| 288 | + |
| 289 | +type Book `@cacheControl`(maxAge: 1800) { |
| 290 | + id: ID! |
| 291 | +} |
| 292 | + |
| 293 | +# Operations |
| 294 | + |
| 295 | +query GetAuthorsWithBooks { |
| 296 | + authors { # 3600 - Because it's of type Author which has maxAge 3600 |
| 297 | + id # 3600 - Because it's a scalar so inherits its parent field's maxAge |
| 298 | + bookCount # 60 - Because it's specified on Author.bookCount |
| 299 | + books { # 900 - Because Author.books is overriding the maxAge specified on the Book type |
| 300 | + id # 900 - Because it's a scalar so inherits its parent field's maxAge |
| 301 | + } |
| 302 | + } |
| 303 | +} |
| 304 | +``` |
| 305 | + |
| 306 | +### Example with an interface |
| 307 | + |
| 308 | +```graphql |
| 309 | +# Schema |
| 310 | + |
| 311 | +type Query { |
| 312 | + auth: Auth |
| 313 | +} |
| 314 | + |
| 315 | +interface Sensitive `@cacheControl`(maxAge: 5) { |
| 316 | + id: ID! |
| 317 | +} |
| 318 | + |
| 319 | +type Auth implements Sensitive { |
| 320 | + id: ID! |
| 321 | + password: String |
| 322 | +} |
| 323 | + |
| 324 | +# Operations |
| 325 | + |
| 326 | +query GetAuthorsWithBooks { |
| 327 | + auth { # 5 - Because it's of type Author which implements Sensitive which |
| 328 | + # has maxAge 5 |
| 329 | + password # 5 - Because it's a scalar so inherits its parent field's maxAge |
| 330 | + } |
| 331 | +} |
| 332 | +``` |
0 commit comments