|
| 1 | +--- |
| 2 | +title: Defer and Stream |
| 3 | +--- |
| 4 | + |
| 5 | +# Defer and Stream |
| 6 | + |
| 7 | +Strawberry provides experimental support for GraphQL's `@defer` and `@stream` |
| 8 | +directives, which enable incremental delivery of response data. These directives |
| 9 | +allow parts of a GraphQL response to be delivered as they become available, |
| 10 | +rather than waiting for the entire response to be ready. |
| 11 | + |
| 12 | +<Note> |
| 13 | + |
| 14 | +This feature requires `graphql-core>=3.3.0a9` and is currently experimental. The |
| 15 | +API and behavior may change in future releases. |
| 16 | + |
| 17 | +**Important limitations:** |
| 18 | + |
| 19 | +- Extensions (most importantly `MaskErrors`) are not fully supported yet. |
| 20 | + Extensions currently only process the initial result and do not handle |
| 21 | + incremental payloads delivered by `@defer` and `@stream`. |
| 22 | +- This means error masking and other extension functionality will only apply to |
| 23 | + the initial response, not to deferred or streamed data. |
| 24 | + |
| 25 | +</Note> |
| 26 | + |
| 27 | +## Enabling Defer and Stream |
| 28 | + |
| 29 | +To use `@defer` and `@stream` directives, you need to enable experimental |
| 30 | +incremental execution in your schema configuration: |
| 31 | + |
| 32 | +```python |
| 33 | +import strawberry |
| 34 | +from strawberry.schema.config import StrawberryConfig |
| 35 | + |
| 36 | + |
| 37 | +@strawberry.type |
| 38 | +class Query: |
| 39 | + # Your query fields here |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +schema = strawberry.Schema( |
| 44 | + query=Query, config=StrawberryConfig(enable_experimental_incremental_execution=True) |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +## Using @defer |
| 49 | + |
| 50 | +The `@defer` directive allows you to mark parts of a query to be resolved |
| 51 | +asynchronously. The initial response will include all non-deferred fields, |
| 52 | +followed by incremental payloads containing the deferred data. |
| 53 | + |
| 54 | +### Example |
| 55 | + |
| 56 | +```python |
| 57 | +import asyncio |
| 58 | +import strawberry |
| 59 | + |
| 60 | + |
| 61 | +@strawberry.type |
| 62 | +class Author: |
| 63 | + id: strawberry.ID |
| 64 | + name: str |
| 65 | + bio: str |
| 66 | + |
| 67 | + |
| 68 | +@strawberry.type |
| 69 | +class Article: |
| 70 | + id: strawberry.ID |
| 71 | + title: str |
| 72 | + content: str |
| 73 | + |
| 74 | + @strawberry.field |
| 75 | + async def author(self) -> Author: |
| 76 | + # Simulate an expensive operation |
| 77 | + await asyncio.sleep(2) |
| 78 | + return Author( |
| 79 | + id=strawberry.ID("1"), |
| 80 | + name="Jane Doe", |
| 81 | + bio="A passionate writer and developer.", |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@strawberry.type |
| 86 | +class Query: |
| 87 | + @strawberry.field |
| 88 | + async def article(self, id: strawberry.ID) -> Article: |
| 89 | + return Article( |
| 90 | + id=id, |
| 91 | + title="Introduction to GraphQL Defer", |
| 92 | + content="Learn how to use the @defer directive...", |
| 93 | + ) |
| 94 | +``` |
| 95 | + |
| 96 | +With this schema, you can query with `@defer`: |
| 97 | + |
| 98 | +```graphql |
| 99 | +query GetArticle { |
| 100 | + article(id: "123") { |
| 101 | + id |
| 102 | + title |
| 103 | + content |
| 104 | + ... on Article @defer { |
| 105 | + author { |
| 106 | + id |
| 107 | + name |
| 108 | + bio |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +The response will be delivered incrementally: |
| 116 | + |
| 117 | +```json |
| 118 | +# Initial payload |
| 119 | +{ |
| 120 | + "data": { |
| 121 | + "article": { |
| 122 | + "id": "123", |
| 123 | + "title": "Introduction to GraphQL Defer", |
| 124 | + "content": "Learn how to use the @defer directive..." |
| 125 | + } |
| 126 | + }, |
| 127 | + "hasNext": true |
| 128 | +} |
| 129 | + |
| 130 | +# Subsequent payload |
| 131 | +{ |
| 132 | + "incremental": [{ |
| 133 | + "data": { |
| 134 | + "author": { |
| 135 | + "id": "1", |
| 136 | + "name": "Jane Doe", |
| 137 | + "bio": "A passionate writer and developer." |
| 138 | + } |
| 139 | + }, |
| 140 | + "path": ["article"] |
| 141 | + }], |
| 142 | + "hasNext": false |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Using @stream with strawberry.Streamable |
| 147 | + |
| 148 | +The `@stream` directive works with list fields and allows items to be delivered |
| 149 | +as they become available. Strawberry provides a special `Streamable` type |
| 150 | +annotation for fields that can be streamed. |
| 151 | + |
| 152 | +### Example |
| 153 | + |
| 154 | +```python |
| 155 | +import asyncio |
| 156 | +import strawberry |
| 157 | +from typing import AsyncGenerator |
| 158 | + |
| 159 | + |
| 160 | +@strawberry.type |
| 161 | +class Comment: |
| 162 | + id: strawberry.ID |
| 163 | + content: str |
| 164 | + author_name: str |
| 165 | + |
| 166 | + |
| 167 | +@strawberry.type |
| 168 | +class BlogPost: |
| 169 | + id: strawberry.ID |
| 170 | + title: str |
| 171 | + |
| 172 | + @strawberry.field |
| 173 | + async def comments(self) -> strawberry.Streamable[Comment]: |
| 174 | + """Stream comments as they are fetched from the database.""" |
| 175 | + for i in range(5): |
| 176 | + # Simulate fetching comments from a database |
| 177 | + await asyncio.sleep(0.5) |
| 178 | + yield Comment( |
| 179 | + id=strawberry.ID(f"comment-{i}"), |
| 180 | + content=f"This is comment number {i}", |
| 181 | + author_name=f"User {i}", |
| 182 | + ) |
| 183 | +``` |
| 184 | + |
| 185 | +Query with `@stream`: |
| 186 | + |
| 187 | +```graphql |
| 188 | +query GetBlogPost { |
| 189 | + blogPost(id: "456") { |
| 190 | + id |
| 191 | + title |
| 192 | + comments @stream(initialCount: 2) { |
| 193 | + id |
| 194 | + content |
| 195 | + authorName |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +The response will stream the comments: |
| 202 | + |
| 203 | +```json |
| 204 | +# Initial payload with first 2 comments |
| 205 | +{ |
| 206 | + "data": { |
| 207 | + "blogPost": { |
| 208 | + "id": "456", |
| 209 | + "title": "My Blog Post", |
| 210 | + "comments": [ |
| 211 | + { |
| 212 | + "id": "comment-0", |
| 213 | + "content": "This is comment number 0", |
| 214 | + "authorName": "User 0" |
| 215 | + }, |
| 216 | + { |
| 217 | + "id": "comment-1", |
| 218 | + "content": "This is comment number 1", |
| 219 | + "authorName": "User 1" |
| 220 | + } |
| 221 | + ] |
| 222 | + } |
| 223 | + }, |
| 224 | + "hasNext": true |
| 225 | +} |
| 226 | + |
| 227 | +# Subsequent payloads for remaining comments |
| 228 | +{ |
| 229 | + "incremental": [{ |
| 230 | + "items": [{ |
| 231 | + "id": "comment-2", |
| 232 | + "content": "This is comment number 2", |
| 233 | + "authorName": "User 2" |
| 234 | + }], |
| 235 | + "path": ["blogPost", "comments", 2] |
| 236 | + }], |
| 237 | + "hasNext": true |
| 238 | +} |
| 239 | +# ... more incremental payloads |
| 240 | +``` |
0 commit comments