-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
069fe2c
commit 936a1d1
Showing
2 changed files
with
136 additions
and
0 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,124 @@ | ||
from typing import List, Optional, Union | ||
|
||
import pytest | ||
|
||
import strawberry | ||
from strawberry.permission import BasePermission | ||
from strawberry.relay import Connection, Node, connection | ||
|
||
|
||
@strawberry.type | ||
class User(Node): | ||
name: str = "John" | ||
|
||
@classmethod | ||
def resolve_nodes(cls, *, info, node_ids, required): | ||
return [cls() for _ in node_ids] | ||
|
||
|
||
class TestPermission(BasePermission): | ||
message = "Not allowed" | ||
|
||
def has_permission(self, source, info, **kwargs): | ||
return False | ||
|
||
|
||
def test_nullable_connection_with_optional(): | ||
@strawberry.type | ||
class Query: | ||
@connection | ||
def users(self) -> Optional[Connection[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 | ||
|
||
|
||
def test_nullable_connection_with_union(): | ||
@strawberry.type | ||
class Query: | ||
@connection | ||
def users(self) -> Union[Connection[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.permission_classes([TestPermission]) | ||
@connection | ||
def users(self) -> Optional[Connection[User]]: | ||
return Connection[User](edges=[], page_info=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_non_nullable_connection(): | ||
@strawberry.type | ||
class Query: | ||
@connection | ||
def users(self) -> Connection[User]: | ||
return Connection[User](edges=[], page_info=None) | ||
|
||
schema = strawberry.Schema(query=Query) | ||
query = """ | ||
query { | ||
users { | ||
edges { | ||
node { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert result.data == {"users": {"edges": []}} | ||
assert not result.errors |