Live Queries
Live queries allow the server to push updates to clients whenever the underlying data changes, using resource-based invalidation and JSON diff-patching.
How It Works
Section titled “How It Works”- A query is marked as
@livein the schema - The server tracks which resource identifiers the query accesses
- When a mutation invalidates one of those identifiers, the query re-executes
- Only the diff is sent to the client via
jsondiffpatch
Marking Queries as Live
Section titled “Marking Queries as Live”Add the @live directive to query fields in your schema:
type Query { users: [User!]! @live}Invalidating Resources
Section titled “Invalidating Resources”From within a resolver, use the liveQueryStore from context:
const resolvers = { Mutation: { createUser: async (_, args, context) => { const user = await db.createUser(args) await context.liveQueryStore.invalidate('Query.users') return user }, },}Invalidation Helpers
Section titled “Invalidation Helpers”The invalidate function accepts multiple formats:
// Simple stringliveQueryStore.invalidate('Query.users')
// Multiple identifiersliveQueryStore.invalidate(['Query.users', 'Query.userCount'])
// Builder functionliveQueryStore.invalidate((tools) => [ tools.id('User', user.id), tools.args('Query.user', { id: user.id }),])Live Identifier Directive
Section titled “Live Identifier Directive”Use @liveIdentifier on arguments to automatically scope live query subscriptions:
type Query { user(id: ID! @liveIdentifier): User @live}Redis-Backed Invalidation
Section titled “Redis-Backed Invalidation”For multi-instance deployments, pass redisUrl to share invalidations across servers:
createServer({ liveQueryStore: { redisUrl: 'redis://localhost:6379', redisChannel: 'my-app-live-queries', },})