Field extensions allow you to implement reusable logic (like permissions, pagination, or data transformations) outside of your resolvers. They wrap the underlying resolver, allowing you to modify the field or the arguments passed to it.
To create an extension, subclass strawberry.extensions.FieldExtension and implement the resolve method. The resolve method receives a next_ argument, which is the next function in the chain (either the next extension or the actual resolver).
Note: The examples below cover synchronous execution. For asynchronous support, see the Async Extensions and Resolvers section.
import strawberry
from strawberry.extensions import FieldExtension
from typing import Callable, Any
class UpperCaseExtension(FieldExtension):
def resolve(
self, next_: Callable[..., Any], source: Any, info: strawberry.Info, **kwargs
):
# Call the next resolver/extension in the chain
result = next_(source, info, **kwargs)
# Modify the result
return str(result).upper()
@strawberry.type
class Query:
@strawberry.field(extensions=[UpperCaseExtension()])
def string(self) -> str:
return "This is a test!!"