1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import asyncio # noqa: INP001
import random
import strawberry
from strawberry.schema.config import StrawberryConfig
@strawberry.type
class Author:
id: strawberry.ID
name: str
@strawberry.type
class Comment:
id: strawberry.ID
content: str
@strawberry.field
async def author(self) -> Author:
await asyncio.sleep(random.uniform(0, 0.5)) # noqa: S311
return Author(id=strawberry.ID("Author:1"), name="John Doe")
@strawberry.type
class BlogPost:
id: strawberry.ID
title: str
content: str
@strawberry.field
async def comments(self) -> strawberry.Streamable[Comment]:
for x in range(5):
await asyncio.sleep(random.uniform(0, 0.5)) # noqa: S311
yield Comment(id=strawberry.ID(f"Comment:{x}"), content="Great post!")
@strawberry.type
class Query:
@strawberry.field
async def hello(self, delay: float = 0) -> str:
await asyncio.sleep(delay)
return "Hello, world!"
@strawberry.field
async def blog_post(self, id: strawberry.ID) -> BlogPost:
return BlogPost(id=id, title="My Blog Post", content="This is my blog post.")
schema = strawberry.Schema(
query=Query,
config=StrawberryConfig(
enable_experimental_incremental_execution=True,
),
)
|