-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make sure that async fields always return Awaitables (#646)
- Loading branch information
1 parent
92c1221
commit 31bb262
Showing
2 changed files
with
275 additions
and
2 deletions.
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,272 @@ | ||
from collections.abc import Awaitable | ||
from typing import Any, Union | ||
|
||
import pytest | ||
import strawberry | ||
from strawberry import BasePermission, Info | ||
|
||
import strawberry_django | ||
from strawberry_django.optimizer import DjangoOptimizerExtension | ||
from tests import models | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
async def test_with_async_permission(db): | ||
class AsyncPermission(BasePermission): | ||
async def has_permission( # type: ignore | ||
self, | ||
source: Any, | ||
info: Info, | ||
**kwargs: Any, | ||
) -> Union[bool, Awaitable[bool]]: | ||
return True | ||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: strawberry.auto | ||
|
||
@strawberry_django.type(models.Color) | ||
class Color: | ||
name: strawberry.auto | ||
fruits: list[Fruit] = strawberry_django.field( | ||
permission_classes=[AsyncPermission] | ||
) | ||
|
||
@strawberry.type(name="Query") | ||
class Query: | ||
colors: list[Color] = strawberry_django.field() | ||
|
||
red = await models.Color.objects.acreate(name="Red") | ||
yellow = await models.Color.objects.acreate(name="Yellow") | ||
|
||
await models.Fruit.objects.acreate(name="Apple", color=red) | ||
await models.Fruit.objects.acreate(name="Banana", color=yellow) | ||
await models.Fruit.objects.acreate(name="Strawberry", color=red) | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
colors { | ||
name | ||
fruits { | ||
name | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = await schema.execute(query) | ||
assert result.errors is None | ||
assert result.data == { | ||
"colors": [ | ||
{ | ||
"name": "Red", | ||
"fruits": [ | ||
{"name": "Apple"}, | ||
{"name": "Strawberry"}, | ||
], | ||
}, | ||
{ | ||
"name": "Yellow", | ||
"fruits": [{"name": "Banana"}], | ||
}, | ||
] | ||
} | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
async def test_with_async_permission_and_optimizer(db): | ||
class AsyncPermission(BasePermission): | ||
async def has_permission( # type: ignore | ||
self, | ||
source: Any, | ||
info: Info, | ||
**kwargs: Any, | ||
) -> Union[bool, Awaitable[bool]]: | ||
return True | ||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: strawberry.auto | ||
|
||
@strawberry_django.type(models.Color) | ||
class Color: | ||
name: strawberry.auto | ||
fruits: list[Fruit] = strawberry_django.field( | ||
permission_classes=[AsyncPermission] | ||
) | ||
|
||
@strawberry.type(name="Query") | ||
class Query: | ||
colors: list[Color] = strawberry_django.field() | ||
|
||
red = await models.Color.objects.acreate(name="Red") | ||
yellow = await models.Color.objects.acreate(name="Yellow") | ||
|
||
await models.Fruit.objects.acreate(name="Apple", color=red) | ||
await models.Fruit.objects.acreate(name="Banana", color=yellow) | ||
await models.Fruit.objects.acreate(name="Strawberry", color=red) | ||
|
||
schema = strawberry.Schema( | ||
query=Query, | ||
extensions=[DjangoOptimizerExtension()], | ||
) | ||
query = """ | ||
query { | ||
colors { | ||
name | ||
fruits { | ||
name | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = await schema.execute(query) | ||
assert result.errors is None | ||
assert result.data == { | ||
"colors": [ | ||
{ | ||
"name": "Red", | ||
"fruits": [ | ||
{"name": "Apple"}, | ||
{"name": "Strawberry"}, | ||
], | ||
}, | ||
{ | ||
"name": "Yellow", | ||
"fruits": [{"name": "Banana"}], | ||
}, | ||
] | ||
} | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_with_sync_permission(db): | ||
class AsyncPermission(BasePermission): | ||
def has_permission( | ||
self, | ||
source: Any, | ||
info: Info, | ||
**kwargs: Any, | ||
) -> Union[bool, Awaitable[bool]]: | ||
return True | ||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: strawberry.auto | ||
|
||
@strawberry_django.type(models.Color) | ||
class Color: | ||
name: strawberry.auto | ||
fruits: list[Fruit] = strawberry_django.field( | ||
permission_classes=[AsyncPermission] | ||
) | ||
|
||
@strawberry.type(name="Query") | ||
class Query: | ||
colors: list[Color] = strawberry_django.field() | ||
|
||
red = models.Color.objects.create(name="Red") | ||
yellow = models.Color.objects.create(name="Yellow") | ||
|
||
models.Fruit.objects.create(name="Apple", color=red) | ||
models.Fruit.objects.create(name="Banana", color=yellow) | ||
models.Fruit.objects.create(name="Strawberry", color=red) | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
colors { | ||
name | ||
fruits { | ||
name | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.errors is None | ||
assert result.data == { | ||
"colors": [ | ||
{ | ||
"name": "Red", | ||
"fruits": [ | ||
{"name": "Apple"}, | ||
{"name": "Strawberry"}, | ||
], | ||
}, | ||
{ | ||
"name": "Yellow", | ||
"fruits": [{"name": "Banana"}], | ||
}, | ||
] | ||
} | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_with_sync_permission_and_optimizer(db): | ||
class AsyncPermission(BasePermission): | ||
def has_permission( | ||
self, | ||
source: Any, | ||
info: Info, | ||
**kwargs: Any, | ||
) -> Union[bool, Awaitable[bool]]: | ||
return True | ||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: strawberry.auto | ||
|
||
@strawberry_django.type(models.Color) | ||
class Color: | ||
name: strawberry.auto | ||
fruits: list[Fruit] = strawberry_django.field( | ||
permission_classes=[AsyncPermission] | ||
) | ||
|
||
@strawberry.type(name="Query") | ||
class Query: | ||
colors: list[Color] = strawberry_django.field() | ||
|
||
red = models.Color.objects.create(name="Red") | ||
yellow = models.Color.objects.create(name="Yellow") | ||
|
||
models.Fruit.objects.create(name="Apple", color=red) | ||
models.Fruit.objects.create(name="Banana", color=yellow) | ||
models.Fruit.objects.create(name="Strawberry", color=red) | ||
|
||
schema = strawberry.Schema( | ||
query=Query, | ||
extensions=[DjangoOptimizerExtension()], | ||
) | ||
query = """ | ||
query { | ||
colors { | ||
name | ||
fruits { | ||
name | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.errors is None | ||
assert result.data == { | ||
"colors": [ | ||
{ | ||
"name": "Red", | ||
"fruits": [ | ||
{"name": "Apple"}, | ||
{"name": "Strawberry"}, | ||
], | ||
}, | ||
{ | ||
"name": "Yellow", | ||
"fruits": [{"name": "Banana"}], | ||
}, | ||
] | ||
} |