From c4714ad07be59f8ef01e0ff7b71d8b25b8dad7a2 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 8 Jan 2025 09:26:44 -0500 Subject: [PATCH] Add docs for .items_for inside resolvers --- guides/object_cache/caching.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/guides/object_cache/caching.md b/guides/object_cache/caching.md index 47740a7cc0..1d39b94b14 100644 --- a/guides/object_cache/caching.md +++ b/guides/object_cache/caching.md @@ -126,6 +126,15 @@ class Query < GraphQL::Schema::Object end ``` +If you're using {{ "GraphQL::Schema::Resolver" | api_doc }}, you'd call `.items_for` slightly differently: + +```ruby +def resolve(division: nil) + # use `context[:current_object]` to get the GraphQL::Schema::Object instance whose field is being resolved + AllTeams.items_for(context[:current_object], division: division) +end +``` + Finally, you'll need to handle `CacheableRelation`s in your object identification methods, for example: ```ruby @@ -149,11 +158,11 @@ class MySchema < GraphQL::Schema end ``` -In this example, `AllTeams` takes care of integrating with the cache: +In this example, `AllTeams` implements several methods to support caching: -- It implements `#id` to create a cache-friendly, stable global ID -- It implements `#to_param` to create a cache fingerprint (using Rails's `#cache_key` under the hood) -- It implements `.find?` to retrieve the list based on its ID +- `#id` creates a cache-friendly, stable global ID +- `#to_param` creates a cache fingerprint (using Rails's `#cache_key` under the hood) +- `.find?` retrieves the list based on its ID This way, if a `Team` is created, the cached result will be invalidated and a fresh result will be created.