Skip to content

Commit f68c333

Browse files
ericapisaniclaude
andcommitted
feat: Add Strawberry test project
Create a new test project for Strawberry GraphQL framework with basic setup. Includes main application file, project configuration, and run script. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent f9b7f31 commit f68c333

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

test-strawberry/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.venv/
2+
uv.lock

test-strawberry/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.14

test-strawberry/main.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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")

test-strawberry/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[project]
2+
name = "test-strawberry"
3+
version = "0"
4+
requires-python = ">=3.12"
5+
6+
dependencies = [
7+
"fastapi>=0.115.11",
8+
"ipdb>=0.13.13",
9+
"sentry-sdk[strawberry]",
10+
"strawberry-graphql[fastapi]>=0.243.0",
11+
"uvicorn>=0.34.0",
12+
]
13+
14+
[tool.uv.sources]
15+
sentry-sdk = { path = "../../sentry-python", editable = true }

test-strawberry/run.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# exit on first error
4+
set -euo pipefail
5+
6+
# Install uv if it's not installed
7+
if ! command -v uv &> /dev/null; then
8+
curl -LsSf https://astral.sh/uv/install.sh | sh
9+
fi
10+
11+
# Run the script
12+
uv run uvicorn main:app --port 8000 --reload

0 commit comments

Comments
 (0)