-
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for optional connections (#3707)
* Support nullable Connection types in relay field decorator Enable nullable Connection types in the connection field decorator by updating type checking logic and adding validation for inner types. Update documentation and add tests to ensure compatibility with permission extensions and different nullable syntax. New Features: - Support nullable Connection types in the connection field decorator in strawberry.relay.fields. Enhancements: - Update type checking logic to handle Optional[Connection[T]] and Connection[T] | None annotations. Documentation: - Update documentation to reflect that connection fields can now be nullable. Tests: - Add tests to verify nullable connection fields work correctly with permission extensions and both Optional[Connection[T]] and Connection[T] | None syntax. Resolves #3703 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Initial working version * Fix typos and types * Pre-commit * Fix missing `@` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add release file * Update tests/relay/test_connection.py Co-authored-by: Thiago Bellini Ribeiro <thiago@bellini.dev> * Add check --------- Co-authored-by: sourcery-ai[bot] <sourcery-ai[bot]@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thiago Bellini Ribeiro <thiago@bellini.dev>
- Loading branch information
1 parent
c9dac1d
commit 8a8e3aa
Showing
5 changed files
with
201 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Release type: minor | ||
|
||
This release adds support for making Relay connection optional, this is useful | ||
when you want to add permission classes to the connection and not fail the whole | ||
query if the user doesn't have permission to access the connection. | ||
|
||
Example: | ||
|
||
```python | ||
import strawberry | ||
from strawberry import relay | ||
from strawberry.permission import BasePermission | ||
|
||
|
||
class IsAuthenticated(BasePermission): | ||
message = "User is not authenticated" | ||
|
||
# This method can also be async! | ||
def has_permission( | ||
self, source: typing.Any, info: strawberry.Info, **kwargs | ||
) -> bool: | ||
return False | ||
|
||
|
||
@strawberry.type | ||
class Fruit(relay.Node): | ||
code: relay.NodeID[int] | ||
name: str | ||
weight: float | ||
|
||
@classmethod | ||
def resolve_nodes( | ||
cls, | ||
*, | ||
info: strawberry.Info, | ||
node_ids: Iterable[str], | ||
): | ||
return [] | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
node: relay.Node = relay.node() | ||
|
||
@relay.connection( | ||
relay.ListConnection[Fruit] | None, permission_classes=[IsAuthenticated()] | ||
) | ||
def fruits(self) -> Iterable[Fruit]: | ||
# This can be a database query, a generator, an async generator, etc | ||
return all_fruits.values() | ||
``` |
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
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,125 @@ | ||
import sys | ||
from typing import Any, Iterable, List, Optional | ||
from typing_extensions import Self | ||
|
||
import pytest | ||
|
||
import strawberry | ||
from strawberry.permission import BasePermission | ||
from strawberry.relay import Connection, Node | ||
|
||
|
||
@strawberry.type | ||
class User(Node): | ||
id: strawberry.relay.NodeID | ||
name: str = "John" | ||
|
||
@classmethod | ||
def resolve_nodes( | ||
cls, *, info: strawberry.Info, node_ids: List[Any], required: bool | ||
) -> List[Self]: | ||
return [cls() for _ in node_ids] | ||
|
||
|
||
@strawberry.type | ||
class UserConnection(Connection[User]): | ||
@classmethod | ||
def resolve_connection( | ||
cls, | ||
nodes: Iterable[User], | ||
*, | ||
info: Any, | ||
after: Optional[str] = None, | ||
before: Optional[str] = None, | ||
first: Optional[int] = None, | ||
last: Optional[int] = None, | ||
) -> Optional[Self]: | ||
return None | ||
|
||
|
||
class TestPermission(BasePermission): | ||
message = "Not allowed" | ||
|
||
def has_permission(self, source, info, **kwargs: Any): | ||
return False | ||
|
||
|
||
def test_nullable_connection_with_optional(): | ||
@strawberry.type | ||
class Query: | ||
@strawberry.relay.connection(Optional[UserConnection]) | ||
def users(self) -> Optional[List[User]]: | ||
return None | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
users { | ||
edges { | ||
node { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.data == {"users": None} | ||
assert not result.errors | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.version_info < (3, 10), | ||
reason="pipe syntax for union is only available on python 3.10+", | ||
) | ||
def test_nullable_connection_with_pipe(): | ||
@strawberry.type | ||
class Query: | ||
@strawberry.relay.connection(UserConnection | None) | ||
def users(self) -> List[User] | None: | ||
return None | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
users { | ||
edges { | ||
node { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.data == {"users": None} | ||
assert not result.errors | ||
|
||
|
||
def test_nullable_connection_with_permission(): | ||
@strawberry.type | ||
class Query: | ||
@strawberry.relay.connection( | ||
Optional[UserConnection], permission_classes=[TestPermission] | ||
) | ||
def users(self) -> Optional[List[User]]: # pragma: no cover | ||
pytest.fail("Should not have been called...") | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
users { | ||
edges { | ||
node { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.data == {"users": None} | ||
assert result.errors[0].message == "Not allowed" |