Skip to content

Commit

Permalink
Update field-extension code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirusblk authored and patrick91 committed Apr 3, 2024
1 parent 8846de2 commit f6d8c71
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions docs/guides/field-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ contexts, please have a look at
</Note>

```python
import typing

import strawberry
from strawberry.extensions import FieldExtension


class UpperCaseExtension(FieldExtension):
def resolve(
self, next_: Callable[..., Any], source: Any, info: strawberry.Info, **kwargs
):
self,
next_: typing.Callable[..., typing.Any],
source: typing.Any,
info: strawberry.Info,
**kwargs: typing.Dict[str, typing.Any],
) -> str:
result = next_(source, info, **kwargs)
return str(result).upper()

Expand Down Expand Up @@ -69,10 +75,12 @@ field:

```python
import time
import typing

import strawberry
from strawberry.extensions import FieldExtension
from strawberry.schema_directive import Location
from strawberry.field import StrawberryField
from strawberry.schema_directive import Location


@strawberry.schema_directive(locations=[Location.FIELD_DEFINITION])
Expand All @@ -81,7 +89,7 @@ class Cached:


class CachingExtension(FieldExtension):
def __init__(self, caching_time=100):
def __init__(self, caching_time: int = 100):
self.caching_time = caching_time
self.last_cached = 0.0
self.cached_result = None
Expand All @@ -90,8 +98,12 @@ class CachingExtension(FieldExtension):
field.directives.append(Cached(time=self.caching_time))

def resolve(
self, next_: Callable[..., Any], source: Any, info: strawberry.Info, **kwargs
) -> Any:
self,
next_: typing.Callable[..., typing.Any],
source: typing.Any,
info: strawberry.Info,
**kwargs: typing.Dict[str, typing.Any],
) -> typing.Any:
current_time = time.time()
if self.last_cached + self.caching_time > current_time:
return self.cached_result
Expand All @@ -103,12 +115,13 @@ class CachingExtension(FieldExtension):
@strawberry.type
class Client:
@strawberry.field(extensions=[CachingExtensions(caching_time=200)])
def analyzed_hours(self, info) -> int:
return do_expensive_computation()
@strawberry.field(extensions=[CachingExtension(caching_time=200)])
def analyzed_hours(self, info: strawberry.types.Info) -> int:
# Do some expensive computation
...
---
type Client {
analyzedHours: Int! @Cached(time=200)
analyzedHours: Int! @cached(time: 200)
}
```

Expand All @@ -121,8 +134,20 @@ extension. This allows for creating a chain of field extensions that each
perform a specific transformation on the data being passed through them.

```python
class LowerCaseExtension(FieldExtension):
def resolve(
self,
next_: typing.Callable[..., typing.Any],
source: typing.Any,
info: strawberry.types.Info,
**kwargs: typing.Dict[str, typing.Any],
) -> str:
result = next_(source, info, **kwargs)
return str(result).lower()


@strawberry.field(extensions=[LowerCaseExtension(), UpperCaseExtension()])
def my_field():
def my_field() -> str:
return "My Result"
```

Expand Down Expand Up @@ -167,18 +192,22 @@ from strawberry.extensions import FieldExtension

class UpperCaseExtension(FieldExtension):
def resolve(
self, next: Callable[..., Any], source: Any, info: strawberry.Info, **kwargs
):
self,
next_: typing.Callable[..., typing.Any],
source: typing.Any,
info: strawberry.Info,
**kwargs: typing.Dict[str, typing.Any],
) -> str:
result = next(source, info, **kwargs)
return str(result).upper()

async def resolve_async(
self,
next: Callable[..., Awaitable[Any]],
source: Any,
next_: typing.Callable[..., typing.Any],
source: typing.Any,
info: strawberry.Info,
**kwargs
):
**kwargs: typing.Dict[str, typing.Any],
) -> str:
result = await next(source, info, **kwargs)
return str(result).upper()

Expand Down

0 comments on commit f6d8c71

Please sign in to comment.