Skip to content

Navigation Menu

Sign in
Sign up

Lighthouse Get Cache Key via php #2621

Unanswered
illusive-ch asked this question in Q&A
Discussion options

I have this

 chatsPaginated(input: ChatsInput @spread): [Chat!]
 @guard
 @can(ability: "viewAny", model: "App\\Models\\Chat")
 @paginate(
 defaultCount: 10
 builder: "App\\GraphQL\\Queries\\ChatsPaginatedQuery@builder"
 )
 @cache(private: false)
 @cacheKey(fields: ["user_id"])
 @orderBy(column: "chat_updated_at", direction: DESC)

However, if a user is trashed using $user->delete(), I would need to flush the cache for all chat lists the user belongs to. I can do this with an observer and job, but I need help getting the actual key used by Lighthouse.

Stored cache key: lighthouse:query:dcda0ee478e6abe7cee5043b3221241c75c1fb13e8993e42a5196591983d53c0 

Here is the written test:

/**
 * @test
 */
 public function when_a_user_deleted_does_not_show_chat()
 {
 $user = User::factory()->create();
 $user2 = User::factory()->for($user->brand)->create();
 $chat = Chat::factory()->create(['is_paid' => false]);
 $chat->users()->attach([$user->id, $user2->id]);
 $this->actingAs($user);
 $response = $this->graphQL('
 query {
 chatsPaginated {
 data{
 id
 users {
 id
 name
 }
 }
 }
 }
')->assertStatus(200);
 // Log cache keys after the query
 $this->assertCount(1, $response->json('data.chatsPaginated.data'));
 $user2->delete();
 // Dump all cache keys again
 $response = $this->graphQL('
 query {
 chatsPaginated {
 data{
 id
 users {
 id
 name
 }
 }
 }
')->assertStatus(200);
 $this->assertCount(0, $response->json('data.chatsPaginated.data'));
 }
You must be logged in to vote

Replies: 1 comment

Comment options

You can get inspired by the implementation of @clearCache, see https://github.com/nuwave/lighthouse/blob/master/src/Cache/ClearCacheDirective.php.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /