|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from typing import AsyncGenerator |
| 4 | + |
| 5 | +import sentry_sdk |
| 6 | +import strawberry |
| 7 | +from fastapi import FastAPI |
| 8 | +from sentry_sdk.integrations.strawberry import StrawberryIntegration |
| 9 | +from strawberry.fastapi import GraphQLRouter |
| 10 | + |
| 11 | +sentry_sdk.init( |
| 12 | + dsn=os.environ.get("SENTRY_DSN"), |
| 13 | + environment=os.environ.get("ENV", "test"), |
| 14 | + traces_sample_rate=1.0, |
| 15 | + profiles_sample_rate=1.0, |
| 16 | + debug=True, |
| 17 | + integrations=[ |
| 18 | + StrawberryIntegration(async_execution=True), |
| 19 | + ], |
| 20 | + _experiments={"trace_lifecycle": "stream"}, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@strawberry.type |
| 25 | +class Book: |
| 26 | + title: str |
| 27 | + author: str |
| 28 | + year: int |
| 29 | + |
| 30 | + |
| 31 | +books_db: list[Book] = [ |
| 32 | + Book(title="Oryx and Crake", author="Margaret Atwood", year=2003), |
| 33 | + Book(title="I, Robot", author="Isaac Asimov", year=1950), |
| 34 | + Book(title="A Closed and Common Orbit", author="Becky Chambers", year=2016), |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +@strawberry.type |
| 39 | +class AddBookResult: |
| 40 | + success: bool |
| 41 | + book: Book |
| 42 | + |
| 43 | + |
| 44 | +@strawberry.type |
| 45 | +class Query: |
| 46 | + @strawberry.field |
| 47 | + def hello(self) -> str: |
| 48 | + return "Hello from Strawberry GraphQL!" |
| 49 | + |
| 50 | + @strawberry.field |
| 51 | + def books(self) -> list[Book]: |
| 52 | + return books_db |
| 53 | + |
| 54 | + @strawberry.field |
| 55 | + def book_by_title(self, title: str) -> Book: |
| 56 | + for book in books_db: |
| 57 | + if book.title.lower() == title.lower(): |
| 58 | + return book |
| 59 | + raise ValueError(f"Book not found: {title}") |
| 60 | + |
| 61 | + @strawberry.field |
| 62 | + def error(self) -> str: |
| 63 | + raise ValueError("help! an error!") |
| 64 | + |
| 65 | + |
| 66 | +@strawberry.type |
| 67 | +class Mutation: |
| 68 | + @strawberry.mutation |
| 69 | + def add_book(self, title: str, author: str, year: int) -> AddBookResult: |
| 70 | + book = Book(title=title, author=author, year=year) |
| 71 | + books_db.append(book) |
| 72 | + return AddBookResult(success=True, book=book) |
| 73 | + |
| 74 | + @strawberry.mutation |
| 75 | + def delete_all_books(self) -> bool: |
| 76 | + books_db.clear() |
| 77 | + return True |
| 78 | + |
| 79 | + @strawberry.mutation |
| 80 | + def mutation_error(self) -> str: |
| 81 | + raise RuntimeError("mutation failed!") |
| 82 | + |
| 83 | + |
| 84 | +@strawberry.type |
| 85 | +class Subscription: |
| 86 | + @strawberry.subscription |
| 87 | + async def countdown(self, start: int = 5) -> AsyncGenerator[int, None]: |
| 88 | + for i in range(start, 0, -1): |
| 89 | + yield i |
| 90 | + await asyncio.sleep(1) |
| 91 | + |
| 92 | + @strawberry.subscription |
| 93 | + async def subscription_error(self) -> AsyncGenerator[str, None]: |
| 94 | + yield "starting..." |
| 95 | + raise RuntimeError("subscription exploded!") |
| 96 | + |
| 97 | + |
| 98 | +schema = strawberry.Schema( |
| 99 | + query=Query, |
| 100 | + mutation=Mutation, |
| 101 | + subscription=Subscription, |
| 102 | +) |
| 103 | + |
| 104 | +graphql_app = GraphQLRouter(schema) |
| 105 | + |
| 106 | +app = FastAPI() |
| 107 | +app.include_router(graphql_app, prefix="/graphql") |
0 commit comments