@triggery/query
Read cached TanStack Query data from a Triggery condition. Pull-only: queryClient.getQueryData(key) runs only when a trigger fires, the host component is never subscribed to cache updates.
Install
Section titled “Install”pnpm add @triggery/core @triggery/react @triggery/query @tanstack/query-core npm install @triggery/core @triggery/react @triggery/query @tanstack/query-core yarn add @triggery/core @triggery/react @triggery/query @tanstack/query-core bun add @triggery/core @triggery/react @triggery/query @tanstack/query-core Peer deps: react >= 18.0.0, @tanstack/query-core ^5. @tanstack/react-query re-exports query-core and works out of the box.
What’s inside
Section titled “What’s inside”| Export | Purpose |
|---|---|
useQueryCondition(trigger, name, queryClient, queryKey, selector?) | Register a condition backed by a cached TanStack Query entry. |
Quick example
Section titled “Quick example”import { QueryClient } from '@tanstack/query-core';
import { createTrigger } from '@triggery/core';
import { useQueryCondition } from '@triggery/query';
type User = { id: string; name: string };
const queryClient = new QueryClient();
const messageTrigger = createTrigger<{
events: { 'new-message': { text: string; from: string } };
conditions: { user: User };
actions: { showToast: { body: string } };
}>({
id: 'message-received',
events: ['new-message'],
required: ['user'],
handler({ event, conditions, actions }) {
if (event.payload.from === conditions.user.id) return; // don't toast own messages
actions.showToast?.({ body: event.payload.text });
},
});
function CurrentUserBridge() {
useQueryCondition(messageTrigger, 'user', queryClient, ['user', 'current']);
return null;
}With a projection selector:
useQueryCondition(messageTrigger, 'user', queryClient, ['profile'], (p) => p?.user);How it works
Section titled “How it works”Pull-only: queryClient.getQueryData(key) runs only when a trigger fires, not on every cache update. The host component is never subscribed to the query — use useQuery / useQueryClient alongside in components that render the data.
When the cache entry is missing, the condition value is undefined, which fails a required gate cleanly and skips the handler instead of throwing.
useQueryCondition<T, S, K>(
trigger: Trigger<S>,
name: K,
queryClient: { getQueryData<T>(key: readonly unknown[]): T | undefined },
queryKey: readonly unknown[],
selector?: (data: T | undefined) => ConditionMap<S>[K] | undefined,
): voidRelated packages
Section titled “Related packages” @triggery/core Exposes Trigger.
@triggery/react useQueryCondition is a React hook.
@triggery/zustand Adapter for non-server client state.
@triggery/redux Adapter for Redux state.
@triggery/socket Pair query cache reads with socket-driven invalidations.