-
-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark for generic inputs (#3547)
* Add benchmark * Bump cache? * Fix typing issue * Make test smaller
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import asyncio | ||
from typing import Generic, List, Optional, TypeVar | ||
|
||
from pytest_codspeed.plugin import BenchmarkFixture | ||
|
||
import strawberry | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
@strawberry.input(description="Filter for GraphQL queries") | ||
class GraphQLFilter(Generic[T]): | ||
"""EXTERNAL Filter for GraphQL queries""" | ||
|
||
eq: Optional[T] = None | ||
in_: Optional[List[T]] = None | ||
nin: Optional[List[T]] = None | ||
gt: Optional[T] = None | ||
gte: Optional[T] = None | ||
lt: Optional[T] = None | ||
lte: Optional[T] = None | ||
contains: Optional[T] = None | ||
icontains: Optional[T] = None | ||
|
||
|
||
@strawberry.type | ||
class Author: | ||
name: str | ||
|
||
|
||
@strawberry.type | ||
class Book: | ||
title: str | ||
|
||
@strawberry.field | ||
async def authors( | ||
self, | ||
name: Optional[GraphQLFilter[str]] = None, | ||
) -> List[Author]: | ||
return [Author(name="F. Scott Fitzgerald")] | ||
|
||
|
||
def get_books(): | ||
return [ | ||
Book(title="The Great Gatsby"), | ||
] * 1000 | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
books: List[Book] = strawberry.field(resolver=get_books) | ||
|
||
|
||
schema = strawberry.Schema(query=Query) | ||
|
||
query = """{ | ||
books { | ||
title | ||
authors(name: {eq: "F. Scott Fitzgerald"}) { | ||
name | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
def test_execute_generic_input(benchmark: BenchmarkFixture): | ||
def run(): | ||
coroutine = schema.execute(query) | ||
|
||
return asyncio.run(coroutine) | ||
|
||
result = benchmark(run) | ||
|
||
assert not result.errors |