-
-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 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
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() | ||
``` |