GraphQL caching in Strawberry #4188
Unanswered
nogueira-raphael
asked this question in
Show and tell
Replies: 1 comment
-
|
Strawberry doesn't have built-in support for cross-request caching in resolvers, but you can integrate caching like Redis by decorating resolver methods. Here's a simple way to implement it: import strawberry
import redis
from functools import lru_cache
cache = redis.Redis(host='localhost', port=6379, db=0)
@lru_cache
def get_cached_user(user_key):
return cache.get(user_key)
@strawberry.type
class User:
name: str
age: int
@strawberry.type
class Query:
@strawberry.field
def user(self, user_id: str) -> User:
# Check cache first
user = get_cached_user(user_id)
if user:
return user
# Simulate fetching user from database or other source
user = User(name="Patrick", age=100)
# Cache the result
cache.set(user_id, user)
return user
schema = strawberry.Schema(query=Query)The |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone 👋
I’ve been working with GraphQL APIs in Python and I’m trying to implement caching in a way that’s reusable and safe.
I know Strawberry already provides:
Those are great, but my use case is slightly different: I’m looking for an approach to cache resolver results across requests (e.g., Redis / TTL cache) to reduce repeated expensive queries or heavy downstream calls.
I couldn’t find a “standard” Strawberry extension/pattern for resolver-level caching, so as a personal exercise I built a small framework-agnostic library.
PyPI (pre-release): https://pypi.org/project/cacheql/
I’d really appreciate feedback from Strawberry maintainers/users on:
What would be the preferred integration point in Strawberry.
Thanks a lot, any feedback is welcome!
Beta Was this translation helpful? Give feedback.
All reactions